Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Improving how to handle with foreign keys at GAEModel
Browse files Browse the repository at this point in the history
  • Loading branch information
felipevolpone committed Jun 27, 2016
1 parent 743edbb commit f454c60
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 1 addition & 2 deletions examples/gae-example/src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from ray_appengine.all import GAEModel
from google.appengine.ext import ndb
from google.appengine.ext.ndb import Model


RaySettings.ENDPOINT_MODULES.append('src.models')
Expand All @@ -31,5 +30,5 @@ class Post(GAEModel):
@endpoint('/comment')
class Comments(GAEModel):
title = ndb.StringProperty(required=False)
textg = ndb.StringProperty(required=True)
text = ndb.StringProperty(required=True)
post_key = ndb.KeyProperty(kind=Post)
21 changes: 20 additions & 1 deletion ray-appengine/ray_appengine/all.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from ray.model import Model
from google.appengine.ext.ndb import Model as AppEngineModel
from google.appengine.ext import ndb


class GAEModel(AppEngineModel, Model):
Expand All @@ -9,6 +10,24 @@ class GAEModel(AppEngineModel, Model):
def columns(cls):
return sorted(cls._properties.keys())

@classmethod
def to_instance(cls, json_attributes):
# instance = cls(**json_attributes)
instance = cls()

keys = {}
for name, property_type in instance._properties.items():
if isinstance(property_type, ndb.KeyProperty):
keys[name] = property_type._kind

for field_name in json_attributes.keys():
if field_name in keys:
setattr(instance, field_name, ndb.Key(keys[field_name], json_attributes[field_name]))
else:
setattr(instance, field_name, json_attributes[field_name])

return instance

def put(self):
can_save = Model.put(self)
if can_save:
Expand All @@ -25,7 +44,7 @@ def to_json(self):
r = self.to_dict()
if self.key:
r['id'] = self.key.id()

return r

@classmethod
Expand Down
16 changes: 16 additions & 0 deletions ray-appengine/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class User(GAEModel):
age = ndb.IntegerProperty()


@endpoint('/post')
class Post(GAEModel):
title = ndb.StringProperty(required=False)
text = ndb.StringProperty(required=True)
owner = ndb.KeyProperty(kind=User)


class TestIntegrated(TestCreateEnviroment):

def test_columns(self):
Expand All @@ -25,6 +32,15 @@ def test_put(self):
all_users = User.query().fetch()
self.assertEqual(1, len(all_users))

def test_put_foreign_key(self):
owner = User(name='john', age=25).put()
new_post = Post.to_instance({'text': 'any', 'owner': owner.to_json()['id']})
self.assertTrue(new_post.put())

posts = Post.query().fetch()
self.assertEqual(1, len(posts))
self.assertEqual(owner.to_json()['id'], posts[0].owner.id())

def test_to_json(self):
u = User(name='felipe', age=33).put()
expected = {'name': 'felipe', 'age': 33, 'id': 1}
Expand Down

0 comments on commit f454c60

Please sign in to comment.