NDOJ/judge/views/notification.py

54 lines
1.6 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
from django.db.models import BooleanField, Value
from judge.utils.cachedict import CacheDict
from judge.models import Profile, Comment, Notification
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):
self.unseen_cnt = self.request.profile.count_unseen_notifications
2022-05-14 17:57:27 +00:00
2020-07-03 02:50:31 +00:00
query = {
2022-05-14 17:57:27 +00:00
"owner": self.request.profile,
2020-07-03 02:50:31 +00:00
}
2020-10-20 03:54:13 +00:00
2022-05-14 17:57:27 +00:00
self.queryset = (
Notification.objects.filter(**query)
.order_by("-time")[:100]
.annotate(seen=Value(True, output_field=BooleanField()))
)
2020-07-03 02:50:31 +00:00
# Mark the several first unseen
for cnt, q in enumerate(self.queryset):
2022-05-14 17:57:27 +00:00
if cnt < self.unseen_cnt:
2020-07-03 02:50:31 +00:00
q.seen = False
else:
break
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)
2022-05-14 17:57:27 +00:00
2020-07-03 02:50:31 +00:00
# update after rendering
Notification.objects.filter(owner=self.request.profile).update(read=True)
2022-05-14 17:57:27 +00:00
return ret