Пример #1
0
def relate_assignees(assessment, related):
    """Generates assignee list and relates them to Assessment objects

    Args:
        assessment (model instance): Assessment model
        related (dict): Dict containing model instances related to assessment
                        - obj
                        - audit
                        - template (an AssessmentTemplate, can be None)
  """
    people_types = {
        "assessors": "Assessor",
        "verifiers": "Verifier",
        "creator": "Creator",
    }
    people_list = []

    for person_key, person_type in people_types.iteritems():
        assign_people(get_value(person_key, **related), person_type,
                      assessment, people_list)

    for person in people_list:
        if person['source'] is not None and person['destination'] is not None:
            rel = Relationship(source=person['source'],
                               destination=person['destination'],
                               context=person['context'])
            rel.attrs = person['attrs']
            db.session.add(rel)
Пример #2
0
 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 = 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 = 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
Пример #3
0
def relate_assignees(assessment, related):
  """Generates assignee list and relates them to Assessment objects

    Args:
        assessment (model instance): Assessment model
        related (dict): Dict containing model instances related to assessment
                        - obj
                        - audit
                        - template (an AssessmentTemplate, can be None)
  """
  people_types = {
      "assessors": "Assessor",
      "verifiers": "Verifier",
      "creator": "Creator",
  }
  people_list = []

  for person_key, person_type in people_types.iteritems():
    assign_people(
        get_value(person_key, **related),
        person_type, assessment, people_list)

  for person in people_list:
    if person['source'] is not None and person['destination'] is not None:
      rel = Relationship(
          source=person['source'],
          destination=person['destination'],
          context=person['context'])
      rel.attrs = person['attrs']
      db.session.add(rel)
Пример #4
0
 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 = 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 = 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
Пример #5
0
 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 = Relationship.find_related(current_obj, obj)
     if not self.unmap and not mapping:
       mapping = Relationship(source=current_obj, destination=obj)
       relationships.append(mapping)
       db.session.add(mapping)
     elif self.unmap and mapping:
       db.session.delete(mapping)
   db.session.flush()
   self.dry_run = True
Пример #6
0
 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 = Comment(description=description,
                           modified_by_id=get_current_user_id())
         db.session.add(comment)
         mapping = Relationship(source=current_obj, destination=comment)
         db.session.add(mapping)
Пример #7
0
 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 = []
   for obj in self.value:
     mapping = Relationship.find_related(current_obj, obj)
     if not self.unmap and not mapping:
       mapping = Relationship(source=current_obj, destination=obj)
       relationships.append(mapping)
       db.session.add(mapping)
     elif self.unmap and mapping:
       db.session.delete(mapping)
   db.session.flush()
   # 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
Пример #8
0
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(Relationship(
      source=src,
      destination_id=dst["id"],
      destination_type=dst["type"],
      context_id=src.context_id,
  ))
Пример #9
0
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
  """
  dst = dst or {}
  if 'id' not in dst or 'type' not in dst:
    return
  rel = Relationship(**{
      "source": src,
      "destination_id": dst["id"],
      "destination_type": dst["type"],
      "context": src.context,
  })
  db.session.add(rel)
Пример #10
0
def map_assessment(assessment, obj):
  """Creates a relationship between an assessment and an object. This also
  generates automappings. Fails silently if obj dict does not have id and type
  keys.

  Args:
    assessment (models.Assessment): The assessment model
    obj (dict): A dict with `id` and `type`.
  Returns:
    None
  """
  obj = obj or {}
  if 'id' not in obj or 'type' not in obj:
    return
  rel = Relationship(**{
      "source": assessment,
      "destination_id": obj["id"],
      "destination_type": obj["type"],
      "context": assessment.context,
  })
  db.session.add(rel)
Пример #11
0
 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 = []
   for obj in self.value:
     mapping = Relationship.find_related(current_obj, obj)
     if not self.unmap and not mapping:
       mapping = Relationship(source=current_obj, destination=obj)
       relationships.append(mapping)
       db.session.add(mapping)
     elif self.unmap and mapping:
       db.session.delete(mapping)
   db.session.flush()
   # 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