From 9db8d6fc1a3ee8d331f26d32762ab70e8985dec7 Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Wed, 29 Aug 2018 15:35:44 +0100 Subject: [PATCH] Allow more flexibility when adding funding. --- .../migrations/0007_allow_blank_source.py | 19 +++++++++++++ .../management/commands/migrate_projects.py | 27 +++++++++++++++---- .../migrations/0006_allow_blank_source.py | 19 +++++++++++++ opentech/public/utils/models.py | 2 ++ .../templates/utils/includes/funding.html | 12 ++++++--- opentech/settings/base.py | 1 + 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 opentech/public/people/migrations/0007_allow_blank_source.py create mode 100644 opentech/public/projects/migrations/0006_allow_blank_source.py diff --git a/opentech/public/people/migrations/0007_allow_blank_source.py b/opentech/public/people/migrations/0007_allow_blank_source.py new file mode 100644 index 000000000..a7491a136 --- /dev/null +++ b/opentech/public/people/migrations/0007_allow_blank_source.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.2 on 2018-08-29 14:27 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0006_fundreviewers'), + ] + + operations = [ + migrations.AlterField( + model_name='funding', + name='source', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='wagtailcore.Page'), + ), + ] diff --git a/opentech/public/projects/management/commands/migrate_projects.py b/opentech/public/projects/management/commands/migrate_projects.py index 5ebf9425a..1b446bbba 100644 --- a/opentech/public/projects/management/commands/migrate_projects.py +++ b/opentech/public/projects/management/commands/migrate_projects.py @@ -1,4 +1,5 @@ import argparse +import itertools import json import mimetypes @@ -172,12 +173,28 @@ class Command(BaseCommand): amounts = self.ensure_iterable(node['field_project_funding_amount']) durations = self.ensure_iterable(node['field_project_term_time']) funds = self.ensure_iterable(node['field_project_funding_request']) - for year, amount, duration, fund in zip(years, amounts, durations, funds): + for year, amount, duration, fund in itertools.zip_longest(years, amounts, durations, funds): + try: + fund = self.funds[fund['target_id']] + except TypeError: + fund = None + + try: + duration = duration['value'] + except TypeError: + duration = 0 + + try: + amount = amount['value'] + except TypeError: + # This is an error, don't give funding + continue + project.funding.add(ProjectFunding( - value=amount['value'], + value=amount, year=year['value'], - duration=duration['value'], - source=self.funds[fund['target_id']], + duration=duration, + source=fund, )) try: @@ -201,7 +218,7 @@ class Command(BaseCommand): pass try: return node[field]['value'] - except TypeError : + except TypeError: return '' def get_referenced_term(self, tid): diff --git a/opentech/public/projects/migrations/0006_allow_blank_source.py b/opentech/public/projects/migrations/0006_allow_blank_source.py new file mode 100644 index 000000000..ac212e957 --- /dev/null +++ b/opentech/public/projects/migrations/0006_allow_blank_source.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.2 on 2018-08-29 14:27 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('projects', '0005_projectpage_drupal_id'), + ] + + operations = [ + migrations.AlterField( + model_name='projectfunding', + name='source', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='wagtailcore.Page'), + ), + ] diff --git a/opentech/public/utils/models.py b/opentech/public/utils/models.py index 2c7fbb01c..559a4e7dd 100644 --- a/opentech/public/utils/models.py +++ b/opentech/public/utils/models.py @@ -270,6 +270,8 @@ class BaseFunding(Orderable): source = models.ForeignKey( 'wagtailcore.Page', on_delete=models.PROTECT, + null=True, + blank=True, ) panels = [ diff --git a/opentech/public/utils/templates/utils/includes/funding.html b/opentech/public/utils/templates/utils/includes/funding.html index c75d5f550..b6b13d124 100644 --- a/opentech/public/utils/templates/utils/includes/funding.html +++ b/opentech/public/utils/templates/utils/includes/funding.html @@ -1,15 +1,19 @@ -{% load wagtailcore_tags %} +{% load wagtailcore_tags humanize %} {% if page.total_funding %} <div class="fund-box {% if class %}{{class}}{% endif %}"> <h4>Funding to date</h4> {% for funding in page.funding.all %} <div class="fund-box__row"> <span>{{ funding.year }}</span> - <span>${{ funding.value }}</span> - <span>{{ funding.duration }} months</span> + <span>${{ funding.value|intcomma }}</span> + {% if funding.duration %} + <span>{{ funding.duration }} months</span> + {% endif %} </div> + {% if funding.source %} <a class="fund-box__link" href="{% pageurl funding.source %}">{{ funding.source }}</a> + {% endif %} {% endfor %} - <h4 class="heading heading--top-space heading--total-funding">Total Funding: <span>${{ page.total_funding }}</span></h4> + <h4 class="heading heading--top-space heading--total-funding">Total Funding: <span>${{ page.total_funding|intcomma }}</span></h4> </div> {% endif %} diff --git a/opentech/settings/base.py b/opentech/settings/base.py index cf6683fef..fb9ed8313 100644 --- a/opentech/settings/base.py +++ b/opentech/settings/base.py @@ -121,6 +121,7 @@ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', + 'django.contrib.humanize', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.postgres', -- GitLab