def get_comment_data(notif): """Return data for comment notifications. This functions checks who should receive the notification and who not, with the Commentable mixin that must be added on the object which has the current comment. If the object on which the comment was made is not Commentable, the function will return an empty dict. Args: notif (Notification): notification with a Comment object_type. Returns: Dict with all data needed for sending comment notifications. """ data = {} recipients = set() comment = get_notification_object(notif) comment_obj = None rel = models.Relationship.find_related(comment, models.Assessment()) if rel: comment_obj = rel.Assessment_destination or rel.Assessment_source if not comment_obj: logger.warning('Comment object not found for notification %s', notif.id) return {} if comment_obj.recipients: recipients = set(comment_obj.recipients.split(",")) for person, assignee_type in comment_obj.assignees: if not recipients or recipients.intersection(set(assignee_type)): data[person.email] = generate_comment_notification( comment_obj, comment, person) return data
def test_create_new_assessment_with_mapped_control(self): "Test for creation assessment with mapped controls" with factories.single_commit(): audit = factories.AuditFactory() control = factories.ControlFactory() revision = models.Revision.query.filter( models.Revision.resource_id == control.id, models.Revision.resource_type == control.__class__.__name__ ).order_by( models.Revision.id.desc() ).first() factories.SnapshotFactory( parent=audit, child_id=control.id, child_type=control.__class__.__name__, revision_id=revision.id ) db.session.commit() self.assertFalse(db.session.query( models.Relationship.get_related_query( models.Assessment(), models.Snapshot() ).exists()).first()[0]) slug = "TestAssessment" response = self.import_data(OrderedDict([ ("object_type", "Assessment"), ("Code*", slug), ("Audit*", audit.slug), ("Assignees*", models.Person.query.all()[0].email), ("Creators", models.Person.query.all()[0].email), ("Title", "Strange title"), ("map:Control versions", control.slug), ])) self._check_csv_response(response, {}) assessment = models.Assessment.query.filter( models.Assessment.slug == slug ).first() self.assertTrue(db.session.query(models.Relationship.get_related_query( assessment, models.Snapshot()).exists()).first()[0] )