def notify_comment(**kwargs): # pragma: no cover """Handler to be fired up upon comments signal to notify the author of a given article.""" actor = kwargs["request"].user receiver = kwargs["comment"].content_object.user obj = kwargs["comment"].content_object create_notification_handler( actor, receiver, Notification.COMMENTED, action_object=obj )
def test_single_notification(self): Notification.objects.mark_all_as_read() obj = News.objects.create(user=self.user, content="This is a short content.") create_notification_handler(self.user, self.other_user, "C", action_object=obj) assert Notification.objects.unread().count() == 1
def follow_user(request, user_id): """ Ajax call to follow a user. """ user = get_object_or_404(User, id=user_id) if request.user in user.followers.all(): user.followers.remove(request.user) text = 'Follow' else: user.followers.add(request.user) create_notification_handler(request.user, user, Notification.FOLLOW, key="social_update") text = 'Unfollow' return HttpResponse(text)
def send_message_request(request, user_id): """ Ajax call to send a message request. """ receiver = get_object_or_404(User, id=user_id) contacter = request.user if contacter in receiver.pending_list.all(): receiver.pending_list.remove(contacter) text = 'Send Request' else: receiver.pending_list.add(contacter) create_notification_handler(contacter, receiver, Notification.FRIEND_REQUEST, key="social_update") text = 'Request Sent' return HttpResponse(text)
def accept_message_request(request, user_id): """ Ajax call to accept a message request. """ sender = get_object_or_404(User, id=user_id) acceptor = request.user if sender in acceptor.pending_list.all(): acceptor.pending_list.remove(sender) acceptor.contact_list.add(sender) sender.contact_list.add(acceptor) create_notification_handler(acceptor, sender, Notification.FRIEND_ACCEPT, key="social_update") text = 'Added to contact list' else: text = 'Unexpected error!' return HttpResponse(text)
def reply_this(self, user, text): """Handler function to create a News instance as a reply to any published news. :requires: :param user: The logged in user who is doing the reply. :param text: String with the reply. """ parent = self.get_parent() reply_news = News.objects.create( user=user, content=text, reply=True, parent=parent ) if self.user != user: create_notification_handler( user, parent.user, Notification.REPLY, action_object=reply_news.parent, id_value=str(parent.uuid_id), key="social_update", )
def switch_like(self, user): if user in self.liked.all(): self.liked.remove(user) if self.user != user: delete_notification_handler( user, self.user, Notification.LIKED, action_object=self, id_value=str(self.uuid_id), key="social_update", ) else: self.liked.add(user) if self.user != user: create_notification_handler( user, self.user, Notification.LIKED, action_object=self, id_value=str(self.uuid_id), key="social_update", )
def test_list_notification(self): Notification.objects.mark_all_as_read() create_notification_handler(self.user, [self.user, self.other_user], "C") assert Notification.objects.unread().count() == 2
def test_global_notification(self): Notification.objects.mark_all_as_read() create_notification_handler(self.user, "global", "C") assert Notification.objects.unread().count() == 1
def broadcast_logout(sender, user, request, **kwargs): """Handler to be fired up upon user logout signal to notify all users.""" create_notification_handler(user, "global", Notification.LOGGED_OUT)