3

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'
ashes999
  • 1,234
  • 1
  • 12
  • 36
  • Your question should really be about signals, not notifications, but between how and when is page2 or rather view2 called. – unlockme Oct 04 '20 at 09:51
  • So how do you suggest I disconnect a signal with those certain parameters anytime in my view. View2 can be called anytime at any point in my application, I want to be able to remove the created signal with those objects anytime. @unlockme – ashes999 Oct 04 '20 at 18:00
  • Look at this https://github.com/django-notifications/django-notifications#generating-notifications information #Notifications are best done in a separate signal. This way you can disconnect that handler.```def my_handler(sender, instance, created, **kwargs): notify.send(instance, verb='was saved') request_finished.connect(my_handler), request_finished.disconnect(my_handler)``` Try that. – unlockme Oct 05 '20 at 12:50
  • @unlockme thanks for your information, but unfortunately I cannot do it, I cannot find a way to disconnect the handler for specific notification argument. I made an example on how I send my notifications now – ashes999 Oct 05 '20 at 19:07
  • 2
    It appears to me, when you say disconnect notification. You are meaning, you want the notifications that has been created to not appear for the user. I hope you are aware that notify.send actually creates a new notification instance in the database. In that case, you can mark the specific notification as read. @ashes999 – unlockme Oct 05 '20 at 19:20
  • I have tried gettings the notification with `request.user.notifications.get(...)` however, everytime I get a model error: ` DoesNotExists` So I am not sure what fields to use since I basically tried everything. But yes my intention is to delete a notification or make it invisible to the user. – ashes999 Oct 05 '20 at 22:43
  • Okay, you are progressing. Now throw the assumptions aside and see what exists in ```request.user.notifications.all()```. Then you will be able to make a decision on how to proceed. – unlockme Oct 06 '20 at 08:33
  • @unlockme I decided to take a new approach and create a new field, however, I am still struggling as the new field is not being added and always returns null, I have created a new question for that. – ashes999 Oct 07 '20 at 18:06
  • Probably not a good idea, sometimes in writing code, you have to through assumptions aside and see what is actually happening inside the code. What did ```notifications.all()``` result in @ashes999. – unlockme Oct 07 '20 at 18:18
  • @unlockme it returned the notifications except that my custom field is now always None, the question is here:https://stackoverflow.com/questions/64249745/adding-custom-field-to-django-notifications-always-returning-none-on-notify-send – ashes999 Oct 07 '20 at 18:25
  • @unlockme okay, now I have noticed what's the issue however, the 'actor_content_type' and 'target_content_type' field seem to be the issue, I do not know how to assign those properly, because my when I use `get()` it returns `DoesNotExist` error – ashes999 Oct 07 '20 at 19:57

0 Answers0