NDOJ/judge/views/notification.py

38 lines
1.3 KiB
Python
Raw 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
2023-10-10 22:38:48 +00:00
from judge.models import Profile, Notification, NotificationProfile
from judge.models.notification import unseen_notifications_count
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
class NotificationList(ListView):
model = Notification
2022-05-14 17:57:27 +00:00
context_object_name = "notifications"
template_name = "notification/list.html"
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
).order_by("-id")[:100]
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"]
2022-05-14 17:57:27 +00:00
context["has_notifications"] = self.queryset.exists()
2020-07-03 02:50:31 +00:00
return context
def get(self, request, *args, **kwargs):
ret = super().get(request, *args, **kwargs)
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