Beispiel #1
0
    def _map_entity(self, entity):
        new_key = Key.from_path(self.to_kind,
                                entity.key().id_or_name(),
                                namespace=self.to_namespace)

        parent = entity.parent()
        if parent:
            # If the entity has an ancestor then we need to make sure that that ancestor exists in
            # the new namespace as well
            new_parent_key = Key.from_path(parent.kind(),
                                           parent.is_or_name(),
                                           namespace=self.to_namespace)
            new_parent_exists = Get([new_parent_key])[0]
            if not new_parent_exists:
                raise DataError(
                    "Trying to copy entity with an ancestor (%r) to a new namespace but the "
                    "ancestor does not exist in the new namespace. Copy the ancestors first."
                    % entity.key())

        def txn():
            existing = Get([new_key])[0]
            if existing and not self.overwrite_existing:
                return
            if isinstance(entity.key().id_or_name(), (int, long)):
                reserve_id(self.to_kind,
                           entity.key().id_or_name(), self.to_namespace)
            new_entity = clone_entity(entity, new_key)
            Put(new_entity)

        RunInTransaction(txn)
Beispiel #2
0
    def _map_entity(self, entity):
        def txn(entity):
            entity = Get(entity.key())
            try:
                entity[self.to_column_name] = entity[self.from_column_name]
            except KeyError:
                return
            Put(entity)

        RunInTransaction(txn, entity)
Beispiel #3
0
    def _map_entity(self, entity):
        column_name = self.field.db_column or self.name
        # Call get_default() separately for each entity, in case it's a callable like timezone.now
        value = self.field.get_default()

        def txn(entity):
            entity = Get(entity.key())
            entity[column_name] = value
            Put(entity)

        RunInTransaction(txn, entity)
Beispiel #4
0
    def _map_entity(self, entity):
        column_name = self.field.db_column or self.name

        def txn(entity):
            entity = Get(entity.key())
            try:
                del entity[column_name]
            except KeyError:
                return
            Put(entity)

        RunInTransaction(txn, entity)
Beispiel #5
0
    def _map_entity(self, entity):
        new_key = Key.from_path(self.to_kind,
                                entity.key().id_or_name(),
                                namespace=self.namespace)

        def txn():
            try:
                existing = Get(new_key)
            except datastore_errors.EntityNotFoundError:
                existing = None
            if existing and not self.overwrite_existing:
                return
            if isinstance(entity.key().id_or_name(), (int, long)):
                reserve_id(self.to_kind,
                           entity.key().id_or_name(), self.namespace)
            new_entity = clone_entity(entity, new_key)
            Put(new_entity)

        RunInTransaction(txn)
Beispiel #6
0
 def _commit_locked(*args, **kw):
     from google.appengine.api.datastore import RunInTransaction
     return RunInTransaction(func, *args, **kw)