3

I am new to GAE and Endpoints Proto Datastore. I've been playing around with a project which has photos and each photo can have many comments. Using keys_with_ancestors as a guide, I've implemented it such that a comment has a photo as a parent and a photo has an alias property called "comments" which does an ancestor query. Here is my code:

Comment:

class Comment(EndpointsModel):    
_message_fields_schema = ('id', 'comment_text', 'owner', 'timestamp','parent')

_parent = None

comment_text = ndb.StringProperty(required=True)
owner = ndb.UserProperty(required=True)
timestamp = ndb.DateTimeProperty(auto_now_add=True)
parent_kind = ndb.StringProperty(required=True)

def SetKey(self):
    if self._parent is not None and self.id is not None:
        key = ndb.Key(self.parent_kind, self._parent, Comment, self.id)
    self.UpdateFromKey(key)

def ParentSet(self, value):    
    self._parent = value
    if ndb.Key(self.parent_kind, value).get() is None:
        raise endpoints.NotFoundException('Parent %s does not exist.' % value)
    if self.id is None: 
        self.key = ndb.Key(self.parent_kind, self._parent, Comment, None)
    else:
        self.SetKey()

    self._endpoints_query_info.ancestor = ndb.Key(self.parent_kind, value)

@EndpointsAliasProperty(setter=ParentSet,required=True,property_type= messages.IntegerField)
def parent(self):
    if self._parent is None and self.key is not None:
        self._parent = self.key.parent().integer_id()
    return self._parent

Photo:

class Photo(EndpointsModel):
DEFAULT_ORDER = '-timestamp'

_message_fields_schema = ('id', 'photo_link', 'description', 'owner','timestamp','comments')

photo_link = ndb.StringProperty(required=True)
description = ndb.StringProperty(required=True)
owner = ndb.UserProperty(required=True)
timestamp = ndb.DateTimeProperty(auto_now_add=True) 

@EndpointsAliasProperty(repeated=True, property_type=Comment.ProtoModel())
def comments(self):
    return Comment.query(ancestor=self.key).order(-Comment.timestamp).fetch()

And the API get request:

@Photo.method(request_fields=('id',),
             name='photo.get',
             http_method='GET',
             path='photo/{id}')
def get_photo(self,photo):
    if not photo.from_datastore:
        raise endpoints.NotFoundException('Photo not found.')
    return photo

First of all, is this even the best way to implement it? Secondly, I put a break point at the comments EndPointsAliasProperty and noticed that it gets called 3 times when the api GET request is made. Is this behavior normal or did I do something wrong? It seems like it could be problematic if a photo had lots of comments and they were getting queried 3 times for each request.

Drewness
  • 5,004
  • 4
  • 32
  • 50
2748
  • 123
  • 2
  • 5

0 Answers0