0

enter image description herei'm sending notifications to a user via django notifications.. and i have username regex working on the html so anyone posts with @username it wil post and the html is linkable so click on the @username it will take hie to the username profile page. Now i am using django signals to match the username and print out the username. but when i use notify to send the notification. i cant't find the recipient (the user who will get the notification). its giving me a error ValueError at /post/new/ Cannot assign "<_sre.SRE_Match object; span=(0, 4), match='@boy'>": "Notification.recipient" must be a "User" instance.

my models.py:

class post(models.Model):
parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_pics', null=True, blank=True)
video = models.FileField(upload_to='post_videos', null=True, blank=True)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)

objects = postManager()

def __str__(self):
    return self.title

class Meta:
    ordering = ['-date_posted', 'title']

def get_absolute_url(self):
        return reverse ('blog-home')

def total_likes(self):
    return self.likes.count()


def post_save_receiver(sender, instance, created, *args,**kwargs):
    if created and not instance.parent:
        user_regex = r'@(?P<username>[\w.@+-]+)'
        m = re.search(user_regex, instance.content)
    if m:
        username = m.group("username")

        notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

post_save.connect(post_save_receiver, sender=post)

1 Answers1

0

The recipient can not be a Match object:

notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

You can try to obtain the User object with:

if m:
    try:
        recipient = User.objects.get(username=m.group('username'))
    except (User.DoesNotExist, User.MultipleObjectsReturned):
        pass
    else:
        notify.send(instance.author, recipient=recipient, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • ONE MORE QUESTION: I have notification urls in my projects directry. But i'm getting some error about the url. i mean mysites url(for notification because i'm using django notifiactions): ` re_path('^inbox/notifications/', include(notifications.urls, namespace='notifications')), ` and my base.html where i link the urls in navbnar: ` Profile notifications ` the error says ` Reverse for 'notifications' not found. ` @WillemVanOnsem – Cavin blake Feb 27 '20 at 11:51
  • Since it has a namespace, it should likely be `{% url 'notifications:notifications' %}`. But the part after the colon depends on the specific view in your notifications. – Willem Van Onsem Feb 27 '20 at 11:54
  • In the answer it is giving me one username if i type multiple usernames. Sending the same signals if they are tagged. I was thinking to replace the search with findall. Will it just work @WillemVanOnsem – Cavin blake Feb 27 '20 at 12:25
  • 1
    @Cavinblake: normally yes. Of course you need to *iterate* over the matches. – Willem Van Onsem Feb 27 '20 at 16:21
  • hi. One more thing, as i told you that the signal only working for one user. If i use two or three username it only sends the signal to the first username and others two users didn't getting any signals at all. Specifically when i put only one username it sends the signal to that username, but if i put two or three username(username username username) it only sends the signal to the first username and didn't send the others. – Cavin blake May 17 '21 at 08:32
  • can you look at it? Your previous answer on this question was very helpful for me. the question is that the signal only working for one user. If i use two or three usernames it only sends the signal to the first username and the other two users didn't get any signals at all. Specifically, when I put only one username it sends the signal to that username, but if I put two or three usernames (username username username) it only sends the signal to the first username and didn't send the others. – Cavin blake Jun 29 '21 at 12:33