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

Commit d20c5f8

Browse files
committed
Merge pull request #12 from graphql-python/feature/support_computed_properties
ComputedProperty support
2 parents 6c4cb3c + b3b6f45 commit d20c5f8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

graphene_gae/ndb/converter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
def convert_ndb_scalar_property(graphene_type, ndb_prop):
2121
description = "%s %s property" % (ndb_prop._name, graphene_type)
2222
if ndb_prop._repeated:
23-
return graphene_type(description=description).List
23+
l = graphene_type(description=description).List
24+
return l if not ndb_prop._required else l.NonNull
25+
26+
if ndb_prop._required:
27+
return graphene_type(description=description).NonNull
2428

2529
return graphene_type(description=description)
2630

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

8488

89+
def convert_computed_property(ndb_computed_prop, meta):
90+
return convert_ndb_scalar_property(String, ndb_computed_prop)
91+
92+
8593
converters = {
8694
ndb.StringProperty: convert_ndb_string_property,
8795
ndb.TextProperty: convert_ndb_string_property,
@@ -92,7 +100,8 @@ def convert_local_structured_property(ndb_structured_prop, meta):
92100
ndb.DateProperty: convert_ndb_datetime_property,
93101
ndb.DateTimeProperty: convert_ndb_datetime_property,
94102
ndb.KeyProperty: convert_ndb_key_propety,
95-
ndb.LocalStructuredProperty: convert_local_structured_property
103+
ndb.LocalStructuredProperty: convert_local_structured_property,
104+
ndb.ComputedProperty: convert_computed_property
96105
}
97106

98107

tests/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ class Comment(ndb.Model):
3232

3333

3434
class Article(ndb.Model):
35-
headline = ndb.StringProperty()
35+
headline = ndb.StringProperty(required=True)
3636
summary = ndb.StringProperty()
3737
body = ndb.TextProperty()
38+
body_hash = ndb.ComputedProperty(lambda self: self.calc_body_hash())
3839
keywords = ndb.StringProperty(repeated=True)
3940

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

4344
created_at = ndb.DateTimeProperty(auto_now_add=True)
4445
updated_at = ndb.DateTimeProperty(auto_now=True)
46+
47+
def calc_body_hash(self):
48+
import hashlib
49+
return hashlib.md5(self.body).hexdigit() if self.body else None
50+

0 commit comments

Comments
 (0)