Exemplo n.º 1
0
 def insert_object(self):
     "insert object handler"
     if self.dry_run or not self.value:
         return
     row_obj = self.row_converter.obj
     relationships = []
     child_id_snapshot_dict = {
         i.child_id: i
         for i in self.audit_object_pool_query.all()
     }
     for obj in self.value:
         snapshot = child_id_snapshot_dict.get(obj.id)
         mapping = models.Relationship.find_related(row_obj, snapshot)
         if not self.unmap and not mapping:
             mapping = models.Relationship(source=row_obj,
                                           destination=snapshot)
             relationships.append(mapping)
             db.session.add(mapping)
         elif self.unmap and mapping:
             db.session.delete(mapping)
     db.session.flush(relationships)
     # it is safe to reuse this automapper since no other objects will be
     # created while creating automappings and cache reuse yields significant
     # performance boost
     automapper = AutomapperGenerator(use_benchmark=False)
     for relation in relationships:
         automapper.generate_automappings(relation)
     self.dry_run = True
Exemplo n.º 2
0
    def insert_object(self):
        """Update document URL values

    This function adds missing URLs and remove existing ones from Documents.
    The existing URLs with new titles just change the title.
    """
        if not self.value or self.row_converter.ignore:
            return

        new_link_map = {doc.link: doc for doc in self.value}
        old_link_map = {
            doc.link: doc
            for doc in self.row_converter.obj.related_objects()
            if isinstance(doc, models.Document)
        }

        for new_link, new_doc in new_link_map.iteritems():
            if new_link in old_link_map:
                old_link_map[new_link].title = new_doc.title
            else:
                models.Relationship(
                    source=self.row_converter.obj,
                    destination=new_doc,
                )

        for old_link, old_doc in old_link_map.iteritems():
            if old_link not in new_link_map:
                if old_doc.related_destinations:
                    old_doc.related_destinations.pop()
                elif old_doc.related_sources:
                    old_doc.related_sources.pop()
                else:
                    current_app.logger.warning(
                        "Invalid relationship state for document "
                        "URLs.")
Exemplo n.º 3
0
    def insert_object(self):
        """Update document URL values

    This function adds missing URLs and remove existing ones from Documents.
    The existing URLs with new titles just change the title.
    """
        if self.row_converter.ignore:
            return
        new_link_map = {d.link: d for d in self.value}
        old_link_map = self._get_old_map()

        for new_link, new_doc in new_link_map.iteritems():
            if new_link in old_link_map:
                old_link_map[new_link].title = new_doc.title
            else:
                models.Relationship(source=self.row_converter.obj,
                                    destination=new_doc)

        for old_link, old_doc in old_link_map.iteritems():
            if old_link in new_link_map:
                continue
            if old_doc.related_destinations:
                old_doc.related_destinations.pop()
            elif old_doc.related_sources:
                old_doc.related_sources.pop()
            else:
                logger.warning("Invalid relationship state for document URLs.")
Exemplo n.º 4
0
 def _create_relationship(self, person):
     relation = models.Relationship(
         source=person,
         destination=self.row_converter.obj,
         context_id=self.row_converter.obj.context_id,
     )
     db.session.add(relation)
     db.session.flush()
     self._create_relationship_attr(relation)
Exemplo n.º 5
0
 def insert_object(self):
     "insert object handler"
     if self.dry_run or not self.value:
         return
     row_obj = self.row_converter.obj
     relationships = []
     child_ids = [o.id for o in self.value]
     child_id_snapshot_dict = {
         i.child_id: i
         for i in self.get_audit_object_pool_query(child_ids).options(
             sqlalchemy.orm.undefer_group('Snapshot_complete'))
     }
     mapping_query = models.Relationship.query.filter(
         models.Relationship.source_type == row_obj.type,
         models.Relationship.source_id == row_obj.id,
         models.Relationship.destination_type == models.Snapshot.__name__,
     ).union(
         models.Relationship.query.filter(
             models.Relationship.destination_type == row_obj.type,
             models.Relationship.destination_id == row_obj.id,
             models.Relationship.source_type == models.Snapshot.__name__,
         )).options(
             sqlalchemy.orm.load_only(models.Relationship.id),
             sqlalchemy.orm.load_only(models.Relationship.source_id),
             sqlalchemy.orm.load_only(models.Relationship.source_type),
             sqlalchemy.orm.load_only(models.Relationship.destination_id),
             sqlalchemy.orm.load_only(models.Relationship.destination_type),
         )
     mappings = {(r.source_id if r.source_type == models.Snapshot.__name__
                  else r.destination_id): r
                 for r in mapping_query}
     for obj in self.value:
         snapshot = child_id_snapshot_dict.get(obj.id)
         mapping = mappings.get(snapshot.id)
         if not self.unmap and not mapping:
             mapping = models.Relationship(source=row_obj,
                                           destination=snapshot)
             relationships.append(mapping)
             db.session.add(mapping)
             signals.Restful.model_posted.send(models.Relationship,
                                               obj=mapping)
         elif self.unmap and mapping:
             db.session.delete(mapping)
             signals.Restful.model_deleted.send(models.Relationship,
                                                obj=mapping)
     db.session.flush(relationships)
     self.dry_run = True
Exemplo n.º 6
0
 def insert_object(self):
     "insert object handler"
     if self.dry_run or not self.value:
         return
     row_obj = self.row_converter.obj
     relationships = []
     child_id_snapshot_dict = {
         i.child_id: i
         for i in self.audit_object_pool_query.all()
     }
     for obj in self.value:
         snapshot = child_id_snapshot_dict.get(obj.id)
         mapping = models.Relationship.find_related(row_obj, snapshot)
         if not self.unmap and not mapping:
             mapping = models.Relationship(source=row_obj,
                                           destination=snapshot)
             relationships.append(mapping)
             db.session.add(mapping)
         elif self.unmap and mapping:
             db.session.delete(mapping)
     db.session.flush(relationships)
     self.dry_run = True