Skip to content

Commit

Permalink
Merge pull request #1143 from toladata/revert-1139-456-remove-additio…
Browse files Browse the repository at this point in the history
…nal-address-wfl2

Revert "Remove unnecessary address field from workflowlevel2"
  • Loading branch information
jefmoura authored Sep 10, 2018
2 parents c630608 + e9caa3e commit c29cc14
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
16 changes: 8 additions & 8 deletions workflow/migrations/0029_auto_20180831_0452.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
#),
]
36 changes: 36 additions & 0 deletions workflow/migrations/0030_auto_20180907_0704.py
Original file line number Diff line number Diff line change
@@ -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),
]
23 changes: 21 additions & 2 deletions workflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions workflow/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ def test_print_instance_with_org(self):
self.assertEqual(unicode(wflvl1), u'ACME <ACME Org>')


@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):
Expand Down

0 comments on commit c29cc14

Please sign in to comment.