Commit fd97cacb by Raphael Lullis

Removing all the Parse prefix name from classes (except for ParseBase and…

Removing all the Parse prefix name from classes (except for ParseBase and ParseResource, which are classes that are just base classes). Also, adding a very basic Push class.
parent 7228a08b
......@@ -66,6 +66,10 @@ class ParseBase(object):
def PUT(cls, uri, **kw):
return cls.execute(uri, 'PUT', **kw)
@classmethod
def DELETE(cls, uri, **kw):
return cls.execute(uri, 'DELETE', **kw)
@property
def _attributes(self):
# return "public" attributes converted to the base parse representation.
......@@ -87,7 +91,7 @@ class ParseBase(object):
def _convertToParseType(self, prop):
key, value = prop
if type(value) == ParseObject:
if type(value) == Object:
value = {'__type': 'Pointer',
'className': value._class_name,
'objectId': value._object_id}
......@@ -106,7 +110,32 @@ class ParseBase(object):
return (key, value)
class ParseUser(ParseBase):
class ParseResource(ParseBase):
def __init__(self, **kw):
self._object_id = kw.pop('objectId', None)
self._updated_at = kw.pop('updatedAt', None)
self._created_at = kw.pop('createdAt', None)
for attr, value in kw.items():
self.__dict__[attr] = value
@classmethod
def retrieve(cls, resource_id):
return cls(**cls.GET('/' + resource_id))
_absolute_url = property(lambda self: '/'.join([self.__class__.ENDPOINT_ROOT + self._object_id]))
def objectId(self):
return self._object_id
def updatedAt(self):
return (self._updated_at and self._ISO8601ToDatetime(self._updated_at) or None)
def createdAt(self):
return (self._created_at and self._ISO8601ToDatetime(self._created_at) or None)
class User(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'users'])
@classmethod
......@@ -121,21 +150,6 @@ class ParseUser(ParseBase):
def request_password_reset(cls, email):
return cls.POST('/'.join([API_ROOT, 'requestPasswordReset']), email=email)
@classmethod
def retrieve(cls, user_id):
return cls(**cls.GET('/' + user_id))
def __init__(self, **kw):
self._object_id = kw.pop('objectId', None)
self._updated_at = kw.pop('updatedAt', None)
self._created_at = kw.pop('createdAt', None)
for attr, value in kw.items():
self.__dict__[attr] = value
_absolute_url = property(lambda self: '/'.join([self.__class__.ENDPOINT_ROOT + self._object_id]))
def save(self, session=None):
session_header = {'X-Parse-Session-Token': session and session.get('sessionToken') }
......@@ -144,7 +158,11 @@ class ParseUser(ParseBase):
)
class ParseObject(ParseBase):
class Installation(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'installations'])
class Object(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'classes'])
def __init__(self, class_name, attrs_dict=None):
......@@ -156,17 +174,6 @@ class ParseObject(ParseBase):
if attrs_dict:
self._populateFromDict(attrs_dict)
def objectId(self):
return self._object_id
def updatedAt(self):
return (self._updated_at and self._ISO8601ToDatetime(self._updated_at)
or None)
def createdAt(self):
return (self._created_at and self._ISO8601ToDatetime(self._created_at)
or None)
def save(self):
if self._object_id:
self._update()
......@@ -177,10 +184,7 @@ class ParseObject(ParseBase):
# URL: /1/classes/<className>/<objectId>
# HTTP Verb: DELETE
uri = '/%s/%s' % (self._class_name, self._object_id)
self.__class__.execute(uri, 'DELETE')
self.__class__.DELETE('/%s/%s' % (self._class_name, self._object_id))
self = self.__init__(None)
def increment(self, key, amount=1):
......@@ -228,7 +232,7 @@ class ParseObject(ParseBase):
if type(value) == dict and '__type' in value:
if value['__type'] == 'Pointer':
value = ParseQuery(value['className']).get(value['objectId'])
value = Query(value['className']).get(value['objectId'])
elif value['__type'] == 'Date':
value = self._ISO8601ToDatetime(value['iso'])
elif value['__type'] == 'Bytes':
......@@ -241,14 +245,6 @@ class ParseObject(ParseBase):
return (key, value)
@property
def _attributes(self):
# return "public" attributes converted to the base parse representation.
return dict([
self._convertToParseType(p) for p in self.__dict__.items()
if p[0][0] != '_'
])
def _create(self):
# URL: /1/classes/<className>
# HTTP Verb: POST
......@@ -266,19 +262,25 @@ class ParseObject(ParseBase):
uri = '/%s/%s' % (self._class_name, self._object_id)
response_dict = self.execute(uri, 'PUT', **self._attributes)
response_dict = self.__class__.PUT(uri, **self._attributes)
self._updated_at = response_dict['updatedAt']
class Push(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'push'])
class ParseQuery(ParseBase):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'classes'])
@classmethod
def send(cls, message, channels=None, **kw):
alert_message = { 'alert': message }
targets = {}
if channels: targets['channels'] = channels
if kw: targets['where'] = kw
return cls.POST('', data=alert_message, **targets)
def __init__(self, class_name):
self._class_name = class_name
class Query(ParseBase):
def __init__(self):
self._where = collections.defaultdict(dict)
self._options = {}
self._object_id = ''
def eq(self, name, value):
self._where[name] = value
......@@ -320,14 +322,37 @@ class ParseQuery(ParseBase):
return self
def get(self, object_id):
self._object_id = object_id
return self._fetch(single_result=True)
return self.__class__.QUERY_CLASS.retrieve(object_id)
def fetch(self):
# hide the single_result param of the _fetch method from the library
# user since it's only useful internally
return self._fetch()
def _fetch(self):
options = dict(self._options) # make a local copy
if self._where:
# JSON encode WHERE values
where = json.dumps(self._where)
options.update({'where': where})
response = self.__class__.GET('', **options)
return [self.__class__.QUERY_CLASS(**result) for result in response['results']]
class ObjectQuery(Query):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'classes'])
def __init__(self, class_name):
self._class_name = class_name
self._where = collections.defaultdict(dict)
self._options = {}
self._object_id = ''
def get(self, object_id):
self._object_id = object_id
return self._fetch(single_result=True)
def _fetch(self, single_result=False):
# URL: /1/classes/<className>/<objectId>
# HTTP Verb: GET
......@@ -344,19 +369,19 @@ class ParseQuery(ParseBase):
response = self.__class__.GET('/%s' % self._class_name, **options)
if single_result:
return ParseObject(self._class_name, response)
return Object(self._class_name, response)
else:
return [ParseObject(self._class_name, result) for result in response['results']]
return [Object(self._class_name, result) for result in response['results']]
class ParseUserQuery(ParseQuery):
class UserQuery(Query):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'users'])
QUERY_CLASS = User
def __init__(self):
self._where = collections.defaultdict(dict)
self._options = {}
def get(self, object_id):
return ParseUser.retrieve(object_id)
class InstallationQuery(Query):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'installations'])
QUERY_CLASS = Installation
def _fetch(self):
options = dict(self._options) # make a local copy
......@@ -365,5 +390,6 @@ class ParseUserQuery(ParseQuery):
where = json.dumps(self._where)
options.update({'where': where})
response = self.__class__.GET('', **options)
return [ParseUser(**result) for result in response['results']]
extra_headers = {'X-Parse-Master-Key': MASTER_KEY }
response = self.__class__.GET('', extra_headers=extra_headers, **options)
return [self.__class__.QUERY_CLASS(**result) for result in response['results']]
......@@ -6,23 +6,23 @@ import unittest
import urllib2
import datetime
import __init__ as ParsePy
import __init__ as parse
try:
import settings_local
except ImportError:
raise ImportError("You must create a settings_local.py file " +
"with an example application to run tests")
raise ImportError(
'You must create a settings_local.py file with an example application to run tests'
)
ParsePy.APPLICATION_ID = settings_local.APPLICATION_ID
ParsePy.API_KEY = settings_local.API_KEY
parse.APPLICATION_ID = settings_local.APPLICATION_ID
parse.API_KEY = settings_local.API_KEY
### FUNCTIONS ###
def test_obj(saved=False):
"""Return a test ParsePy.ParseObject (content is from the docs)"""
ret = ParsePy.ParseObject("GameScore")
"""Return a test parse.Object (content is from the docs)"""
ret = parse.Object("GameScore")
ret.score = 1337
ret.playerName = "Sean Plott"
ret.cheatMode = False
......@@ -33,11 +33,10 @@ def test_obj(saved=False):
### CLASSES ###
class TestParseObjectAndQuery(unittest.TestCase):
class TestObjectAndQuery(unittest.TestCase):
"""
Tests for the ParsePy.ParseObject interface for creating and updating Parse
objects, as well as the ParsePy.ParseQuery interface for retrieving them
Tests for the parse.Object interface for creating and updating Parse
objects, as well as the parse.Query interface for retrieving them
"""
def check_test_obj(self, o):
......@@ -52,13 +51,13 @@ class TestParseObjectAndQuery(unittest.TestCase):
self.assertEqual(o.location, "POINT(-30.0 43.21)")
def test_object(self):
"""Test the creation, retrieval and updating of a ParseObject"""
"""Test the creation, retrieval and updating of a Object"""
gameScore = test_obj()
gameScore.save()
self.check_test_obj(gameScore)
# retrieve a new one
query = ParsePy.ParseQuery("GameScore")
query = parse.Query('GameScore')
obj1 = query.get(gameScore.objectId())
self.check_test_obj(obj1)
......@@ -95,7 +94,7 @@ class TestParseObjectAndQuery(unittest.TestCase):
o.increment("score")
self.assertEqual(o.score, 1338)
query = ParsePy.ParseQuery("GameScore")
query = parse.Query("GameScore")
o2 = query.get(o.objectId())
self.assertEqual(o2.score, 1338)
......@@ -107,12 +106,12 @@ class TestParseObjectAndQuery(unittest.TestCase):
def test_relationship(self):
"""Test relationship between objects"""
post = ParsePy.ParseObject("Post")
post = parse.Object("Post")
post.title = "I'm Hungry"
post.content = "Where should we go for lunch?"
post.save()
comment = ParsePy.ParseObject("Comment")
comment = parse.Object("Comment")
comment.content = "Let's do Sushirrito"
comment.parent = post
comment.save()
......@@ -124,8 +123,8 @@ class TestParseObjectAndQuery(unittest.TestCase):
self.assertEqual(comment_id.__class__, unicode)
# retrieve new ones
post2 = ParsePy.ParseQuery("Post").get(post_id)
comment2 = ParsePy.ParseQuery("Comment").get(comment_id)
post2 = parse.Query("Post").get(post_id)
comment2 = parse.Query("Comment").get(comment_id)
# check the relationship between the saved post and comment
self.assertEqual(comment2.parent.objectId(), post_id)
self.assertEqual(comment2.parent.title, "I'm Hungry")
......@@ -135,11 +134,10 @@ class TestParseObjectAndQuery(unittest.TestCase):
o = test_obj(True)
obj_id = o.objectId()
self.check_test_obj(o)
o2 = ParsePy.ParseQuery("GameScore").get(obj_id)
o2 = parse.Query("GameScore").get(obj_id)
self.check_test_obj(o2)
o2.delete()
self.assertRaises(urllib2.HTTPError,
ParsePy.ParseQuery("GameScore").get, obj_id)
self.assertRaises(urllib2.HTTPError, parse.Query("GameScore").get, obj_id)
if __name__ == "__main__":
......
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