From f9a9305ec716f39f0d718c8eaa8d21986d069b25 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 19 Jan 2018 10:14:23 +0000
Subject: [PATCH] Add dates and pass the deadline to the page

---
 .../migrations/0006_add_dates_to_round.py     | 26 +++++++++++++++
 opentech/apply/funds/models.py                | 32 +++++++++++++++++++
 opentech/public/funds/models.py               |  4 +++
 .../templates/public_funds/fund_page.html     |  4 +--
 .../public_funds/includes/fund_apply_cta.html |  6 ++--
 5 files changed, 67 insertions(+), 5 deletions(-)
 create mode 100644 opentech/apply/funds/migrations/0006_add_dates_to_round.py

diff --git a/opentech/apply/funds/migrations/0006_add_dates_to_round.py b/opentech/apply/funds/migrations/0006_add_dates_to_round.py
new file mode 100644
index 000000000..747ef1347
--- /dev/null
+++ b/opentech/apply/funds/migrations/0006_add_dates_to_round.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.8 on 2018-01-18 16:24
+from __future__ import unicode_literals
+
+import datetime
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('funds', '0005_round'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='round',
+            name='end_date',
+            field=models.DateField(blank=True, default=datetime.date.today),
+        ),
+        migrations.AddField(
+            model_name='round',
+            name='start_date',
+            field=models.DateField(blank=True, default=datetime.date.today),
+        ),
+    ]
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index dabc84d28..55d6874e4 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -1,9 +1,14 @@
+from datetime import date
+
+from django.core.exceptions import ValidationError
 from django.db import models
 
 from modelcluster.fields import ParentalKey
 from wagtail.wagtailadmin.edit_handlers import (
     FieldPanel,
     InlinePanel,
+    FieldRowPanel,
+    MultiFieldPanel,
     StreamFieldPanel,
 )
 from wagtail.wagtailcore.fields import StreamField
@@ -43,6 +48,13 @@ class FundType(AbstractStreamForm):
     def workflow_class(self):
         return WORKFLOW_CLASS[self.get_workflow_display()]
 
+    def next_deadline(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
+
     content_panels = AbstractStreamForm.content_panels + [
         FieldPanel('workflow'),
         InlinePanel('forms', label="Forms"),
@@ -75,6 +87,26 @@ class Round(AbstractStreamForm):
     parent_page_types = ['funds.FundType']
     subpage_types = []  # type: ignore
 
+    start_date = models.DateField(blank=True, default=date.today)
+    end_date = models.DateField(blank=True, default=date.today)
+
+    content_panels = AbstractStreamForm.content_panels + [
+        MultiFieldPanel([
+            FieldRowPanel([
+                FieldPanel('start_date'),
+                FieldPanel('end_date'),
+            ]),
+        ], heading="Dates")
+    ]
+
     def get_defined_fields(self):
         # Only return the first form, will need updating for when working with 2 stage WF
         return self.get_parent().specific.forms.all()[0].fields
+
+    def clean(self):
+        super().clean()
+
+        if self.start_date > self.end_date:
+            raise ValidationError({
+                'end_date': 'End date must come after the start date',
+            })
diff --git a/opentech/public/funds/models.py b/opentech/public/funds/models.py
index 0854ec5db..7e51a9efa 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 deadline(self):
+        return self.fund_type.specific.next_deadline()
+
 
 class FundIndex(BasePage):
     subpage_types = ['FundPage']
diff --git a/opentech/public/funds/templates/public_funds/fund_page.html b/opentech/public/funds/templates/public_funds/fund_page.html
index 7c6f6f516..56f758dd6 100644
--- a/opentech/public/funds/templates/public_funds/fund_page.html
+++ b/opentech/public/funds/templates/public_funds/fund_page.html
@@ -2,7 +2,7 @@
 {% load wagtailcore_tags wagtailimages_tags navigation_tags static %}
 
 {% block content %}
-    {% include "public_funds/includes/fund_apply_cta.html" with fund_type=page.fund_type.specific %}
+    {% include "public_funds/includes/fund_apply_cta.html" with fund_page=page %}
     <div class="wrapper wrapper--flex">
         <section class="section section--main">
             <h1>{{ page.title }}</h1>
@@ -11,6 +11,6 @@
             {% include_block page.body %}
         </section>
     </div>
-    {% include "public_funds/includes/fund_apply_cta.html" with fund_type=page.fund_type.specific %}
+    {% include "public_funds/includes/fund_apply_cta.html" with fund_page=page %}
     {% include "includes/relatedcontent.html" with related_pages=page.related_pages.all %}
 {% endblock %}
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 1f81aa425..0fd7669e0 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,12 +2,12 @@
 
 <div class="wrapper wrapper--flex">
     <div class="section section--apply-cta">
-        {% if fund_type.deadline %}
+        {% if fund_page.deadline %}
             <div class="deadline">
-                {% trans "Next deadline" %}: {{ fund_type.deadline|date:"M j, Y" }}
+                {% trans "Next deadline" %}: {{ fund_page.deadline|date:"M j, Y" }}
             </div>
             <div class="apply-link">
-                <a class="button" href="{% pageurl fund_type %}">{% trans "Apply for this fund" %}</a>
+                <a class="button" href="{% pageurl fund_page.fund_type %}">{% trans "Apply for this fund" %}</a>
             </div>
         {% else %}
             <div class="deadline">
-- 
GitLab