Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields will not read what it writes #60

Closed
wants to merge 4 commits into from

Conversation

nparley
Copy link
Contributor

@nparley nparley commented May 29, 2015

When to_representation is run on a geo object json.load adds unicode strings so the result is something like:

{u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]}

this is the string that rest_framework puts into the web form for any PUTs to edit the field. Therefore the PUT will send the string:

"{u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]}"

which will not work with GEOSGeometry as it does not expect the u symbols. The way around this is to eval the string as a python dictionary and then dump the json. This creates the string:

'{"type": "Point", "coordinates": [39.921092168090915, 28.04062843322754]}'

which will then work.

When to_representation is run on a geo object json.load adds unicode strings so the result is something like:

```
{u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]}
```

this is the string that rest_framework puts into the web form for any PUTs to edit the field. Therefore the PUT will send the string:

```
"{u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]}"
```

which will not work with GEOSGeometry as it does not expect the u symbols. The way around this is to eval the string as a python dictionary and then dump the json. This creates the string:

```
'{"type": "Point", "coordinates": [39.921092168090915, 28.04062843322754]}'
```

which will then work.
@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 3ad8048 on nparley:patch-1 into de69a7a on djangonauts:master.

@nparley
Copy link
Contributor Author

nparley commented May 29, 2015

The other maybe cleaner option is to create a JSONDict class that overwrites the str for dict to use json.dumps, i.e.

class JSONDict(dict):
    def __str__(self):
        return json.dumps(self)

Then returning: JSONDict(json.loads(GEOSGeometry(value).geojson)) from to_representation makes the string:

'{"type": "Point", "coordinates": [39.921092168090915, 28.04062843322754]}' 

Return dictionary with correct json __str__ value
@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 114c734 on nparley:patch-1 into de69a7a on djangonauts:master.

Only update the __str__ of the returned dict
@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 0227517 on nparley:patch-1 into de69a7a on djangonauts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 95.62% when pulling 0227517 on nparley:patch-1 into de69a7a on djangonauts:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 95.62% when pulling 0227517 on nparley:patch-1 into de69a7a on djangonauts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 95.62% when pulling 0227517 on nparley:patch-1 into de69a7a on djangonauts:master.

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 0227517 on nparley:patch-1 into 4a7b829 on djangonauts:master.

@nemesifier
Copy link
Member

If the solution was ok, it would have been simpler to just return GEOSGeometry(value).geojson.

Unfortunately this affects the JSON response too, in which geojson objects become strings.

This is a known issue unfortunately.
See here:
https://github.com/djangonauts/django-rest-framework-gis/blob/master/tests/django_restframework_gis_tests/tests.py#L441-L444

@nemesifier
Copy link
Member

well actually I was wrong, your solution works.

@nemesifier
Copy link
Member

@nparley could you squash your 4 commits into one commit with a meaningful commit message please?

nemesifier pushed a commit that referenced this pull request Jun 27, 2015
Valid for python 2 only.
Avoid representations like {u'type': u'Point', u'coordinates':
[39.921092168090915, 28.04062843322754]}
For more information see #60
nemesifier added a commit that referenced this pull request Jun 27, 2015
* reintroduced disabled test
* avoid double rendering of geojson string
* added TODO hint for removing this code when python 2 will be deprecated
@nemesifier
Copy link
Member

no worries @nparley i've done it for you and merged manually (0dda528), thank you for collaborating.

@nemesifier nemesifier closed this Jun 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants