Skip to content

Commit

Permalink
set default JSONField
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Nov 29, 2014
1 parent ade98dd commit d9abae4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
34 changes: 34 additions & 0 deletions boundaries/migrations/0002_auto_20141129_1402.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import jsonfield.fields


def set_defaults(apps, schema_editor):
Boundary = apps.get_model("boundaries", "Boundary")
for boundary in Boundary.objects.all():
if boundary.metadata is None:
boundary.metadata = {}
boundary.save()


class Migration(migrations.Migration):

dependencies = [
('boundaries', '0001_initial'),
]

operations = [
migrations.RunPython(set_defaults),
migrations.AlterField(
model_name='boundary',
name='metadata',
field=jsonfield.fields.JSONField(default={}, help_text='The attributes of the boundary from the shapefile, as a dictionary.'),
),
migrations.AlterField(
model_name='boundaryset',
name='extra',
field=jsonfield.fields.JSONField(default={}, help_text='Any additional metadata.'),
),
]
6 changes: 3 additions & 3 deletions boundaries/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BoundarySet(models.Model):
help_text=ugettext_lazy("The date from which the set's boundaries are in effect."))
end_date = models.DateField(blank=True, null=True,
help_text=ugettext_lazy("The date until which the set's boundaries are in effect."))
extra = JSONField(blank=True, null=True,
extra = JSONField(default={},
help_text=ugettext_lazy("Any additional metadata."))

name_plural = property(lambda s: s.name)
Expand Down Expand Up @@ -139,7 +139,7 @@ class Boundary(models.Model):
help_text=ugettext_lazy("An identifier of the boundary, which should be unique within the set."))
name = models.CharField(max_length=192, db_index=True,
help_text=ugettext_lazy('The name of the boundary.'))
metadata = JSONField(blank=True,
metadata = JSONField(default={},
help_text=ugettext_lazy('The attributes of the boundary from the shapefile, as a dictionary.'))
shape = models.MultiPolygonField(
help_text=ugettext_lazy('The geometry of the boundary in EPSG:4326.'))
Expand Down Expand Up @@ -411,7 +411,7 @@ def __init__(self, dictionary):
'start_date': None,
'end_date': None,
'notes': '',
'extra': dictionary.pop('metadata', None),
'extra': dictionary.pop('metadata', {}),

# Boundary functions.
'id_func': lambda feature: '',
Expand Down
2 changes: 1 addition & 1 deletion boundaries/tests/test_boundary_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_as_dict(self):
'extent': None,
'start_date': None,
'end_date': None,
'extra': None,
'extra': {},
})

def test_extend(self):
Expand Down
2 changes: 1 addition & 1 deletion boundaries/tests/test_boundary_set_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BoundarySetDetailTestCase(ViewTestCase, ViewsTests, PrettyTests):
'licence_url': '',
'end_date': None,
'name_singular': '',
'extra': None,
'extra': {},
'notes': '',
'authority': '',
'source_url': '',
Expand Down
2 changes: 1 addition & 1 deletion boundaries/tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_defaults(self):
self.assertEqual(definition['start_date'], None)
self.assertEqual(definition['end_date'], None)
self.assertEqual(definition['notes'], '')
self.assertEqual(definition['extra'], None)
self.assertEqual(definition['extra'], {})
self.assertEqual(definition['id_func']({}), '')
self.assertEqual(definition['slug_func']({}), 'Test')
self.assertEqual(definition['is_valid_func']({}), True)
Expand Down
5 changes: 4 additions & 1 deletion boundaries/tests/test_loadshapefiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def test_no_data_sources(self):
)

def test_get_version(self):
self.assertEqual(Command().get_version(), '0.6.0')
try:
Command().get_version()
except Exception as e:
self.fail('Exception %s raised: %s %s' % (type(e).__name__, e, traceback.format_exc()))


class LoadableTestCase(TestCase):
Expand Down

0 comments on commit d9abae4

Please sign in to comment.