From e56acb9e0b6c2a3aca41d415ffdfa83d986be680 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 11 Jan 2018 07:53:35 +0000
Subject: [PATCH] Add funding to the template

---
 opentech/public/people/migrations/0004_funding.py    |  2 +-
 opentech/public/people/models.py                     |  8 +++++---
 .../public/people/templates/people/person_page.html  | 12 ++++++++++++
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/opentech/public/people/migrations/0004_funding.py b/opentech/public/people/migrations/0004_funding.py
index 583eef7f8..55de7f989 100644
--- a/opentech/public/people/migrations/0004_funding.py
+++ b/opentech/public/people/migrations/0004_funding.py
@@ -24,7 +24,7 @@ class Migration(migrations.Migration):
                 ('year', models.PositiveIntegerField()),
                 ('duration', models.PositiveIntegerField(help_text='In months')),
                 ('page', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='funding', to='people.PersonPage')),
-                ('source', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='wagtailcore.Page')),
+                ('source', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='wagtailcore.Page')),
             ],
             options={
                 'ordering': ['sort_order'],
diff --git a/opentech/public/people/models.py b/opentech/public/people/models.py
index 3155811a2..04ca3f7e4 100644
--- a/opentech/public/people/models.py
+++ b/opentech/public/people/models.py
@@ -79,9 +79,7 @@ class Funding(Orderable):
     duration = models.PositiveIntegerField(help_text='In months')
     source = models.ForeignKey(
         'wagtailcore.Page',
-        blank=True,
-        null=True,
-        on_delete=models.SET_NULL
+        on_delete=models.PROTECT,
     )
 
     panels = [
@@ -172,6 +170,10 @@ class PersonPage(BasePage):
         InlinePanel('funding', label='Funding'),
     ]
 
+    @property
+    def total_funding(self):
+        return sum(funding.value for funding in self.funding.all())
+
 
 class PersonIndexPage(BasePage):
     subpage_types = ['PersonPage']
diff --git a/opentech/public/people/templates/people/person_page.html b/opentech/public/people/templates/people/person_page.html
index d06d83f2e..bdffcff4e 100644
--- a/opentech/public/people/templates/people/person_page.html
+++ b/opentech/public/people/templates/people/person_page.html
@@ -52,6 +52,18 @@
                 <h3>{{ item.get_service_display }}</h3>
                 <p>{{ item.profile_url }}</p>
             {% endfor %}
+            <h2>Funding to date</h2>
+            {% for funding in page.funding.all %}
+            <table>
+                <tr>
+                    <td>{{ funding.year }}</td>
+                    <td>${{ funding.value }}</td>
+                    <td>{{ funding.duration }} months</td>
+                    <td><a href="{% pageurl funding.source %}">{{ funding.source }}</a></td>
+                </tr>
+            </table>
+            {% endfor %}
+            <p>Total FUnding: {{ page.total_funding }}</p>
         </div>
     </section>
 
-- 
GitLab