def get_entities(keys): rpc = datastore.GetRpcFromKwargs({}) keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys) entities = None try: entities = datastore.Get(keys, rpc=rpc) except datastore_errors.EntityNotFoundError: assert not multiple return entities
def get_by_id(cls, ids, parent=None, **kwargs): if not parent: parent = cls.db_parent rpc = datastore.GetRpcFromKwargs(kwargs) if isinstance(parent, db.Model): parent = parent.key() ids, multiple = datastore.NormalizeAndTypeCheck(ids, (int, long)) keys = [ datastore.Key.from_path(cls.kind(), id, parent=parent) for id in ids ] if multiple: return get(keys, rpc=rpc) else: return get(keys[0], rpc=rpc)
def put_entities(entities): rpc = datastore.GetRpcFromKwargs({}) keys = datastore.Put(entities, rpc=rpc) return keys
class BaseModel(db.Model): db_parent = BASEMODEL_PARENT protect_attrs = [] additional_attrs = [] @classmethod def create(cls, **kwargs): return BaseModel._create(cls, **kwargs) @staticmethod def _create(cls, **kwargs): parent = BASEMODEL_PARENT return cls(parent, **kwargs) @property def str_key(self): return str(self.key()) @property def id(self): return str(self.key().id()) @property def key_name(self): return self.key().name() def to_dict(self): _res = {} if hasattr(self, "stats"): _res["stats"] = self.stats.to_dict() _res["id"] = str(self.id) props = self.properties() res = dict([(k, getattr(self, k)) for k in props.keys() if (not k.startswith("_") and k not in self.protect_attrs) ]) res.update(_res) return res def update(self, commit=True, **kwargs): props = self.properties() need_save = False for attr, val in kwargs.iteritems(): if getattr(self, attr, None) != val: if (not attr.startswith("_")) and (attr != "id"): if (attr in props) or (unicode(attr) in props ) or attr in self.additional_attrs: need_save = True val = gae_type_convert(val, props[attr].data_type) setattr(self, attr, val) if attr == "public" and hasattr(self, "stats"): self.stats.public = val self.stats.save() if need_save: self.save() return self def put(self): count = 0 while count < 3: try: ret = db.Model.put(self) if ret: break except db.Timeout: count += 1 else: raise db.Timeout() remove(self.key()) return ret def save(self, commit=True): return self.put() def delete(self, commit=False): keys = [self.key()] if hasattr(self, "stats"): keys.append(self.stats.key()) remove(keys) return db.delete(keys) @classmethod def get_by_key_name(cls, key_names, parent=None, **kwargs): try: if not parent: parent = cls.db_parent parent = db._coerce_to_key(parent) except db.BadKeyError, e: raise db.BadArgumentError(str(e)) rpc = datastore.GetRpcFromKwargs(kwargs) key_names, multiple = datastore.NormalizeAndTypeCheck( key_names, basestring) keys = [ datastore.Key.from_path(cls.kind(), name, parent=parent) for name in key_names ] if multiple: return get(keys, rpc=rpc) else: return get(keys[0], rpc=rpc)
def store(self, **kwargs): rpc = datastore.GetRpcFromKwargs(kwargs) self._populate_internal_entity() return datastore.Put(self._entity, rpc=rpc)
class BaseModel(db.Model): key_template = "" db_parent = BASEMODEL_PARENT @property def str_key(self): return str(self.key()) @property def id(self): return self.key().id() @property def keyname(self): return self.key().name() def save_settings(self, **kwds): props = self.properties() for p in props.keys(): if unicode(p) in kwds: val = kwds.get(p) t = props[p].data_type if t == list and not isinstance(val, list): val = [ line.strip() for line in val.strip().split("\n") if (line and line.strip()) ] elif t == bool and not isinstance(val, bool): val = val.strip().capitalize() if val == "False" or val == u"False": val = False else: val = True elif t == basestring: try: val = str(val).strip() except: val = unicode(val).strip() else: val = t(val) setattr(self, p, val) self.save() return self def put(self): count = 0 while count < 3: try: ret = db.Model.put(self) if ret: break except db.Timeout: count += 1 else: raise db.Timeout() remove(self.key()) return ret save = put Save = put def delete(self): remove(self.key()) return super(BaseModel, self).delete() @classmethod def gen_key_name(cls, **kw): try: return cls.key_template % kw except KeyError: logging.warn('generate key_name failed: %s <- %s', cls.key_template, kw) @classmethod def get_by_key_name(cls, key_names, parent=None, **kwargs): try: if not parent: parent = cls.db_parent parent = db._coerce_to_key(parent) except db.BadKeyError, e: raise db.BadArgumentError(str(e)) rpc = datastore.GetRpcFromKwargs(kwargs) key_names, multiple = datastore.NormalizeAndTypeCheck( key_names, basestring) keys = [ datastore.Key.from_path(cls.kind(), name, parent=parent) for name in key_names ] if multiple: return get(keys, rpc=rpc) else: return get(keys[0], rpc=rpc)