From 6047d292dcb9c5ea4c801b4255baff010f1c4625 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Tue, 17 May 2022 22:34:08 -0500 Subject: [PATCH] Add notif for comment page author --- judge/comments.py | 16 ++++++++++++++-- judge/models/comment.py | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/judge/comments.py b/judge/comments.py index 2b7579a..fdd0a81 100644 --- a/judge/comments.py +++ b/judge/comments.py @@ -131,10 +131,22 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): # add notification for reply if comment.parent and comment.parent.author != comment.author: - notification_rep = Notification( + notification_reply = Notification( owner=comment.parent.author, comment=comment, category="Reply" ) - notification_rep.save() + notification_reply.save() + + # add notification for page authors + page_authors = comment.page_object.authors.all() + for user in page_authors: + if user == comment.author: + continue + notification = Notification( + owner=user, comment=comment, category="Comment" + ) + notification.save() + # except Exception: + # pass add_mention_notifications(comment) diff --git a/judge/models/comment.py b/judge/models/comment.py index a4ffc57..c03df7e 100644 --- a/judge/models/comment.py +++ b/judge/models/comment.py @@ -15,7 +15,7 @@ from reversion.models import Version from judge.models.contest import Contest from judge.models.interface import BlogPost -from judge.models.problem import Problem +from judge.models.problem import Problem, Solution from judge.models.profile import Profile from judge.utils.cachedict import CacheDict @@ -171,6 +171,22 @@ class Comment(MPTTModel): def get_absolute_url(self): return "%s#comment-%d" % (self.link, self.id) + @cached_property + def page_object(self): + try: + page = self.page + if page.startswith("p:"): + return Problem.objects.get(code=page[2:]) + elif page.startswith("c:"): + return Contest.objects.get(key=page[2:]) + elif page.startswith("b:"): + return BlogPost.objects.get(id=page[2:]) + elif page.startswith("s:"): + return Solution.objects.get(problem__code=page[2:]) + return None + except ObjectDoesNotExist: + return None + def __str__(self): return "%(page)s by %(user)s" % { "page": self.page,