def flush(self): for namespace, path_to_max_id in self.ns_to_path_to_max_id.iteritems(): for path, max_id in path_to_max_id.iteritems(): datastore.AllocateIds(db.Key.from_path(namespace=namespace, _app=self.app_id, *list(path)), max=max_id) self.ns_to_path_to_max_id = collections.defaultdict(dict)
def execute_pre_save_hooks(kind, pb_key, entity): put_type_id = put_type.UNKNOWN func_list = pre_save_hooks.get(kind, None) if func_list is not None: key = db.Key._FromPb(pb_key) if not key.id_or_name(): path = key.to_path()[:-1] path.append(1) model_key = db.Key.from_path(*path) ids = datastore.AllocateIds(model_key, 1) path = path[:-1] path.append(ids[0]) new_key = db.Key.from_path(*path) pb_key = new_key._ToPb() entity.key().CopyFrom(pb_key) group = entity.mutable_entity_group() root = entity.key().path().element(0) group.add_element().CopyFrom(root) e = datastore.Entity._FromPb(entity) instance = db.class_for_kind(kind).from_entity(e) put_type_id = put_type.NEWLY_CREATED else: entity.key_.CopyFrom(pb_key) e = datastore.Entity._FromPb(entity) instance = db.class_for_kind(kind).from_entity(e) created = get_created_datetime(instance) updated = get_updated_datetime(instance) if created: threshold = datetime.timedelta(0, 0, 1000) if updated: if abs(created - updated) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED else: if (datetime.datetime.now() - created) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED for func in func_list: func(instance, put_type_id)