From fb8906af794f183faacf951cd19de817e12b7af6 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 19 Jan 2018 12:12:59 +0000
Subject: [PATCH] Get the open round and allow for open ended rounds on the
 front end

---
 opentech/apply/funds/models.py                | 14 ++++---
 opentech/apply/funds/tests/test_models.py     | 38 +++++++++++++++++++
 opentech/public/funds/models.py               |  4 ++
 .../public_funds/includes/fund_apply_cta.html |  4 +-
 4 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 6b2395e06..271e3f157 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -50,12 +50,16 @@ class FundType(AbstractStreamForm):
     def workflow_class(self):
         return WORKFLOW_CLASS[self.get_workflow_display()]
 
-    def next_deadline(self):
+    @property
+    def open_round(self):
         rounds = Round.objects.child_of(self).live().public().specific()
-        open_rounds = rounds.filter(
-            end_date__gte=date.today(),
-        )
-        return open_rounds.first().end_date
+        return rounds.filter(
+            Q(start_date__lte=date.today()) &
+            Q(Q(end_date__isnull=True) | Q(end_date__gte=date.today()))
+        ).first()
+
+    def next_deadline(self):
+        return self.open_round.end_date
 
     content_panels = AbstractStreamForm.content_panels + [
         FieldPanel('workflow'),
diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py
index 5786f9ed9..78a65c820 100644
--- a/opentech/apply/funds/tests/test_models.py
+++ b/opentech/apply/funds/tests/test_models.py
@@ -8,12 +8,50 @@ from opentech.apply.funds.workflow import SingleStage
 from .factories import FundTypeFactory, RoundFactory
 
 
+def days_from_today(days):
+    return date.today() + timedelta(days=days)
+
+
 class TestFundModel(TestCase):
     def test_can_access_workflow_class(self):
         fund = FundTypeFactory(parent=None)
         self.assertEqual(fund.workflow, 'single')
         self.assertEqual(fund.workflow_class, SingleStage)
 
+    def test_no_open_rounds(self):
+        fund = FundTypeFactory(parent=None)
+        self.assertIsNone(fund.open_round)
+
+    def test_open_ended_round(self):
+        fund = FundTypeFactory(parent=None)
+        open_round = RoundFactory(end_date=None, parent=fund)
+        self.assertEqual(fund.open_round, open_round)
+
+    def test_normal_round(self):
+        fund = FundTypeFactory(parent=None)
+        open_round = RoundFactory(parent=fund)
+        self.assertEqual(fund.open_round, open_round)
+
+    def test_closed_round(self):
+        fund = FundTypeFactory(parent=None)
+        yesterday = days_from_today(-1)
+        last_week = days_from_today(-7)
+        RoundFactory(start_date=last_week, end_date=yesterday, parent=fund)
+        self.assertIsNone(fund.open_round)
+
+    def test_round_not_open(self):
+        fund = FundTypeFactory(parent=None)
+        tomorrow = days_from_today(1)
+        RoundFactory(start_date=tomorrow, parent=fund)
+        self.assertIsNone(fund.open_round)
+
+    def test_multiple_open_rounds(self):
+        fund = FundTypeFactory(parent=None)
+        open_round = RoundFactory(parent=fund)
+        next_round_start = open_round.end_date + timedelta(days=1)
+        RoundFactory(start_date=next_round_start, end_date=None, parent=fund)
+        self.assertEqual(fund.open_round, open_round)
+
 
 class TestRoundModel(TestCase):
     def setUp(self):
diff --git a/opentech/public/funds/models.py b/opentech/public/funds/models.py
index 7e51a9efa..1f0ccf9ab 100644
--- a/opentech/public/funds/models.py
+++ b/opentech/public/funds/models.py
@@ -47,6 +47,10 @@ class FundPage(BasePage):
         InlinePanel('related_pages', label="Related pages"),
     ]
 
+    @property
+    def has_open(self):
+        return bool(self.fund_type.specific.open_round)
+
     @property
     def deadline(self):
         return self.fund_type.specific.next_deadline()
diff --git a/opentech/public/funds/templates/public_funds/includes/fund_apply_cta.html b/opentech/public/funds/templates/public_funds/includes/fund_apply_cta.html
index 0fd7669e0..36033e8fe 100644
--- a/opentech/public/funds/templates/public_funds/includes/fund_apply_cta.html
+++ b/opentech/public/funds/templates/public_funds/includes/fund_apply_cta.html
@@ -2,10 +2,12 @@
 
 <div class="wrapper wrapper--flex">
     <div class="section section--apply-cta">
-        {% if fund_page.deadline %}
+        {% if fund_page.has_open %}
+            {% if fund_page.deadline %}
             <div class="deadline">
                 {% trans "Next deadline" %}: {{ fund_page.deadline|date:"M j, Y" }}
             </div>
+            {% endif %}
             <div class="apply-link">
                 <a class="button" href="{% pageurl fund_page.fund_type %}">{% trans "Apply for this fund" %}</a>
             </div>
-- 
GitLab