def set_obj_attr(self): """ Create comment relationships with LCA """ current_obj = self.row_converter.obj cad_obj = all_models.CustomAttributeDefinition.query.get(self.value) cav_id = cad_obj.attribute_values[-1].id current_obj.custom_attribute_revision_upd({ "custom_attribute_revision_upd": { "custom_attribute_definition": { "id": self.value }, "custom_attribute_value": { "id": cav_id }, }, }) current_user = get_current_user() definition = cad_obj.definition assignee_types = [ acl.ac_role.name for person, acl in definition.access_control_list if person == current_user ] current_obj.assignee_types = assignee_types mapping = all_models.Relationship(source=definition, destination=current_obj) db.session.add(mapping)
def set_obj_attr(self): """ Create comments """ if self.dry_run or not self.value: return current_obj = self.row_converter.obj for description in self.value: if current_obj.access_control_list: current_user = get_current_user() assignee_types = [ acl.ac_role.name for person, acl in current_obj.access_control_list if person == current_user ] assignee_type = ','.join(assignee_types) comment = all_models.Comment( description=description, modified_by_id=get_current_user_id(), assignee_type=assignee_type) else: comment = all_models.Comment( description=description, modified_by_id=get_current_user_id()) db.session.add(comment) mapping = all_models.Relationship(source=current_obj, destination=comment) db.session.add(mapping) self.row_converter.comments.append(comment)
def map_objects(src, dst): """Creates a relationship between an src and dst. This also generates automappings. Fails silently if dst dict does not have id and type keys. Args: src (model): The src model dst (dict): A dict with `id` and `type`. Returns: None """ if not dst: return if 'id' not in dst or 'type' not in dst: return destination = referenced_objects.get(dst["type"], dst["id"]) if not destination: raise ValueError("No {} with id {} found.".format( dst["type"], dst["id"])) check_mapping_permissions(src, destination) db.session.add( all_models.Relationship( source=src, destination=destination, context=src.context or destination.context, ))
def _create_relationship(self): """Create relationship for newly created review (used for ACL)""" from ggrc.models import all_models if self in db.session.new: db.session.add( all_models.Relationship(source=self.reviewable, destination=self))
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_evidence in new_link_map.iteritems(): if new_link not in old_link_map: rel_obj = all_models.Relationship( source=self.row_converter.obj, destination=new_evidence) signals.Restful.model_posted.send(rel_obj.__class__, obj=rel_obj) else: db.session.expunge(new_evidence) for old_link, old_evidence in old_link_map.iteritems(): if old_link in new_link_map: continue if old_evidence.related_destinations: signals.Restful.model_deleted.send(old_evidence.__class__, obj=old_evidence) old_evidence.related_destinations.pop() elif old_evidence.related_sources: signals.Restful.model_deleted.send(old_evidence.__class__, obj=old_evidence) old_evidence.related_sources.pop() else: logger.warning("Invalid relationship state for document URLs.")
def generate_assignee_relations(assessment, assignee_ids, verifier_ids, creator_ids): """Generates db relations to assessment for sent role ids. Args: assessment (model instance): Assessment model assignee_ids (list): list of person ids verifier_ids (list): list of person ids creator_ids (list): list of person ids """ people = set(assignee_ids + verifier_ids + creator_ids) person_dict = {i.id: i for i in all_models.Person.query.filter( all_models.Person.id.in_(people) )} for person_id in people: person = person_dict.get(person_id) if person is None: continue roles = [] if person_id in assignee_ids: roles.append("Assessor") if person_id in verifier_ids: roles.append("Verifier") if person_id in creator_ids: roles.append("Creator") rel = all_models.Relationship( source=person, destination=assessment, context=assessment.context, ) rel.attrs = {"AssigneeType": ",".join(roles)} db.session.add(rel)
def _add_audit_relationships(audits): """Add missing relationships for audit direct mappings.""" for audit in audits: all_models.Relationship( source=audit, destination=audit.program, )
def insert_object(self): """Update document Reference URL values This function adds missing URLs and remove existing ones from Documents. """ if self.row_converter.ignore: return new_link_map = {d.link: d for d in self.value} old_link_map = self._get_old_map() parent = self.row_converter.obj for new_link, new_doc in new_link_map.iteritems(): if new_link not in old_link_map: all_models.Relationship(source=parent, destination=new_doc) else: db.session.expunge(new_doc) for old_link, old_doc in old_link_map.iteritems(): if old_link in new_link_map: continue if not (self.remove_relationship(old_doc.related_destinations, lambda x: x.destination) or self.remove_relationship(old_doc.related_sources, lambda x: x.source)): logger.warning("Invalid relationship state for document URLs.")
def insert_object(self): """ Create a new mapping object """ if self.dry_run or not self.value: return current_obj = self.row_converter.obj relationships = [] mapping = None for obj in self.value: if current_obj.id: mapping = all_models.Relationship.find_related( current_obj, obj) if not self.unmap and not mapping: if not (self.mapping_object.__name__ == "Audit" and not getattr(current_obj, "allow_map_to_audit", True)): mapping = all_models.Relationship(source=current_obj, destination=obj) relationships.append(mapping) db.session.add(mapping) else: self.add_warning(errors.SINGLE_AUDIT_RESTRICTION, mapped_type=obj.type, object_type=current_obj.type) elif self.unmap and mapping: if not (self.mapping_object.__name__ == "Audit" and not getattr(current_obj, "allow_unmap_from_audit", True)): db.session.delete(mapping) else: self.add_warning(errors.UNMAP_AUDIT_RESTRICTION, mapped_type=obj.type, object_type=current_obj.type) db.session.flush() self.dry_run = True
def create_relationship(sender, obj=None, src=None, service=None, event=None, initial_state=None): # noqa """Create relationship between proposal and parent instance.""" if isinstance(obj, all_models.Proposal): all_models.Relationship(source=obj.instance, destination=obj)
def set_obj_attr(self): """ Create comments """ if self.dry_run or not self.value: return current_obj = self.row_converter.obj for description in self.value: comment = all_models.Comment(description=description, modified_by_id=get_current_user_id()) db.session.add(comment) mapping = all_models.Relationship(source=current_obj, destination=comment) db.session.add(mapping)
def _create_event_relationship(task, event): """Creates event relationship if there is no relationship.""" relationship = utils.get_relationship( left_id=event.id, left_model_name="CalendarEvent", right_id=task.id, right_model_name="CycleTaskGroupObjectTask", ) if not relationship: db.session.add( all_models.Relationship( source=task, destination=event, ))
def _create_event_with_relationship(self, task, person_id): """Creates calendar event and relationship based on task and person id.""" event = all_models.CalendarEvent( due_date=task.end_date, attendee_id=person_id, title=self.TASK_TITLE_TEMPLATE.format(prefix=self.title_prefix), modified_by_id=person_id, ) db.session.add(event) db.session.add( all_models.Relationship( source=task, destination=event, )) return event
def setup_evidence(self, source_gdrive_id, user_id): """Generate Evidence File object""" parent = self.row_converter.obj evidence = all_models.Evidence(title=source_gdrive_id, modified_by_id=user_id, context=parent.context, kind=self.KIND, source_gdrive_id=source_gdrive_id, parent_obj={ 'id': parent.id, 'type': parent.__class__.__name__ }) evidence.add_admin_role() rel_obj = all_models.Relationship( source=all_models.Assessment.query.get(parent.id), destination=evidence) signals.Restful.model_posted.send(rel_obj.__class__, obj=rel_obj)
def add_comment_about(proposal, reason, txt): """Create comment about proposal for reason with required text.""" if not isinstance(proposal.instance, comment.Commentable): return txt = txt or "" txt = txt.strip() if txt.startswith("<p>"): txt = txt[3:] if txt.endswith("</p>"): txt = txt[:-4] txt = txt.strip() comment_text = proposal.build_comment_text(reason, txt, proposal.proposed_by) created_comment = all_models.Comment( description=comment_text, modified_by_id=login.get_current_user_id(), initiator_instance=proposal) all_models.Relationship(source=proposal.instance, destination=created_comment)
def map_objects(src, dst): """Creates a relationship between an src and dst. This also generates automappings. Fails silently if dst dict does not have id and type keys. Args: src (model): The src model dst (dict): A dict with `id` and `type`. Returns: None """ if not dst: return if 'id' not in dst or 'type' not in dst: return db.session.add( all_models.Relationship( source=src, destination_id=dst["id"], destination_type=dst["type"], context_id=src.context_id, ))
def workflow(self, workflow): """Setter for workflow foreign key.""" if not self._workflow and workflow: all_models.Relationship(source=workflow, destination=self) self._workflow = workflow
def _build_relationship(self, parent_obj): """Build relationship between evidence and parent object""" from ggrc.models import all_models rel = all_models.Relationship(source=parent_obj, destination=self) db.session.add(rel) signals.Restful.model_put.send(rel.__class__, obj=rel, service=self)