Esempio n. 1
0
  def send_comment_notifications(self):
    """Send comment people mentions notifications."""
    from ggrc.notifications import people_mentions

    if self.comments:
      people_mentions.handle_comment_mapped(obj=self.obj,
                                            comments=self.comments)
Esempio n. 2
0
  def test_handle_task_comment(self, send_email_mock):
    """Test handling of mapped comment to cycle task."""
    with factories.single_commit():
      person = factories.PersonFactory(email="*****@*****.**")
      obj = wf_factories.CycleTaskGroupObjectTaskFactory(
          slug=u"TSK-1",
          title=u"task1",
      )
      comment = factories.CommentFactory(
          description=u"One <a href=\"mailto:[email protected]\"></a>",
      )
      comment.modified_by_id = person.id
      comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
      url = "http://localhost/dashboard#!task&query=%22task%20slug%22%3DTSK-1"

    people_mentions.handle_comment_mapped(obj, [comment])
    expected_title = (u"[email protected] mentioned you "
                      u"on a comment within task1")
    expected_body = (
        u"[email protected] mentioned you on a comment within task1 "
        u"at 01/09/2018 23:31:42 PST:\n"
        u"One <a href=\"mailto:[email protected]\"></a>\n"
    )
    body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={
        "comments": [expected_body],
        "url": url,
    })
    send_email_mock.assert_called_once_with(u"*****@*****.**",
                                            expected_title, body)
Esempio n. 3
0
    def test_handle_one_comment(self, send_email_mock):
        """Test handling of mapped comment."""
        with factories.single_commit():
            person = factories.PersonFactory(email="*****@*****.**")
            obj = factories.ProductFactory(title="Product1")
            comment = factories.CommentFactory(
                description=u"One <a href=\"mailto:[email protected]\"></a>", )
            comment.modified_by_id = person.id
            comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
            url = urljoin(get_url_root(), utils.view_url_for(obj))

        people_mentions.handle_comment_mapped(obj, [comment])
        expected_title = (u"[email protected] mentioned you "
                          u"on a comment within Product1")
        expected_body = (
            u"[email protected] mentioned you on a comment within Product1 "
            u"at 01/09/2018 23:31:42 PST:\n"
            u"One <a href=\"mailto:[email protected]\"></a>\n")
        body = settings.EMAIL_MENTIONED_PERSON.render(
            person_mention={
                "comments": [expected_body],
                "url": url,
            })
        send_email_mock.assert_called_once_with(u"*****@*****.**",
                                                expected_title, body)
Esempio n. 4
0
    def test_handle_task_comment(self, send_email_mock):
        """Test handling of mapped comment to cycle task."""
        with factories.single_commit():
            person = factories.PersonFactory(email="*****@*****.**")
            obj = wf_factories.CycleTaskGroupObjectTaskFactory(
                slug=u"TSK-1",
                title=u"task1",
            )
            comment = factories.CommentFactory(
                description=u"One <a href=\"mailto:[email protected]\"></a>", )
            comment.modified_by_id = person.id
            comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
            url = "http://localhost/dashboard#!task&query=%22task%20slug%22%3DTSK-1"

        people_mentions.handle_comment_mapped(obj, [comment])
        expected_title = (u"[email protected] mentioned you "
                          u"on a comment within task1")
        expected_body = (
            u"[email protected] mentioned you on a comment within task1 "
            u"at 01/09/2018 23:31:42 PST:\n"
            u"One <a href=\"mailto:[email protected]\"></a>\n")
        body = settings.EMAIL_MENTIONED_PERSON.render(
            person_mention={
                "comments": [expected_body],
                "url": url,
            })
        send_email_mock.assert_called_once_with(u"*****@*****.**",
                                                expected_title, body)
