Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

ComputedProperty support #12

Merged
merged 3 commits into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions graphene_gae/ndb/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
def convert_ndb_scalar_property(graphene_type, ndb_prop):
description = "%s %s property" % (ndb_prop._name, graphene_type)
if ndb_prop._repeated:
return graphene_type(description=description).List
l = graphene_type(description=description).List
return l if not ndb_prop._required else l.NonNull

if ndb_prop._required:
return graphene_type(description=description).NonNull

return graphene_type(description=description)

Expand Down Expand Up @@ -82,6 +86,10 @@ def convert_local_structured_property(ndb_structured_prop, meta):
return ConversionResult(name=name, field=Field(t))


def convert_computed_property(ndb_computed_prop, meta):
return convert_ndb_scalar_property(String, ndb_computed_prop)


converters = {
ndb.StringProperty: convert_ndb_string_property,
ndb.TextProperty: convert_ndb_string_property,
Expand All @@ -92,7 +100,8 @@ def convert_local_structured_property(ndb_structured_prop, meta):
ndb.DateProperty: convert_ndb_datetime_property,
ndb.DateTimeProperty: convert_ndb_datetime_property,
ndb.KeyProperty: convert_ndb_key_propety,
ndb.LocalStructuredProperty: convert_local_structured_property
ndb.LocalStructuredProperty: convert_local_structured_property,
ndb.ComputedProperty: convert_computed_property
}


Expand Down
8 changes: 7 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class Comment(ndb.Model):


class Article(ndb.Model):
headline = ndb.StringProperty()
headline = ndb.StringProperty(required=True)
summary = ndb.StringProperty()
body = ndb.TextProperty()
body_hash = ndb.ComputedProperty(lambda self: self.calc_body_hash())
keywords = ndb.StringProperty(repeated=True)

author_key = ndb.KeyProperty(kind='Author')
tags = ndb.KeyProperty(Tag, repeated=True)

created_at = ndb.DateTimeProperty(auto_now_add=True)
updated_at = ndb.DateTimeProperty(auto_now=True)

def calc_body_hash(self):
import hashlib
return hashlib.md5(self.body).hexdigit() if self.body else None