Commit 04a13ff7 by Roman Krejcik

Object.factory returns user defined types if exists. Also returns same type for…

Object.factory returns user defined types if exists. Also returns same type for two calls with same arguments.
parent 859fd3e9
......@@ -310,12 +310,19 @@ class Object(six.with_metaclass(ObjectMetaclass, ParseResource)):
@classmethod
def factory(cls, class_name):
class DerivedClass(cls):
pass
DerivedClass.__name__ = str(class_name)
DerivedClass.set_endpoint_root()
return DerivedClass
"""find proper Object subclass matching class_name
system types like _User are mapped to types without underscore (parse_resr.user.User)
If user don't declare matching type, class is created on the fly
"""
class_name = class_name.lstrip('_')
types = cls.__subclasses__()
while types:
t = types.pop()
if t.__name__ == class_name:
return t
types.extend(t.__subclasses__())
else:
return type(class_name, (Object,), {})
@classmethod
def set_endpoint_root(cls):
......
......@@ -90,6 +90,10 @@ class TestObject(unittest.TestCase):
def testCanInstantiateParseType(self):
self.assertEqual(self.sao_paulo.location.latitude, -23.5)
def testFactory(self):
self.assertEqual(Object.factory('_User'), User)
self.assertEqual(Object.factory('GameScore'), GameScore)
def testCanSaveDates(self):
now = datetime.datetime.now()
self.score.last_played = now
......@@ -138,7 +142,7 @@ class TestObject(unittest.TestCase):
# get the object, see if it has saved
qs = GameScore.Query.get(objectId=self.score.objectId)
self.assertIsInstance(qs.item, Object, "Associated CollectedItem is not an object")
self.assertIsInstance(qs.item, CollectedItem)
self.assertEqual(qs.item.type, "Sword", "Associated CollectedItem does not have correct attributes")
def testBatch(self):
......
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