Esempio n. 5
0
  def test_handle_one_comment(self, send_email_mock):
    """Test handling of mapped comment."""
    with factories.single_commit():
      person = factories.PersonFactory(email="*****@*****.**")
      obj = factories.ProductFactory(title="Product1")
      comment = factories.CommentFactory(
          description=u"One <a href=\"mailto:[email protected]\"></a>",
      )
      comment.modified_by_id = person.id
      comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
      url = urljoin(get_url_root(), utils.view_url_for(obj))

    people_mentions.handle_comment_mapped(obj, [comment])
    expected_title = (u"[email protected] mentioned you "
                      u"on a comment within Product1")
    expected_body = (
        u"[email protected] mentioned you on a comment within Product1 "
        u"at 01/09/2018 23:31:42 PST:\n"
        u"One <a href=\"mailto:[email protected]\"></a>\n"
    )
    body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={
        "comments": [expected_body],
        "url": url,
    })
    send_email_mock.assert_called_once_with(u"*****@*****.**",
                                            expected_title, body)
Esempio n. 6
0
    def send_comment_notifications(self):
        """Send comment people mentions notifications."""
        from ggrc.notifications import people_mentions

        if self.comments:
            people_mentions.handle_comment_mapped(obj=self.obj,
                                                  comments=self.comments)
Esempio n. 7
0
  def handle_assessment_put(_, obj, **kwargs):
    # pylint: disable=unused-argument
    """Handle Assessments with Comment mapping action"""
    from ggrc.notifications import people_mentions

    if not isinstance(obj, WithAction) or not isinstance(
        obj, (comment.Commentable, ChangeTracked)
    ):
      return

    actions = getattr(obj, '_actions')
    if not actions or 'add_related' not in actions:
      return

    saved_comments = list()
    for action in actions.get('add_related'):
      obj_type = action.get("type")
      obj_id = action.get("id")

      if obj_type != "Comment" or not obj_id:
        continue

      saved_comments.append(all_models.Comment.query.get(obj_id))

    people_mentions.handle_comment_mapped(obj=obj, comments=saved_comments)
Esempio n. 8
0
 def test_handle_empty_comment(self, send_email_mock):
   """Test handling of mapped comment with no mention."""
   with factories.single_commit():
     person = factories.PersonFactory(email="*****@*****.**")
     obj = factories.ProductFactory(title="Product2")
     comment = factories.CommentFactory(description=u"test")
     comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
     comment.modified_by_id = person.id
   people_mentions.handle_comment_mapped(obj, [comment])
   send_email_mock.assert_not_called()
Esempio n. 9
0
 def test_handle_empty_comment(self, send_email_mock):
     """Test handling of mapped comment with no mention."""
     with factories.single_commit():
         person = factories.PersonFactory(email="*****@*****.**")
         obj = factories.ProductFactory(title="Product2")
         comment = factories.CommentFactory(description=u"test")
         comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)
         comment.modified_by_id = person.id
     people_mentions.handle_comment_mapped(obj, [comment])
     send_email_mock.assert_not_called()
Esempio n. 10
0
    def test_person_mentioned_create(self, send_email_mock, *_):
        """Test that a user with authorized domain is created when mentioned."""
        with factories.single_commit():
            obj = factories.ProductFactory(title="Product6")
            comment = factories.CommentFactory(
                description=u"One <a href=\"mailto:[email protected]"
                u"\"></a>", )
            comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)

        people_mentions.handle_comment_mapped(obj, [comment])
        person = all_models.Person.query.filter_by(
            email="*****@*****.**").first()
        self.assertIsNotNone(person)
        send_email_mock.assert_called_once()
Esempio n. 11
0
  def handle_posted_after_commit(obj_class, obj, **kwargs):
    """Send people mentions on comment posted."""
    # pylint: disable=unused-argument
    from ggrc.notifications import people_mentions

    if (obj.source_type not in ("Comment", "ExternalComment") and
       obj.destination_type not in ("Comment", "ExternalComment")):
      return

    comment, other = obj.source, obj.destination
    if comment.type not in ("Comment", "ExternalComment"):
      comment, other = other, comment

    if isinstance(other, (Commentable, ExternalCommentable, ChangeTracked)):
      people_mentions.handle_comment_mapped(obj=other, comments=[comment])
