diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py index 6b2395e06e6eea35abc172a1ea218ff30fecf2e7..271e3f1575327fd64cfc95920f8e1519ab8d3c75 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 5786f9ed993e6348a2ac25e6573fd5cf994e96d0..78a65c820ad2a1ace49c278370ddb0b8778ce70e 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 7e51a9efaaef2d409458e443dd84f4fc0bfc5ea5..1f0ccf9abad62e845251493aa19f781944eb666a 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 0fd7669e0969b83d2afd4b443f3c4db69417362f..36033e8fec5901026951c7fb6cbe4079d5683228 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>