Package pyamf :: Package adapters :: Module _google_appengine_ext_db
[hide private]
[frames] | no frames]

Source Code for Module pyamf.adapters._google_appengine_ext_db

  1  # Copyright (c) 2007-2009 The PyAMF Project. 
  2  # See LICENSE for details. 
  3   
  4  """ 
  5  Google App Engine adapter module. 
  6   
  7  Sets up basic type mapping and class mappings for using the Datastore API 
  8  in Google App Engine. 
  9   
 10  @see: U{Datastore API on Google App Engine (external) 
 11  <http://code.google.com/appengine/docs/datastore>} 
 12   
 13  @since: 0.3.1 
 14  """ 
 15   
 16  from google.appengine.ext import db 
 17  import datetime 
 18   
 19  import pyamf 
 20   
21 -class ModelStub(object):
22 - def __init__(self):
23 for attr in DataStoreClassAlias.INTERNAL_ATTRS: 24 setattr(self, attr, None)
25
26 -class DataStoreClassAlias(pyamf.ClassAlias):
27 # The name of the attribute used to represent the key 28 KEY_ATTR = '_key' 29 30 # A list of private attributes on a db.Model/db.Expando list that need to 31 # be synced with the datastore instance 32 INTERNAL_ATTRS = ['_entity', '_parent', '_key_name', '_app', '_parent_key'] 33
34 - def getAttrs(self, obj):
35 """ 36 @since: 0.4 37 """ 38 static_attrs, dynamic_attrs = pyamf.ClassAlias.getAttrs(self, obj) 39 40 if static_attrs is None: 41 static_attrs = obj.properties().keys() 42 43 if obj.is_saved(): 44 static_attrs.insert(0, self.KEY_ATTR) 45 46 if dynamic_attrs is None: 47 dynamic_attrs = obj.dynamic_properties() 48 49 return static_attrs, dynamic_attrs
50
51 - def getAttributes(self, obj):
52 static_attrs = {} 53 dynamic_attrs = {} 54 static_attrs_names, dynamic_attrs_names = self.getAttrs(obj) 55 56 if self.KEY_ATTR in static_attrs_names: 57 static_attrs_names.remove(self.KEY_ATTR) 58 59 for a in static_attrs_names: 60 static_attrs[a] = getattr(obj, a) 61 62 if obj.is_saved(): 63 static_attrs[self.KEY_ATTR] = str(obj.key()) 64 65 for a in dynamic_attrs_names: 66 dynamic_attrs[a] = getattr(obj, a) 67 68 return static_attrs, dynamic_attrs
69
70 - def createInstance(self):
71 return ModelStub()
72
73 - def applyAttributes(self, obj, attrs):
74 new_obj = None 75 76 if DataStoreClassAlias.KEY_ATTR in attrs.keys() and attrs[DataStoreClassAlias.KEY_ATTR] is not None: 77 new_obj = self.klass.get(attrs[DataStoreClassAlias.KEY_ATTR]) 78 del attrs[DataStoreClassAlias.KEY_ATTR] 79 80 properties = self.klass.properties() 81 p_keys = properties.keys() 82 83 if new_obj is not None: 84 for a in DataStoreClassAlias.INTERNAL_ATTRS: 85 if hasattr(new_obj, a): 86 setattr(obj, a, getattr(new_obj, a)) 87 88 for k in self.klass.properties().keys(): 89 setattr(obj, k, getattr(new_obj, k)) 90 91 for k in new_obj.dynamic_properties(): 92 setattr(obj, k, getattr(new_obj, k)) 93 94 obj.__class__ = self.klass 95 96 for k, v in attrs.iteritems(): 97 if k in p_keys: 98 prop = properties[k] 99 100 if isinstance(v, datetime.datetime): 101 if isinstance(prop, db.DateProperty): 102 v = v.date() 103 elif isinstance(prop, db.TimeProperty): 104 v = v.time() 105 106 setattr(obj, k, v)
107
108 -def handleQuery(q):
109 if q.count() == 0: 110 return [] 111 112 return [i for i in q]
113 114 pyamf.add_type(db.Query, handleQuery) 115 pyamf.register_alias_type(DataStoreClassAlias, db.Model, db.Expando) 116