Пример #1
0
 def handle_post(self):
     """POST handler."""
     # pylint: disable=attribute-defined-outside-init
     self.proposed_by = login.get_current_user()
     # pylint: enable=attribute-defined-outside-init
     self._add_comment_about(self.STATES.PROPOSED, self.agenda)
     relationship.Relationship(source=self.instance, destination=self)
Пример #2
0
    def _get_model_instance(self, src=None):
        """For Parent and Snapshottable src and dst, create an empty Snapshot.
    Args:
      src: dict containing new object source.

    Returns:
      obj: An instance of current model.
      needs_update: Flag shows if we need to update obj attributes.
    """
        needs_update = True
        _, _, is_snapshot = self._parse_snapshot_data(src)

        if is_snapshot:
            snapshot = Snapshot()
            db.session.add(snapshot)

            return snapshot, needs_update

        _relationship = self._get_relationship(src)

        if _relationship:
            needs_update = _relationship.is_external
        else:
            _relationship = relationship.Relationship()
            db.session.add(_relationship)
        return _relationship, needs_update
Пример #3
0
def create_audit_relationship(audit_stub, obj):
    """Create audit to assessment template relationship"""
    parent_audit = audit.Audit.query.get(audit_stub["id"])

    rel = relationship.Relationship(source=parent_audit,
                                    destination=obj,
                                    context=parent_audit.context)
    db.session.add(rel)
Пример #4
0
 def _build_relationship(self, parent_obj):
   """Build relationship between document and documentable object"""
   from ggrc.models import relationship
   rel = relationship.Relationship(
       source=parent_obj,
       destination=self
   )
   db.session.add(rel)
Пример #5
0
def _ensure_program_relationships(snapshots):
  """Ensure that snapshotted object is related to audit program.

  This function is made to handle multiple snapshots for a single audit.
  Args:
    snapshots: list of snapshot objects with child_id, child_type and parent.
  """
  # assert that every parent is an Audit as the code relies on program_id field
  assert {s.parent_type for s in snapshots} == {"Audit"}

  rel = relationship.Relationship

  program_children = {}
  for obj in snapshots:
    program_children.setdefault(obj.parent.program, set()).add(
        (obj.child_type, obj.child_id)
    )

  for program, children_set in program_children.items():
    query = db.session.query(
        rel.destination_type, rel.destination_id
    ).filter(
        and_(
            rel.source_type == "Program",
            rel.source_id == program.id,
            tuple_(rel.destination_type, rel.destination_id).in_(children_set)
        )
    ).union_all(
        db.session.query(
            rel.source_type, rel.source_id
        ).filter(
            and_(
                rel.destination_type == "Program",
                rel.destination_id == program.id,
                tuple_(rel.source_type, rel.source_id).in_(children_set)
            )
        )
    )
    children_set.difference_update(query.all())

    child_objects = {}
    type_ids = {}
    for child in children_set:
      type_ids.setdefault(child[0], set()).add(child[1])
    for child_type, ids in type_ids.items():
      child_model = inflector.get_model(child_type)
      query = child_model.query.filter(child_model.id.in_(ids))
      for child in query:
        child_objects[(child.type, child.id)] = child

    for child in children_set:
      if child in child_objects:
        db.session.add(
            relationship.Relationship(
                source=program,
                destination=child_objects[child],
            )
        )
Пример #6
0
def create_audit_relationship(audit_stub, obj):
    """Create audit to assessment template relationship"""
    # pylint: disable=W0212
    parent_audit = audit.Audit.query.get(audit_stub["id"])
    if not permissions_for()._is_allowed_for(parent_audit, "update"):
        raise Forbidden()
    rel = relationship.Relationship(source=parent_audit,
                                    destination=obj,
                                    context=parent_audit.context)
    db.session.add(rel)
Пример #7
0
    def clone(self, target):
        """Clone Assessment Template and related custom attributes."""
        assessment_template_copy = self._clone()
        rel = relationship.Relationship(source=target,
                                        destination=assessment_template_copy)
        db.session.add(rel)
        db.session.flush()

        for cad in self.custom_attribute_definitions:
            # pylint: disable=protected-access
            cad._clone(assessment_template_copy)

        return (assessment_template_copy, rel)
Пример #8
0
  def add_comment(comment_text, source, initiator_object=None):
    """Adds comment into the session.

      Args:
        comment_text: comment text.
        source: object to which comment will be attached via relationship.
        initiator_object: initiator of comment creation.
    """
    created_comment = comment.Comment(
        description=comment_text,
        modified_by_id=login.get_current_user_id(),
        initiator_instance=initiator_object,
    )
    relationship.Relationship(
        source=source,
        destination=created_comment
    )
Пример #9
0
  def _copy_obj(cls, source, target=None):
    """Make object copy of source into target as destination.

    Source will be cloned and mapped to target if it's provided.

    Args:
        source: Object that should be clonned.
        target: Destination for coppied object.

    Returns:
        Cloned object.
    """
    # pylint: disable=protected-access
    clonned_object = source._clone(target)
    if target:
      db.session.add(relationship.Relationship(
          source=target,
          destination=clonned_object,
      ))
    return clonned_object
Пример #10
0
    def json_create(self, obj, src):
        """For Parent and Snapshottable src and dst, fill in the Snapshot obj."""
        parent, child, is_snapshot = self._parse_snapshot_data(src)

        if is_snapshot:
            snapshot_data = {
                "parent": parent,
                "child_type": child["type"],
                "child_id": child["id"],
                "update_revision": "new",
            }
            json_builder.create(obj, snapshot_data)
            obj.modified_by = get_current_user()
            obj.context = obj.parent.context
            relationship.Relationship(
                source=obj.parent,
                destination=obj,
            )
            return None

        return super(RelationshipResource, self).json_create(obj, src)
Пример #11
0
 def task_group(self, task_group):
   """Setter for task group foreign key."""
   if not self._task_group and task_group:
     relationship.Relationship(source=task_group, destination=self)
   self._task_group = task_group
 def cycle_task_group(self, cycle_task_group):
     """Setter for cycle task group foreign key."""
     if not self._cycle_task_group and cycle_task_group:
         relationship.Relationship(source=cycle_task_group,
                                   destination=self)
     self._cycle_task_group = cycle_task_group
Пример #13
0
 def _create_relationship(self):
   """Create relationship for newly created review (used for ACL)"""
   if self in db.session.new:
     db.session.add(
         relationship.Relationship(source=self.reviewable, destination=self)
     )
Пример #14
0
 def workflow(self, workflow):
     """Set workflow foreign key and relationship."""
     if not self._workflow and workflow:
         relationship.Relationship(source=workflow, destination=self)
     self._workflow = workflow
Пример #15
0
 def cycle(self, cycle):
     """Set cycle foreign key and relationship."""
     if not self._cycle and cycle:
         relationship.Relationship(source=cycle, destination=self)
     self._cycle = cycle
Пример #16
0
 def cycle_task_group_object_task(self, cycle_task):
     if not self._cycle_task_group_object_task and cycle_task:
         relationship.Relationship(source=cycle_task, destination=self)
     self._cycle_task_group_object_task = cycle_task