django-notifications/notifications/views.py

154 lines
4.4 KiB
Python
Raw Normal View History

2018-05-31 02:59:07 +00:00
# -*- coding: utf-8 -*-
2023-06-24 00:10:23 +00:00
""" Django Notifications example views """
2023-05-22 23:26:41 +00:00
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
2015-11-26 10:17:43 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
2023-05-24 10:48:32 +00:00
from django.utils.encoding import iri_to_uri
from django.utils.http import url_has_allowed_host_and_scheme
from django.views.decorators.cache import never_cache
from django.views.generic import ListView
2023-05-24 10:48:32 +00:00
from swapper import load_model
from notifications.helpers import get_notification_list
2023-07-11 23:28:23 +00:00
from notifications.settings import notification_settings
2023-06-24 00:10:23 +00:00
Notification = load_model("notifications", "Notification")
2015-11-26 10:17:12 +00:00
class NotificationViewList(ListView):
2023-06-24 00:10:23 +00:00
template_name = "notifications/list.html"
context_object_name = "notifications"
2023-07-11 23:28:23 +00:00
paginate_by = notification_settings.PAGINATE_BY
2015-11-26 10:17:12 +00:00
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
2023-06-24 00:10:23 +00:00
return super().dispatch(request, *args, **kwargs)
2015-11-26 10:17:12 +00:00
class AllNotificationsList(NotificationViewList):
"""
Index page for authenticated user
"""
2015-11-26 10:17:12 +00:00
def get_queryset(self):
2023-07-11 23:28:23 +00:00
if notification_settings.SOFT_DELETE:
qset = self.request.user.notifications.active()
2015-11-26 10:17:12 +00:00
else:
qset = self.request.user.notifications.all()
return qset
2014-10-14 01:31:53 +00:00
2015-11-26 10:17:12 +00:00
class UnreadNotificationsList(NotificationViewList):
def get_queryset(self):
return self.request.user.notifications.unread()
2014-10-14 01:31:53 +00:00
@login_required
def mark_all_as_read(request):
request.user.notifications.mark_all_as_read()
2014-10-14 01:31:53 +00:00
2023-06-24 00:10:23 +00:00
_next = request.GET.get("next")
2014-10-14 01:31:53 +00:00
2023-05-22 23:26:41 +00:00
if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS):
return redirect(iri_to_uri(_next))
2023-06-24 00:10:23 +00:00
return redirect("notifications:unread")
2015-11-26 10:17:43 +00:00
@login_required
def mark_as_read(request, slug=None):
2023-06-27 00:06:44 +00:00
notification_id = slug
2023-06-24 00:10:23 +00:00
notification = get_object_or_404(Notification, recipient=request.user, id=notification_id)
notification.mark_as_read()
2023-06-24 00:10:23 +00:00
_next = request.GET.get("next")
2023-05-22 23:26:41 +00:00
if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS):
return redirect(iri_to_uri(_next))
2023-06-24 00:10:23 +00:00
return redirect("notifications:unread")
2013-12-12 01:07:35 +00:00
2015-11-26 10:17:43 +00:00
2013-12-12 01:07:35 +00:00
@login_required
def mark_as_unread(request, slug=None):
2023-06-27 00:06:44 +00:00
notification_id = slug
2013-12-12 01:07:35 +00:00
2023-06-24 00:10:23 +00:00
notification = get_object_or_404(Notification, recipient=request.user, id=notification_id)
2013-12-12 01:07:35 +00:00
notification.mark_as_unread()
2023-06-24 00:10:23 +00:00
_next = request.GET.get("next")
2013-12-12 01:07:35 +00:00
2023-05-22 23:26:41 +00:00
if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS):
return redirect(iri_to_uri(_next))
2013-12-12 01:07:35 +00:00
2023-06-24 00:10:23 +00:00
return redirect("notifications:unread")
2014-10-14 01:31:53 +00:00
2014-10-14 01:31:53 +00:00
@login_required
def delete(request, slug=None):
2023-06-27 00:06:44 +00:00
notification_id = slug
2014-10-14 01:31:53 +00:00
2023-06-24 00:10:23 +00:00
notification = get_object_or_404(Notification, recipient=request.user, id=notification_id)
2015-11-26 10:17:43 +00:00
2023-07-11 23:28:23 +00:00
if notification_settings.SOFT_DELETE:
notification.deleted = True
notification.save()
else:
notification.delete()
2014-10-14 01:31:53 +00:00
2023-06-24 00:10:23 +00:00
_next = request.GET.get("next")
2014-10-14 01:31:53 +00:00
2023-05-22 23:26:41 +00:00
if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS):
return redirect(iri_to_uri(_next))
2014-10-14 01:31:53 +00:00
2023-06-24 00:10:23 +00:00
return redirect("notifications:all")
2015-11-26 10:17:43 +00:00
2019-09-18 11:43:50 +00:00
@never_cache
def live_unread_notification_count(request):
if not request.user.is_authenticated:
2023-06-24 00:10:23 +00:00
data = {"unread_count": 0}
else:
data = {
2023-06-24 00:10:23 +00:00
"unread_count": request.user.notifications.unread().count(),
}
2015-07-20 09:51:00 +00:00
return JsonResponse(data)
2015-11-26 10:17:43 +00:00
2019-09-18 11:43:50 +00:00
@never_cache
2015-07-20 09:51:00 +00:00
def live_unread_notification_list(request):
2023-06-24 00:10:23 +00:00
"""Return a json with a unread notification list"""
if not request.user.is_authenticated:
2023-06-24 00:10:23 +00:00
data = {"unread_count": 0, "unread_list": []}
return JsonResponse(data)
2015-11-26 10:17:43 +00:00
2023-06-24 00:10:23 +00:00
unread_list = get_notification_list(request, "unread")
2023-06-24 00:10:23 +00:00
data = {"unread_count": request.user.notifications.unread().count(), "unread_list": unread_list}
return JsonResponse(data)
2019-09-18 11:43:50 +00:00
@never_cache
def live_all_notification_list(request):
2023-06-24 00:10:23 +00:00
"""Return a json with a unread notification list"""
if not request.user.is_authenticated:
2023-06-24 00:10:23 +00:00
data = {"all_count": 0, "all_list": []}
return JsonResponse(data)
all_list = get_notification_list(request)
2023-06-24 00:10:23 +00:00
data = {"all_count": request.user.notifications.count(), "all_list": all_list}
return JsonResponse(data)
def live_all_notification_count(request):
if not request.user.is_authenticated:
2023-06-24 00:10:23 +00:00
data = {"all_count": 0}
else:
data = {
2023-06-24 00:10:23 +00:00
"all_count": request.user.notifications.count(),
}
return JsonResponse(data)