Change notification backend

This commit is contained in:
cuom1999 2023-10-10 17:38:48 -05:00
parent 5f97491f0d
commit 7f854c40dd
15 changed files with 188 additions and 134 deletions

View file

@ -27,7 +27,7 @@ from judge.dblock import LockModel
from judge.models import Comment, CommentVote, Notification, BlogPost
from judge.utils.views import TitleMixin
from judge.widgets import MathJaxPagedownWidget, HeavyPreviewPageDownWidget
from judge.comments import add_mention_notifications, del_mention_notifications
from judge.comments import add_mention_notifications
import json
@ -240,7 +240,6 @@ class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView):
def form_valid(self, form):
# update notifications
comment = form.instance
del_mention_notifications(comment)
add_mention_notifications(comment)
with transaction.atomic(), revisions.create_revision():

View file

@ -2,10 +2,9 @@ from django.contrib.auth.decorators import login_required
from django.views.generic import ListView
from django.utils.translation import ugettext as _
from django.utils.timezone import now
from django.db.models import BooleanField, Value
from judge.utils.cachedict import CacheDict
from judge.models import Profile, Comment, Notification
from judge.models import Profile, Notification, NotificationProfile
from judge.models.notification import unseen_notifications_count
__all__ = ["NotificationList"]
@ -16,24 +15,11 @@ class NotificationList(ListView):
template_name = "notification/list.html"
def get_queryset(self):
self.unseen_cnt = self.request.profile.count_unseen_notifications
self.unseen_cnt = unseen_notifications_count(self.request.profile)
query = {
"owner": self.request.profile,
}
self.queryset = (
Notification.objects.filter(**query)
.order_by("-time")[:100]
.annotate(seen=Value(True, output_field=BooleanField()))
)
# Mark the several first unseen
for cnt, q in enumerate(self.queryset):
if cnt < self.unseen_cnt:
q.seen = False
else:
break
self.queryset = Notification.objects.filter(
owner=self.request.profile
).order_by("-id")[:100]
return self.queryset
@ -46,8 +32,6 @@ class NotificationList(ListView):
def get(self, request, *args, **kwargs):
ret = super().get(request, *args, **kwargs)
# update after rendering
Notification.objects.filter(owner=self.request.profile).update(read=True)
NotificationProfile.objects.filter(user=request.profile).update(unread_count=0)
unseen_notifications_count.dirty(self.request.profile)
return ret

View file

@ -56,10 +56,10 @@ from judge.models import (
Problem,
Profile,
Contest,
Notification,
ContestProblem,
OrganizationProfile,
)
from judge.models.notification import make_notification
from judge import event_poster as event
from judge.utils.ranker import ranker
from judge.utils.views import (
@ -1019,16 +1019,9 @@ class AddOrganizationBlog(
html = (
f'<a href="{link}">{self.object.title} - {self.organization.name}</a>'
)
for user in self.organization.admins.all():
if user.id == self.request.profile.id:
continue
notification = Notification(
owner=user,
author=self.request.profile,
category="Add blog",
html_link=html,
)
notification.save()
make_notification(
self.organization.admins.all(), "Add blog", html, self.request.profile
)
return res
@ -1104,17 +1097,8 @@ class EditOrganizationBlog(
)
html = f'<a href="{link}">{blog.title} - {self.organization.name}</a>'
post_authors = blog.authors.all()
posible_user = self.organization.admins.all() | post_authors
for user in posible_user:
if user.id == self.request.profile.id:
continue
notification = Notification(
owner=user,
author=self.request.profile,
category=action,
html_link=html,
)
notification.save()
posible_users = self.organization.admins.all() | post_authors
make_notification(posible_users, action, html, self.request.profile)
def form_valid(self, form):
with transaction.atomic(), revisions.create_revision():

View file

@ -80,6 +80,7 @@ def vote_page(request, delta):
else:
PageVote.objects.filter(id=pagevote_id).update(score=F("score") + delta)
break
_dirty_vote_score(pagevote_id, request.profile)
return HttpResponse("success", content_type="text/plain")
@ -103,3 +104,8 @@ class PageVoteDetailView(TemplateResponseMixin, SingleObjectMixin, View):
context = super(PageVoteDetailView, self).get_context_data(**kwargs)
context["pagevote"] = self.object.get_or_create_pagevote()
return context
def _dirty_vote_score(pagevote_id, profile):
pv = PageVote(id=pagevote_id)
pv.vote_score.dirty(pv, profile)

View file

@ -49,16 +49,10 @@ ticket_widget = (
def add_ticket_notifications(users, author, link, ticket):
html = f'<a href="{link}">{ticket.linked_item}</a>'
users = set(users)
if author in users:
users.remove(author)
for user in users:
notification = Notification(
owner=user, html_link=html, category="Ticket", author=author
)
notification.save()
make_notification(users, "Ticket", html, author)
class TicketForm(forms.Form):