def send_notifications(self, obj, history=None): """ Shortcut method for resources with special save cases on actions methods that not uses standard `post_save` hook of drf resources. """ if history is None: history = self.get_last_history() # If not history found, or it is empty. Do notthing. if not history: return if self._not_notify: return obj = self.get_object_for_snapshot(obj) # Process that analizes the corresponding diff and # some text fields for extract mentions and add them # to watchers before obtain a complete list of # notifiable users. services.analize_object_for_watchers(obj, history.comment, history.owner) # Get a complete list of notifiable users for current # object and send the change notification to them. services.send_notifications(obj, history=history)
def test_analize_object_for_watchers_adding_owner_non_empty_comment(): user1 = f.UserFactory.create() issue = MagicMock() issue.description = "Foo" issue.content = "" history = MagicMock() history.comment = "Comment" history.owner = user1 services.analize_object_for_watchers(issue, history.comment, history.owner) assert issue.add_watcher.call_count == 1
def test_analize_object_for_watchers(): user1 = f.UserFactory.create() user2 = f.UserFactory.create() issue = MagicMock() issue.description = "Foo @{0} @{1} ".format(user1.username, user2.username) issue.content = "" history = MagicMock() history.comment = "" services.analize_object_for_watchers(issue, history.comment, history.owner) assert issue.add_watcher.call_count == 2
def _create_wiki_page_when_create_wiki_link_if_not_exist(self, request, wiki_link): try: self.check_permissions(request, "create_wiki_page", wiki_link) except exc.PermissionDenied: # Create only the wiki link because the user doesn't have permission. pass else: # Create the wiki link and the wiki page if not exist. wiki_page, created = models.WikiPage.objects.get_or_create( slug=wiki_link.href, project=wiki_link.project, defaults={"owner": self.request.user, "last_modifier": self.request.user}) if created: # Creaste the new history entre, sSet watcher for the new wiki page # and send notifications about the new page created history = take_snapshot(wiki_page, user=self.request.user) analize_object_for_watchers(wiki_page, history.comment, history.owner) send_notifications(wiki_page, history=history)
def persist_history_snapshot(self, obj=None, delete: bool = False): """ Shortcut for resources with special save/persist logic. """ user = self.request.user comment = self.request.DATA.get("comment", "") if obj is None: obj = self.get_object() sobj = self.get_object_for_snapshot(obj) if sobj != obj and delete: delete = False notifications_services.analize_object_for_watchers(obj, comment, user) self.__last_history = take_snapshot(sobj, comment=comment, user=user, delete=delete) self.__object_saved = True
def persist_history_snapshot(self, obj=None, delete:bool=False): """ Shortcut for resources with special save/persist logic. """ user = self.request.user comment = "" if isinstance(self.request.DATA, dict): comment = self.request.DATA.get("comment", "") if obj is None: obj = self.get_object() sobj = self.get_object_for_snapshot(obj) if sobj != obj: delete = False notifications_services.analize_object_for_watchers(obj, comment, user) self.__last_history = take_snapshot(sobj, comment=comment, user=user, delete=delete) self.__object_saved = True
def _create_wiki_page_when_create_wiki_link_if_not_exist( self, request, wiki_link): try: self.check_permissions(request, "create_wiki_page", wiki_link) except exc.PermissionDenied: # Create only the wiki link because the user doesn't have permission. pass else: # Create the wiki link and the wiki page if not exist. wiki_page, created = models.WikiPage.objects.get_or_create( slug=wiki_link.href, project=wiki_link.project, defaults={ "owner": self.request.user, "last_modifier": self.request.user }) if created: # Creaste the new history entre, sSet watcher for the new wiki page # and send notifications about the new page created history = take_snapshot(wiki_page, user=self.request.user) analize_object_for_watchers(wiki_page, history.comment, history.owner) send_notifications(wiki_page, history=history)