Esempio n. 12
0
  def handle_posted_after_commit(obj_class, obj, **kwargs):
    """Send people mentions on comment posted."""
    # pylint: disable=unused-argument
    from ggrc.notifications import people_mentions

    if (obj.source_type not in ("Comment", "ExternalComment") and
       obj.destination_type not in ("Comment", "ExternalComment")):
      return

    comment, other = obj.source, obj.destination
    if comment.type not in ("Comment", "ExternalComment"):
      comment, other = other, comment

    if isinstance(other, (Commentable, ExternalCommentable, ChangeTracked)):
      people_mentions.handle_comment_mapped(obj=other, comments=[comment])
Esempio n. 13
0
  def test_person_mentioned_create(self, send_email_mock, *_):
    """Test that a user with authorized domain is created when mentioned."""
    with factories.single_commit():
      obj = factories.ProductFactory(title="Product6")
      comment = factories.CommentFactory(
          description=u"One <a href=\"mailto:[email protected]"
                      u"\"></a>",
      )
      comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42)

    people_mentions.handle_comment_mapped(obj, [comment])
    person = all_models.Person.query.filter_by(
        email="*****@*****.**"
    ).first()
    self.assertIsNotNone(person)
    send_email_mock.assert_called_once()
Esempio n. 14
0
def apply_mentions_proposal_comment(
    sender, obj=None, src=None, service=None,
    event=None, initial_state=None
):
  """Send people mentions in proposal comment."""
  if not event:
    return
  comment_revision = all_models.Revision.query.filter(
      all_models.Revision.resource_type == all_models.Comment.__name__,
      all_models.Revision.event_id == event.id,
  ).first()
  if not comment_revision:
    return
  comment_id = comment_revision.resource_id
  created_comment = all_models.Comment.query.get(comment_id)

  from ggrc.notifications import people_mentions
  people_mentions.handle_comment_mapped(obj=obj.instance,
                                        comments=[created_comment])
Esempio n. 15
0
  def apply_mentions_comment(event, obj):
    """Send people mentions in proposal comment.

      Args:
        event: event in which comment was added into session
        obj: object for which comment was created
    """
    if not event:
      return
    comment_revision = revision.Revision.query.filter(
        revision.Revision.resource_type == comment.Comment.__name__,
        revision.Revision.event_id == event.id,
    ).first()
    if not comment_revision:
      return
    comment_id = comment_revision.resource_id
    created_comment = comment.Comment.query.get(comment_id)

    from ggrc.notifications import people_mentions
    people_mentions.handle_comment_mapped(obj=obj, comments=[created_comment])
Esempio n. 16
0
def apply_mentions_proposal_comment(sender,
                                    obj=None,
                                    src=None,
                                    service=None,
                                    event=None,
                                    initial_state=None):
    """Send people mentions in proposal comment."""
    if not event:
        return
    comment_revision = all_models.Revision.query.filter(
        all_models.Revision.resource_type == all_models.Comment.__name__,
        all_models.Revision.event_id == event.id,
    ).first()
    if not comment_revision:
        return
    comment_id = comment_revision.resource_id
    created_comment = all_models.Comment.query.get(comment_id)

    from ggrc.notifications import people_mentions
    people_mentions.handle_comment_mapped(obj=obj.instance,
                                          comments=[created_comment])
Esempio n. 17
0
    def handle_comment_mapping(sender, objects=None, **kwargs):
        """Update Commentable.updated_at when Comment mapped."""
        # pylint: disable=unused-argument

        from ggrc.notifications import people_mentions

        for obj in objects:
            if (obj.source_type not in ("Comment", "ExternalComment")
                    and obj.destination_type
                    not in ("Comment", "ExternalComment")):
                continue

            comment, other = obj.source, obj.destination
            if comment.type not in ("Comment", "ExternalComment"):
                comment, other = other, comment

            if isinstance(other,
                          (Commentable, ExternalCommentable, ChangeTracked)):
                other.updated_at = datetime.utcnow()
                people_mentions.handle_comment_mapped(obj=other,
                                                      comments=[comment])