Skip to content

Commit

Permalink
Fix gis_data serialization
Browse files Browse the repository at this point in the history
We recently changed the `ProjectSerializer` to use a `JsonField` serializer for
the `gis_data` field to reflect that fact that this data is now returned from
the geoprocessing service as an actual JSON object rather than a string
containing JSON data. Because this field serializer change was not also made to
the `ProjectUpdateSerializer` we ended up saving the string representation of a
Python dict into the database rather than a JSON string. This caused the model
to crash when attempting to load a previously saved project.

Adding a plain `JsonField()` to the `ProjectUpdateSerializer` caused test
failures that were resolved by adding attributes signifying that it is a
nullable field, which matches the underlying model. I changed both usages of
`JsonField` for consistency.

I tried to make `ProjectUpdateSerializer` inherit from `ProjectSerializer` but
it caused problems with scenario serialization.
  • Loading branch information
jwalgran committed Oct 3, 2017
1 parent 4b29a06 commit 57a7cca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import ast
import json

from django.db import migrations


def fix_gis_data_serialization(apps, schema_editor):
Project = apps.get_model('modeling', 'Project')
for project in Project.objects.filter(gis_data__startswith='{u'):
project.gis_data = json.dumps(ast.literal_eval(project.gis_data))
project.save()


class Migration(migrations.Migration):

dependencies = [
('modeling', '0022_project_wkaoi'),
]

operations = [
migrations.RunPython(fix_gis_data_serialization,
migrations.RunPython.noop)
]
3 changes: 2 additions & 1 deletion src/mmw/apps/modeling/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Meta:
geo_field = 'area_of_interest'

user = UserSerializer(default=serializers.CurrentUserDefault())
gis_data = JsonField()
gis_data = JsonField(required=False, allow_null=True)
scenarios = ScenarioSerializer(many=True, read_only=True)


Expand All @@ -61,3 +61,4 @@ class Meta:

user = UserSerializer(default=serializers.CurrentUserDefault(),
read_only=True)
gis_data = JsonField(required=False, allow_null=True)

0 comments on commit 57a7cca

Please sign in to comment.