From e9caa3eb0155448fa158ebc24805e6dadbd2e46f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mu=C3=B1oz=20C=C3=A1rdenas?= Date: Fri, 7 Sep 2018 15:50:28 +0200 Subject: [PATCH] Revert "Remove unnecessary address field from workflowlevel2" --- .../migrations/0029_auto_20180831_0452.py | 16 ++++----- .../migrations/0030_auto_20180907_0704.py | 36 +++++++++++++++++++ workflow/models.py | 23 ++++++++++-- workflow/tests/test_models.py | 28 +++++++++++++++ 4 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 workflow/migrations/0030_auto_20180907_0704.py diff --git a/workflow/migrations/0029_auto_20180831_0452.py b/workflow/migrations/0029_auto_20180831_0452.py index acba69743..710ddf278 100644 --- a/workflow/migrations/0029_auto_20180831_0452.py +++ b/workflow/migrations/0029_auto_20180831_0452.py @@ -12,12 +12,12 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RemoveField( - model_name='historicalworkflowlevel2', - name='address', - ), - migrations.RemoveField( - model_name='workflowlevel2', - name='address', - ), + #migrations.RemoveField( + # model_name='historicalworkflowlevel2', + # name='address', + #), + #migrations.RemoveField( + # model_name='workflowlevel2', + # name='address', + #), ] diff --git a/workflow/migrations/0030_auto_20180907_0704.py b/workflow/migrations/0030_auto_20180907_0704.py new file mode 100644 index 000000000..aace844a7 --- /dev/null +++ b/workflow/migrations/0030_auto_20180907_0704.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.3 on 2018-09-07 14:04 +from __future__ import unicode_literals + +import django.contrib.postgres.fields.hstore +from django.db import migrations +from django.db.utils import ProgrammingError + + +def create_fields(apps, schema_editor): + # If fields are not there, add them. If they were added before, then + # ignore the error. + try: + migrations.AddField( + model_name='historicalworkflowlevel2', + name='address', + field=django.contrib.postgres.fields.hstore.HStoreField(blank=True, help_text='Address object with the structure: street (string), house_number (string), postal_code: (string), city (string), country (string)', null=True), + ) + migrations.AddField( + model_name='workflowlevel2', + name='address', + field=django.contrib.postgres.fields.hstore.HStoreField(blank=True, help_text='Address object with the structure: street (string), house_number (string), postal_code: (string), city (string), country (string)', null=True), + ) + except ProgrammingError: + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('workflow', '0029_auto_20180831_0452'), + ] + + operations = [ + migrations.RunPython(create_fields), + ] diff --git a/workflow/models.py b/workflow/models.py index f627ad160..a4ef04ac4 100755 --- a/workflow/models.py +++ b/workflow/models.py @@ -6,7 +6,7 @@ from django.conf import settings from django.contrib.postgres import fields from django.contrib.auth.models import User, Group -from django.contrib.postgres.fields import JSONField +from django.contrib.postgres.fields import HStoreField, JSONField from django.contrib.sites.models import Site from django.core.exceptions import ValidationError from django.db.models import Q @@ -15,7 +15,7 @@ except ImportError: from datetime import datetime as timezone from simple_history.models import HistoricalRecords -from voluptuous import Schema, Required +from voluptuous import Schema, All, Any, Length, Required from search.utils import ElasticsearchIndexer @@ -1079,6 +1079,7 @@ class WorkflowLevel2(models.Model): actual_end_date = models.DateTimeField(blank=True, null=True) actual_duration = models.CharField(max_length=255, blank=True, null=True) actual_cost = models.DecimalField("Actual Cost", decimal_places=2, max_digits=20, default=Decimal("0.00"), blank=True, help_text="Cost to date calculated from Budget Module") + address = HStoreField(blank=True, null=True, help_text="Address object with the structure: street (string), house_number (string), postal_code: (string), city (string), country (string)") capacity_built = models.TextField("Describe how sustainability was ensured for this project?", max_length=755, blank=True, null=True, help_text="Descriptive, did this help increases internal or external capacity") description = models.TextField("Description", blank=True, null=True, help_text="Description of the overall effort") description_of_community_involvement = models.TextField(blank=True, null=True, help_text="Descriptive, what community orgs are groups are involved") @@ -1163,6 +1164,24 @@ class Meta: ("can_approve", "Can approve initiation"), ) + def _validate_address(self, address): + schema = Schema({ + 'street': All(Any(str, unicode), Length(max=100)), + 'house_number': All(Any(str, unicode), Length(max=20)), + 'postal_code': All(Any(str, unicode), Length(max=20)), + 'city': All(Any(str, unicode), Length(max=85)), + 'country': All(Any(str, unicode), Length(max=50)), + }) + schema(address) + + def clean_fields(self, exclude=None): + super(WorkflowLevel2, self).clean_fields(exclude=exclude) + if self.address: + try: + self._validate_address(self.address) + except Exception as error: + raise ValidationError(error) + def save(self, *args, **kwargs): if self.create_date is None: self.create_date = timezone.now() diff --git a/workflow/tests/test_models.py b/workflow/tests/test_models.py index 3ec9421d8..632820f0c 100644 --- a/workflow/tests/test_models.py +++ b/workflow/tests/test_models.py @@ -143,6 +143,34 @@ def test_print_instance_with_org(self): self.assertEqual(unicode(wflvl1), u'ACME ') +@tag('pkg') +class WorkflowLevel2Test(TestCase): + def test_print_instance(self): + wflvl2 = factories.WorkflowLevel2.build() + self.assertEqual(unicode(wflvl2), u'Help Syrians') + + def test_save_address_fail(self): + wflvl2 = factories.WorkflowLevel2() + wflvl2.address = { + 'street': None, + } + self.assertRaises(ValidationError, wflvl2.save) + + wflvl2.address = { + 'house_number': 'a'*21, + } + self.assertRaises(ValidationError, wflvl2.save) + + def test_save_address(self): + factories.WorkflowLevel2(address={ + 'street': 'Oderberger Straße', + 'house_number': '16A', + 'postal_code': '10435', + 'city': 'Berlin', + 'country': 'Germany', + }) + + @tag('pkg') class OrganizationTest(TestCase): def test_print_instance(self):