我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用google.appengine.ext.ndb.StringProperty()。
def query_purchase_with_customer_key(): # [START purchase_with_customer_key_models] class Customer(ndb.Model): name = ndb.StringProperty() class Purchase(ndb.Model): customer = ndb.KeyProperty(kind=Customer) price = ndb.IntegerProperty() # [END purchase_with_customer_key_models] def query_purchases_for_customer_via_key(customer_entity): purchases = Purchase.query( Purchase.customer == customer_entity.key).fetch() return purchases return Customer, Purchase, query_purchases_for_customer_via_key
def query_purchase_with_ancestor_key(): # [START purchase_with_ancestor_key_models] class Customer(ndb.Model): name = ndb.StringProperty() class Purchase(ndb.Model): price = ndb.IntegerProperty() # [END purchase_with_ancestor_key_models] def create_purchase_for_customer_with_ancestor(customer_entity): purchase = Purchase(parent=customer_entity.key) return purchase def query_for_purchases_of_customer_with_ancestor(customer_entity): purchases = Purchase.query(ancestor=customer_entity.key).fetch() return purchases return (Customer, Purchase, create_purchase_for_customer_with_ancestor, query_for_purchases_of_customer_with_ancestor)
def get_TextField(kwargs): """ Returns a ``TextField``, applying the ``ndb.StringProperty`` length limit of 500 bytes. """ kwargs['validators'].append(validators.length(max=500)) return f.TextField(**kwargs)
def convert_StringProperty(self, model, prop, kwargs): """Returns a form field for a ``ndb.StringProperty``.""" if prop._repeated: return StringListPropertyField(**kwargs) kwargs['validators'].append(validators.length(max=500)) return get_TextField(kwargs)
def get_TextField(kwargs): """ Returns a ``TextField``, applying the ``ndb.StringProperty`` length limit of 500 bytes. """ kwargs['validators'].append(validators.length(max=500)) return f.StringField(**kwargs)
def convert_StringProperty(self, model, prop, kwargs): """Returns a form field for a ``ndb.StringProperty``.""" if prop._repeated: return StringListPropertyField(**kwargs) if prop._required: kwargs['validators'].append(validators.InputRequired()) # kwargs['validators'].append(validators.length(max=500)) return get_TextField(kwargs)
def testNoneResult_raisesException(self, patch_convert): from graphene_gae.ndb.converter import convert_ndb_property patch_convert.get.return_value = lambda *_: None with self.assertRaises(Exception) as context: prop = ndb.StringProperty() prop._code_name = "my_prop" convert_ndb_property(prop) expected_message = 'Failed to convert NDB propeerty to a GraphQL field my_prop (StringProperty())' self.assertTrue(expected_message in context.exception.message, msg=context.exception.message)
def testStringProperty_shouldConvertToString(self): self.__assert_conversion(ndb.StringProperty, graphene.String)
def testStringProperty_repeated_shouldConvertToList(self): ndb_prop = ndb.StringProperty(repeated=True) result = convert_ndb_property(ndb_prop) graphene_type = result.field._type self.assertIsInstance(graphene_type, graphene.List) self.assertEqual(graphene_type.of_type, graphene.String)
def testStringProperty_required_shouldConvertToList(self): ndb_prop = ndb.StringProperty(required=True) result = convert_ndb_property(ndb_prop) graphene_type = result.field._type self.assertIsInstance(graphene_type, graphene.NonNull) self.assertEqual(graphene_type.of_type, graphene.String)
def testKeyProperty_withSuffix(self): my_registry = Registry() class User(ndb.Model): name = ndb.StringProperty() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User') prop._code_name = 'user_key' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertEqual(_type._type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertEqual(_type._type, UserType)
def testKeyProperty_withSuffix_required(self): class User(ndb.Model): name = ndb.StringProperty() my_registry = Registry() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User', required=True) prop._code_name = 'user_key' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertIsInstance(_type._type, NonNull) self.assertEqual(_type._type.of_type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertIsInstance(_type._type, NonNull) self.assertEqual(_type._type.of_type, UserType)
def testKeyProperty_withoutSuffix(self): my_registry = Registry() class User(ndb.Model): name = ndb.StringProperty() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User') prop._code_name = 'user' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertEqual(_type._type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertEqual(_type._type, UserType)
def __init__(self, model_type, ancestor_type=None, keep_history=True): self.model_type = model_type self.ancestor_type = ancestor_type self.model_name = model_type.__name__ self.keep_history = keep_history if self.keep_history: history_props = { 'date': ndb.DateTimeProperty(auto_now_add=True), 'obj': ndb.StructuredProperty(model_type, repeated=False), 'client_id': ndb.StringProperty(), } self.history_model = type( self.model_name + 'History', (ndb.Model,), history_props)