Commit 10e3bb94 by David Robinson

Merge pull request #1 from billyto/master

geoPoints
parents 205a4af1 842c6c50
*.pyc
.DS_Store
settings_local.py
...@@ -86,6 +86,16 @@ We can store a reference to another ParseObject by assigning it to an attribute: ...@@ -86,6 +86,16 @@ We can store a reference to another ParseObject by assigning it to an attribute:
>>> gameScore.item = collectedItem >>> gameScore.item = collectedItem
~~~~~ ~~~~~
We can also store geoPoint dataTypes as attributes using the format <code>'POINT(latitude longitude)'</code>, with latitude and longitude as float values
~~~~~ {python}
>>> restaurant = ParsePy.ParseObject("Restaurant")
>>> restaurant.name = "Los Pollos Hermanos"
>>> restaurant.location ="POINT(12.0 -34.45)"
>>> restaurant.save()
~~~~~
Querying Querying
-------- --------
......
...@@ -17,6 +17,7 @@ import base64 ...@@ -17,6 +17,7 @@ import base64
import json import json
import datetime import datetime
import collections import collections
import re
API_ROOT = 'https://api.parse.com/1/classes' API_ROOT = 'https://api.parse.com/1/classes'
...@@ -117,6 +118,10 @@ class ParseObject(ParseBase): ...@@ -117,6 +118,10 @@ class ParseObject(ParseBase):
response_dict = self._executeCall(uri, 'GET') response_dict = self._executeCall(uri, 'GET')
self._populateFromDict(response_dict) self._populateFromDict(response_dict)
def _isGeoPoint(self,value):
if isinstance(value, str):
return re.search("\\bPOINT\\(\\b([-+]?\\d*\\.\\d+|\\d+) ([-+]?\\d*\\.\\d+|\\d+)\\)", value, re.I)
def _populateFromDict(self, attrs_dict): def _populateFromDict(self, attrs_dict):
if 'objectId' in attrs_dict: if 'objectId' in attrs_dict:
self._object_id = attrs_dict['objectId'] self._object_id = attrs_dict['objectId']
...@@ -145,6 +150,11 @@ class ParseObject(ParseBase): ...@@ -145,6 +150,11 @@ class ParseObject(ParseBase):
elif type(value) == ParseBinaryDataWrapper: elif type(value) == ParseBinaryDataWrapper:
value = {'__type': 'Bytes', value = {'__type': 'Bytes',
'base64': base64.b64encode(value)} 'base64': base64.b64encode(value)}
elif self._isGeoPoint(value):
coordinates=re.findall(r'[-+]?\d*\.\d+|\d+',value)
value= {'__type':'GeoPoint',
'latitude':float(coordinates[0]),
'longitude':float(coordinates[1])}
return (key, value) return (key, value)
...@@ -159,6 +169,8 @@ class ParseObject(ParseBase): ...@@ -159,6 +169,8 @@ class ParseObject(ParseBase):
elif value['__type'] == 'Bytes': elif value['__type'] == 'Bytes':
value = ParseBinaryDataWrapper(base64.b64decode( value = ParseBinaryDataWrapper(base64.b64decode(
value['base64'])) value['base64']))
elif value['__type'] == 'GeoPoint':
value = 'POINT(%s %s)'%(value['latitude'],value['longitude'])
else: else:
raise Exception('Invalid __type.') raise Exception('Invalid __type.')
......
...@@ -26,8 +26,9 @@ def test_obj(saved=False): ...@@ -26,8 +26,9 @@ def test_obj(saved=False):
ret.score = 1337 ret.score = 1337
ret.playerName = "Sean Plott" ret.playerName = "Sean Plott"
ret.cheatMode = False ret.cheatMode = False
ret.location = "POINT(30.0 -43.21)" #"POINT(30 -43.21)"
if saved: if saved:
ret.save() ret.save()
return ret return ret
...@@ -48,6 +49,7 @@ class TestParseObjectAndQuery(unittest.TestCase): ...@@ -48,6 +49,7 @@ class TestParseObjectAndQuery(unittest.TestCase):
# TODO: str vs unicode # TODO: str vs unicode
#self.assertEqual(o.playerName.__class__, unicode) #self.assertEqual(o.playerName.__class__, unicode)
self.assertEqual(o.cheatMode.__class__, bool) self.assertEqual(o.cheatMode.__class__, bool)
self.assertEqual(o.location,"POINT(30.0 -43.21)")
def test_object(self): def test_object(self):
"""Test the creation, retrieval and updating of a ParseObject""" """Test the creation, retrieval and updating of a ParseObject"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment