Removing Django notification by disconnecting signal in django-notifications-hq

684 views Asked by At

I am intending to remove a notification in the django-notifications-hq package by disconnecting the signal in my views after a signal is created:

As an example:

def view1:
    notify.send(sender=user, recipient=other_user, verb=message, target=object)
    return redirect('app:page')

def view2:
    notify.disconnect(sender=user, reciever=other_user)
    return redirect('app:page2')

user and other_user are the same users in this example

This will disconnect all signals between user and other_user, what I intend to do is to only disconnect the signal created between those users for that specific object.

I have already looked into the source code and I cannot find how I can manage to do such a thing.

For your reference, the GitHub link is: https://github.com/django-notifications/django-notifications

Also this is the Signals file in that package:

''' Django notifications signal file '''
# -*- coding: utf-8 -*-
from django.dispatch import Signal

notify = Signal(providing_args=[  # pylint: disable=invalid-name
    'recipient', 'actor', 'verb', 'action_object', 'target', 'description',
    'timestamp', 'level'
])

EDIT

Here is a more clarified example of what I am trying to achieve:

in my app/views.py

I have:

def post_like(request, id):
  
   post = get_objects_or_404(Post, id=id)
   if request.user in post.likes:
      post.likes.remove(request.user)
      # remove notification sent to post.author here
   elif request.user not in post.likes:
      post.likes.add(request.user)
      notify.send(sender=request.user, recipient=post.author, verb="Liked your post", target = post)
      """
      Continue other functions here
      """

How can I connnect and disconnect the signals for this view? I cannot find an example in the documentation.

EDIT

I am trying to get the specific notification according to the notification model field:

request.user.notifications.get(actor_content_type__model='Profile', actor_object_id=request.user.id, target_content_type__model='home.Post', target_object_id=post.id, recipient=post.author, verb=message)

I got an error:

raise self.model.DoesNotExist(
main.models.Notification.DoesNotExist: Notification matching query does not exist.

After checking out more I noticed:

AttributeError: 'Notification' object has no attribute 'actor_content_type__model'
0

There are 0 answers