diff --git a/opentech/public/people/models.py b/opentech/public/people/models.py index 04ca3f7e421fe7380951e0e72634b90ed310f7b5..b6ca87aa2fdab1876249b54377940c913295523e 100644 --- a/opentech/public/people/models.py +++ b/opentech/public/people/models.py @@ -13,13 +13,12 @@ from wagtail.wagtailadmin.edit_handlers import ( FieldRowPanel, InlinePanel, MultiFieldPanel, - PageChooserPanel, StreamFieldPanel ) from wagtail.wagtailimages.edit_handlers import ImageChooserPanel from opentech.public.utils.blocks import StoryBlock -from opentech.public.utils.models import BasePage +from opentech.public.utils.models import BasePage, BaseFunding, FundingMixin class SocialMediaProfile(models.Model): @@ -72,25 +71,8 @@ class PersonPagePersonType(models.Model): return self.person_type.title -class Funding(Orderable): +class Funding(BaseFunding): page = ParentalKey('PersonPage', related_name='funding') - value = models.PositiveIntegerField() - year = models.PositiveIntegerField() - duration = models.PositiveIntegerField(help_text='In months') - source = models.ForeignKey( - 'wagtailcore.Page', - on_delete=models.PROTECT, - ) - - panels = [ - FieldRowPanel([ - FieldPanel('year'), - FieldPanel('value'), - FieldPanel('duration'), - ]), - # This is stubbed as we need to be able to select from multiple - PageChooserPanel('source'), - ] class PersonContactInfomation(Orderable): @@ -132,7 +114,7 @@ class PersonContactInfomation(Orderable): }) -class PersonPage(BasePage): +class PersonPage(FundingMixin, BasePage): subpage_types = [] parent_page_types = ['PersonIndexPage'] @@ -170,10 +152,6 @@ 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 342ddc2c740fc513267e09c000db9509b17c6699..93755fa587890b773100b18d4767a639ca502d69 100644 --- a/opentech/public/people/templates/people/person_page.html +++ b/opentech/public/people/templates/people/person_page.html @@ -52,18 +52,8 @@ <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> + + {% include "utils/includes/funding.html" %} </div> </section> diff --git a/opentech/public/projects/migrations/0002_projectfunding.py b/opentech/public/projects/migrations/0002_projectfunding.py new file mode 100644 index 0000000000000000000000000000000000000000..56cd4bf2ad24097246620096a8668e089ae69700 --- /dev/null +++ b/opentech/public/projects/migrations/0002_projectfunding.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2018-01-15 17:12 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import modelcluster.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailcore', '0040_page_draft_title'), + ('projects', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='ProjectFunding', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sort_order', models.IntegerField(blank=True, editable=False, null=True)), + ('value', models.PositiveIntegerField()), + ('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='projects.ProjectPage')), + ('source', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='wagtailcore.Page')), + ], + options={ + 'ordering': ['sort_order'], + 'abstract': False, + }, + ), + ] diff --git a/opentech/public/projects/models.py b/opentech/public/projects/models.py index 3535dc2cb6a634cc57793cff47100fd5473b017c..b74a31b8c5c48ebeb6a79893005c34c77bd2136d 100644 --- a/opentech/public/projects/models.py +++ b/opentech/public/projects/models.py @@ -18,7 +18,9 @@ from wagtail.wagtailsearch import index from opentech.public.utils.blocks import StoryBlock from opentech.public.utils.models import ( + BaseFunding, BasePage, + FundingMixin, RelatedPage, ) @@ -76,7 +78,11 @@ class ProjectPageRelatedPage(RelatedPage): ] -class ProjectPage(BasePage): +class ProjectFunding(BaseFunding): + page = ParentalKey('ProjectPage', related_name='funding') + + +class ProjectPage(FundingMixin, BasePage): subpage_types = [] parent_page_types = ['ProjectIndexPage'] @@ -93,7 +99,6 @@ class ProjectPage(BasePage): # Fields to add: # otf_status # status - # funding search_fields = BasePage.search_fields + [ index.SearchField('introduction'), @@ -106,6 +111,7 @@ class ProjectPage(BasePage): StreamFieldPanel('body'), InlinePanel('contact_details', label="Contact Details"), InlinePanel('related_pages', label="Related pages"), + InlinePanel('funding', label="Funding"), ] diff --git a/opentech/public/projects/templates/projects/project_page.html b/opentech/public/projects/templates/projects/project_page.html index a6740d230d749e74e899a4a7ab1f2a88a4f8099a..bb064432c763c887a6f7ffa4609787ef1de02c1d 100644 --- a/opentech/public/projects/templates/projects/project_page.html +++ b/opentech/public/projects/templates/projects/project_page.html @@ -29,5 +29,6 @@ </div> </div> + {% include "utils/includes/funding.html" %} {% endblock content %} diff --git a/opentech/public/utils/models.py b/opentech/public/utils/models.py index 08d72f3bbfe463094b0e4eb5ad085cfcf498578b..d247017450ba03e773efcd0f41d796f529d7eceb 100644 --- a/opentech/public/utils/models.py +++ b/opentech/public/utils/models.py @@ -3,6 +3,7 @@ from django.db import models from wagtail.wagtailadmin.edit_handlers import ( FieldPanel, + FieldRowPanel, MultiFieldPanel, PageChooserPanel, StreamFieldPanel, @@ -259,3 +260,40 @@ class BasePage(SocialFields, ListingFields, Page): SocialFields.promote_panels + ListingFields.promote_panels ) + + +class BaseFunding(Orderable): + value = models.PositiveIntegerField() + year = models.PositiveIntegerField() + duration = models.PositiveIntegerField(help_text='In months') + source = models.ForeignKey( + 'wagtailcore.Page', + on_delete=models.PROTECT, + ) + + panels = [ + FieldRowPanel([ + FieldPanel('year'), + FieldPanel('value'), + FieldPanel('duration'), + ]), + # This is stubbed as we need to be able to select from multiple + PageChooserPanel('source'), + ] + + class Meta(Orderable.Meta): + abstract = True + + +class FundingMixin(models.Model): + '''Impliments the funding total calulation + + You still need to include the InlinePanel('funding') in the child class + ''' + + class Meta: + abstract = True + + @property + def total_funding(self): + return sum(funding.value for funding in self.funding.all()) diff --git a/opentech/public/utils/templates/utils/includes/funding.html b/opentech/public/utils/templates/utils/includes/funding.html new file mode 100644 index 0000000000000000000000000000000000000000..d858ce5dcc11d072858f56f0674dc61fdb9e4719 --- /dev/null +++ b/opentech/public/utils/templates/utils/includes/funding.html @@ -0,0 +1,13 @@ +{% load wagtailcore_tags %} +<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>