Add notification
This commit is contained in:
parent
ab59065c0b
commit
de704fc250
17 changed files with 279 additions and 56 deletions
|
@ -13,9 +13,10 @@ from reversion import revisions
|
|||
from reversion.models import Version
|
||||
|
||||
from judge.dblock import LockModel
|
||||
from judge.models import Comment, CommentVote
|
||||
from judge.models import Comment, CommentVote, Notification
|
||||
from judge.utils.views import TitleMixin
|
||||
from judge.widgets import MathJaxPagedownWidget
|
||||
from judge.comments import add_mention_notifications, del_mention_notifications
|
||||
|
||||
__all__ = ['upvote_comment', 'downvote_comment', 'CommentEditAjax', 'CommentContent',
|
||||
'CommentEdit']
|
||||
|
@ -116,6 +117,11 @@ class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView):
|
|||
form_class = CommentEditForm
|
||||
|
||||
def form_valid(self, form):
|
||||
# update notifications
|
||||
comment = form.instance
|
||||
del_mention_notifications(comment)
|
||||
add_mention_notifications(comment)
|
||||
|
||||
with transaction.atomic(), revisions.create_revision():
|
||||
revisions.set_comment(_('Edited from site'))
|
||||
revisions.set_user(self.request.user)
|
||||
|
|
52
judge/views/notification.py
Normal file
52
judge/views/notification.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
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
|
||||
|
||||
__all__ = ['NotificationList']
|
||||
|
||||
class NotificationList(ListView):
|
||||
model = Notification
|
||||
context_object_name = 'notifications'
|
||||
template_name = 'notification/list.html'
|
||||
|
||||
def get_queryset(self):
|
||||
self.unseen_cnt = self.request.profile.count_unseen_notifications
|
||||
|
||||
query = {
|
||||
'owner': self.request.profile,
|
||||
'comment__hidden': False,
|
||||
}
|
||||
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
|
||||
|
||||
return self.queryset
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['unseen_count'] = self.unseen_cnt
|
||||
context['title'] = _('Notifications (%d unseen)' % context['unseen_count'])
|
||||
context['has_notifications'] = self.queryset.exists()
|
||||
context['page_titles'] = CacheDict(lambda page: Comment.get_page_title(page))
|
||||
return context
|
||||
|
||||
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)
|
||||
|
||||
return ret
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue