NDOJ/judge/views/notification.py

43 lines
1.5 KiB
Python
Raw Permalink Normal View History

2020-07-03 02:50:31 +00:00
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
2024-04-27 01:55:24 +00:00
from django.http import Http404
2020-07-03 02:50:31 +00:00
2023-10-10 22:38:48 +00:00
from judge.models import Profile, Notification, NotificationProfile
from judge.models.notification import unseen_notifications_count
2024-04-27 01:55:24 +00:00
from judge.utils.infinite_paginator import InfinitePaginationMixin
2020-07-03 02:50:31 +00:00
2022-05-14 17:57:27 +00:00
__all__ = ["NotificationList"]
2020-07-03 02:50:31 +00:00
2024-04-27 01:55:24 +00:00
class NotificationList(InfinitePaginationMixin, ListView):
2020-07-03 02:50:31 +00:00
model = Notification
2022-05-14 17:57:27 +00:00
context_object_name = "notifications"
template_name = "notification/list.html"
2024-04-27 01:55:24 +00:00
paginate_by = 50
2022-05-14 17:57:27 +00:00
2020-07-03 02:50:31 +00:00
def get_queryset(self):
2023-10-10 22:38:48 +00:00
self.unseen_cnt = unseen_notifications_count(self.request.profile)
2022-05-14 17:57:27 +00:00
2023-10-10 22:38:48 +00:00
self.queryset = Notification.objects.filter(
owner=self.request.profile
2024-04-27 01:55:24 +00:00
).order_by("-id")
2022-05-14 17:57:27 +00:00
2020-07-03 02:50:31 +00:00
return self.queryset
2022-05-14 17:57:27 +00:00
2020-07-03 02:50:31 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["unseen_count"] = self.unseen_cnt
2023-04-03 16:55:13 +00:00
context["title"] = _("Notifications (%d unseen)") % context["unseen_count"]
2024-04-27 01:55:24 +00:00
context["first_page_href"] = "."
2020-07-03 02:50:31 +00:00
return context
def get(self, request, *args, **kwargs):
ret = super().get(request, *args, **kwargs)
2024-04-27 01:55:24 +00:00
if not request.user.is_authenticated:
raise Http404()
2023-10-10 22:38:48 +00:00
NotificationProfile.objects.filter(user=request.profile).update(unread_count=0)
unseen_notifications_count.dirty(self.request.profile)
2022-05-14 17:57:27 +00:00
return ret