def __create_value_for_mongo_value(self, mongo_value): if isinstance(mongo_value, Binary): return datastore_types.Blob(str(mongo_value)) if isinstance(mongo_value, types.DictType): if mongo_value['class'] == 'rating': return datastore_types.Rating(int(mongo_value["rating"])) if mongo_value['class'] == 'category': return datastore_types.Category(mongo_value["category"]) if mongo_value['class'] == 'key': return self.__key_for_id(mongo_value['path']) if mongo_value['class'] == 'list': return [ self.__create_value_for_mongo_value(v) for v in mongo_value['list'] ] if mongo_value['class'] == 'user': return users.User(email=mongo_value["email"]) if mongo_value['class'] == 'text': return datastore_types.Text(mongo_value['string']) if mongo_value['class'] == 'im': return datastore_types.IM(mongo_value['protocol'], mongo_value['address']) if mongo_value['class'] == 'geopt': return datastore_types.GeoPt(mongo_value['lat'], mongo_value['lon']) if mongo_value['class'] == 'email': return datastore_types.Email(mongo_value['value']) if mongo_value['class'] == 'bytes': return datastore_types.ByteString(mongo_value['value']) if mongo_value['class'] == 'blobkey': return datastore_types.BlobKey(mongo_value['value']) return mongo_value
def testDatastoreTypes(self): """Puts and gets different basic datastore types.""" entity = datastore.Entity('TestKind') entity.update({ 'rating': datastore_types.Rating(1), 'category': datastore_types.Category('bugs'), 'key': datastore_types.Key.from_path('foo', 'bar'), 'user': users.User('*****@*****.**'), 'text': datastore_types.Text('some text'), 'blob': datastore_types.Blob('data'), 'bytestring': datastore_types.ByteString('data'), 'im': datastore_types.IM('http://example.com/', 'Larry97'), 'geopt': datastore_types.GeoPt(1.1234, -1.1234), 'email': datastore_types.Email('*****@*****.**'), 'blobkey': datastore_types.BlobKey('27f5a7'), }) datastore.Put(entity) e = datastore.Get(entity) datastore.Delete(entity)
def test_entity_to_dict(self): """Converts a datastore.Entity instance to a JSON encodable dict.""" from datetime import datetime from gaesynkit import handlers from google.appengine.api import datastore from google.appengine.api import datastore_types from google.appengine.api import users from google.appengine.ext import db entity = datastore.Entity("Test") entity.update({ "string": "A string.", "byte_string": datastore_types.ByteString("Byte String"), "boolean": True, "int": 42, "float": 1.82, "date": datetime(2011, 01, 06), "list": [1, 2, 3, 4], "key": db.Key.from_path("Kind", "name"), "user": users.User("*****@*****.**"), "email": db.Email("*****@*****.**"), "location": db.GeoPt(52.500556, 13.398889), "category": db.Category("coding"), "link": db.Link("http://www.google.com"), "im": db.IM("sip", "foobar"), "phone": db.PhoneNumber("1 (206) 555-1212"), "address": db.PostalAddress("Address"), "rating": db.Rating(99) })
def save_queues_to_db(self): """ Stores file queue information into the datastore. Raises: ValueError: If queue info has not been set. """ if not self._queue_info_file: raise ValueError("Queue info must be set before saving the queues") json_queues = json.dumps(self._queue_info_file) entity = datastore.Entity(self.QUEUE_KIND, name=self._app_id, _app=self.APPSCALE_QUEUES) entity.update({self.QUEUE_INFO: datastore_types.Blob(json_queues), self.APP_NAME: datastore_types.ByteString(self._app_id)}) datastore.Put(entity)
def parse(self, value): # Parse escaped Python strings to ByteString values. # It is an error if the string contains non-ASCII values. bytestring = value.encode('ascii').decode('string-escape') return datastore_types.ByteString(bytestring)
def bytestring_from_base64(value): """Return a datastore bytestring property from a base64 encoded value.""" decoded_value = base64.b64decode(value) return datastore_types.ByteString(decoded_value)