def _normalize_name(name): """Helper used to convert a user entered name in to search compatible string. In order to make search as error free as possible, names of info records are converted to a simplified utf-8 encoded string that makes prefix searches easy. to make searching simpler, it removes all extra punctuation and spaces. Examples: _normalize_name('Duke Ellington') == 'duke ellington' _normalize_name(' Duke Ellington ') == 'duke ellington' _normalize_name('Duke-Ellington!') == 'duke ellington' _normalize_name('Duke_Ellington') == 'duke ellington' _normalize_name(u'Duk\xea Ellington') == 'Duk\xc3\xaa Ellington' Args: name: Name to convert to search string. Returns: Lower case, single space separated ByteString of name with punctuation removed. Unicode values are converted to UTF-8 encoded string. """ if name is None: return None elif isinstance(name, str): name = name.decode('utf-8') # Must explicitly replace '_' because the \w re part does not name = name.replace(u'_', u' ') names = _SEARCH_NAME_REGEX.findall(name) name = ' '.join(names) return db.ByteString(name.lower().encode('utf-8'))
class MyModel(db.Model): # p1 must be a short string; default is None; not required p1 = db.StringProperty() # p2 must be a short string; cannot be None p2 = db.StringProperty(required=True) # p3 must be a short string; set to 'a default value' if not initialized p3 = db.StringProperty(default='a default value') # p4 must be 'Winter', 'Spring', 'Summer' or 'Autumn'; implies required=True p4 = db.StringProperty(choices=['Winter', 'Spring', 'Summer', 'Autumn']) # p5 will not appear in any indexes and cannot be used in queries p5 = db.StringProperty(indexed=False) # p6 must be a long integer >= 1923, as defined in the function is_recent_year p6 = db.IntegerProperty(validator=is_recent_year) string_prop = db.StringProperty() text_prop = db.TextProperty() bytestring_prop = db.ByteString() blob_prop = db.BlobProperty() boolean_prop = db.BooleanProperty() integer_prop = db.IntegerProperty() float_prop = db.FloatProperty() date_prop = db.DateProperty() datetime_prop = db.DateTimeProperty() time_prop = db.TimeProperty() user_prop = db.UserProperty() # auto_now_prop is automatically set to datetime.datetime.now() # every time the object is created or loaded from the datastore. auto_now_prop = db.DateTimeProperty(auto_now=True) # auto_now_add_prop is automatically set to # datetime.datetime.now() when the object is first created. The # value is preserved for subsequent updates. auto_now_add_prop = db.DateTimeProperty(auto_now_add=True) # auto_current_user_prop is automatically set to # users.get_current_user() every time the object is created or # loaded from the datastore. (This is None if the user is not # signed in to Google Accounts.) auto_current_user_prop = db.UserProperty(auto_current_user=True) # auto_current_user_add_prop is automatically set to # users.get_current_user() when the object is first created. The # value is preserved for subsequent updates. auto_current_user_add_prop = db.UserProperty(auto_current_user_add=True) # integer_list_prop accepts a list of long integer values, # possibly an empty list. integer_list_prop = db.ListProperty(long) # string_list_prop accepts a list of string values, possibly an # empty list. string_list_prop = db.StringListProperty()
def search(cls, name_prefix=None): """Create search query based on info record name prefix. Args: name_prefix: User input name-prefix to search for. If name_prefix is empty string or None returns all records of Info sub-class. Records are sorted by their encoded name. Returns: Datastore query pointing to search results. """ name_prefix = _normalize_name(name_prefix) query = cls.all().order('encoded_name') if name_prefix: query.filter('encoded_name >=', db.ByteString(name_prefix)) # Do not need to worry about name_prefix + '\xff\xff' because not # a unicode character. query.filter('encoded_name <=', db.ByteString(name_prefix + '\xff')) return query
def get(self): e1 = Entity() e1.string_prop = 'string value, limited to 500 bytes' e1.text_prop = db.Text('text value, can be up to 1 megabyte') e1.short_blob_prop = db.ByteString('\x6B\x74\x68\x78\x62\x79\x65') e1.blob_prop = db.Blob('\x6B\x74\x68\x78\x62\x79\x65') e1.boolean_prop = True e1.integer_prop = 99 e1.float_prop = 3.14159 e1.datetime_prop = datetime.datetime.now() e1.null_prop = None e1.geopt_prop = db.GeoPt(47.620339, -122.349629) e1.multivalued_prop = ['string value', True, 3.14159] db.put(e1) self.response.write('<p>Created an entity, key: %s</p>' % e1.key()) e2 = Entity() e2.key_prop = e1.key() db.put(e2) self.response.write('<p>Created an entity, key: %s</p>' % e2.key()) a = Entity() a.prop1 = 'abc' a.prop2 = None a.put() # a has two properties: prop1 and prop2. prop2 has a null value. b = Entity() b.prop1 = 'def' b.put() # b has one property: prop1. It does not have a property named prop2. b.prop2 = 123 b.put() # b now has a property named prop2. del b.prop2 b.put() # b no longer has a property named prop2. db.delete([e1, e2, a, b]) self.response.write('<p>Entities deleted.</p>') self.response.write('<p>The time is: %s</p>' % str(datetime.datetime.now()))
from google.appengine.ext import db import datetime print 'Content-Type: text/html' print '' class Entity(db.Expando): pass e1 = Entity() e1.string_prop = 'string value, limited to 500 bytes' e1.text_prop = db.Text('text value, can be up to 1 megabyte') e1.short_blob_prop = db.ByteString('\x6B\x74\x68\x78\x62\x79\x65') e1.blob_prop = db.Blob('\x6B\x74\x68\x78\x62\x79\x65') e1.boolean_prop = True e1.integer_prop = 99 e1.float_prop = 3.14159 e1.datetime_prop = datetime.datetime.now() e1.null_prop = None e1.geopt_prop = db.GeoPt(47.620339, -122.349629) e1.multivalued_prop = ['string value', True, 3.14159] db.put(e1) print '<p>Created an entity, key: %s</p>' % e1.key() e2 = Entity() e2.key_prop = e1.key() db.put(e2)