Saturday, 28 November 2020

Adding If statement to signals not working properly

I have set a Notification system for my Django project whenever a user likes or comments on a post the Author receives a notification about this activity.

I have read about the Signals in django: https://docs.djangoproject.com/en/3.1/topics/signals/#listening-to-signals

In the Post Model, I have added a num_likes which reflects the number of likes the post has received.

I am trying to add an option so that the Author of the Post can receive a Notification when the num_likes reaches a certain number. In my example, it is the First Like.

So, here is what I have tried but nothing happened.

Here is the models.py

class Post(models.Model):
    title = models.CharField(max_length=100, unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author')
    num_likes = models.IntegerField(default=0, verbose_name='No. of Likes')
    likes = models.ManyToManyField(User, related_name='liked', blank=True)

    def __str__(self):
        return self.title
#---------------------------My trial------------------------------------------
    def like_progress(sender, instance, *args, **kwargs):
        post = instance
        if post.num_likes == 1:
            notify = Notification(post=post, user=post.author, notification_type=3)
            notify.save()

# num_likes
post_save.connect(Post.like_progress, sender=Post)
#---------------------------My trial------------------------------------------

Here are the notifications model.py

class Notification(models.Model):
    NOTIFICATION_TYPES=((1,'Like'),(2,'Comment'),(3,'Admin'))

    post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name="noti_post", blank=True, null=True)
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
    user = models.ForeignKey(User, on_delete=models.CASCADE,related_name="noti_to_user")
    notification_type= models.IntegerField(choices=NOTIFICATION_TYPES)
    text_preview= models.CharField(max_length=90,blank=True)
    date=models.DateTimeField(auto_now=True)
    is_seen=models.BooleanField(default=False)

    def __str__(self):
        return self.notification_type

here are the notifications app views.py

def ShowNotifications(request, *args, **kwargs):
    user=request.user
    notifications= Notification.objects.filter(user=user).order_by('-date')
    Notification.objects.filter(user=user, is_seen=False).update(is_seen=True)

    template= loader.get_template('notifications/notifications.html')

    context = {
        'notifications': notifications,
    }

    return HttpResponse(template.render(context, request))

here is the url.py

app_name = 'notifications'

urlpatterns = [
    path('', ShowNotifications, name='show-notifications'),

here is the template:

<!-- Admin Notification -->

<!-- Admin Notification -->

So to summarize:

I have a perfectly working Notification system I am just trying to add to it an option to notify the Author of a Post that he received a first like for example. What am I doing wrong in the above and how can I get this feature to work?

If there is anything vague or more information required please ask



from Adding If statement to signals not working properly

No comments:

Post a Comment