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
|
2012-07-22 13:14:44 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2023-06-22 23:42:40 +00:00
|
|
|
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
|
2020-03-02 06:31:19 +00:00
|
|
|
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
|
2020-04-11 19:47:43 +00:00
|
|
|
|
2023-06-24 00:10:23 +00:00
|
|
|
Notification = load_model("notifications", "Notification")
|
2018-05-31 02:55:54 +00:00
|
|
|
|
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):
|
2012-07-22 13:14:44 +00:00
|
|
|
"""
|
|
|
|
|
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:
|
2018-05-31 02:55:54 +00:00
|
|
|
qset = self.request.user.notifications.active()
|
2015-11-26 10:17:12 +00:00
|
|
|
else:
|
2018-05-31 02:55:54 +00:00
|
|
|
qset = self.request.user.notifications.all()
|
|
|
|
|
return qset
|
2012-07-22 13:14:44 +00:00
|
|
|
|
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()
|
2012-07-22 13:14:44 +00:00
|
|
|
|
2014-10-14 01:31:53 +00:00
|
|
|
|
2012-10-24 23:12:32 +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")
|
2012-07-22 13:14:44 +00:00
|
|
|
|
2015-11-26 10:17:43 +00:00
|
|
|
|
2012-07-22 13:14:44 +00:00
|
|
|
@login_required
|
2012-10-24 23:12:32 +00:00
|
|
|
def mark_as_read(request, slug=None):
|
2023-06-27 00:06:44 +00:00
|
|
|
notification_id = slug
|
2012-07-22 13:14:44 +00:00
|
|
|
|
2023-06-24 00:10:23 +00:00
|
|
|
notification = get_object_or_404(Notification, recipient=request.user, id=notification_id)
|
2012-07-22 13:14:44 +00:00
|
|
|
notification.mark_as_read()
|
|
|
|
|
|
2023-06-24 00:10:23 +00:00
|
|
|
_next = request.GET.get("next")
|
2012-07-22 13:14:44 +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))
|
2012-07-22 13:14:44 +00:00
|
|
|
|
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
|
|
|
|
2015-04-11 05:28:52 +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:
|
2015-04-11 05:28:52 +00:00
|
|
|
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-07-19 14:35:34 +00:00
|
|
|
|
2015-11-26 10:17:43 +00:00
|
|
|
|
2019-09-18 11:43:50 +00:00
|
|
|
@never_cache
|
2015-07-19 14:35:34 +00:00
|
|
|
def live_unread_notification_count(request):
|
2023-06-22 23:42:40 +00:00
|
|
|
if not request.user.is_authenticated:
|
2023-06-24 00:10:23 +00:00
|
|
|
data = {"unread_count": 0}
|
2016-02-08 03:54:35 +00:00
|
|
|
else:
|
|
|
|
|
data = {
|
2023-06-24 00:10:23 +00:00
|
|
|
"unread_count": request.user.notifications.unread().count(),
|
2016-02-08 03:54:35 +00:00
|
|
|
}
|
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"""
|
2023-06-22 23:42:40 +00:00
|
|
|
if not request.user.is_authenticated:
|
2023-06-24 00:10:23 +00:00
|
|
|
data = {"unread_count": 0, "unread_list": []}
|
2016-02-08 03:54:35 +00:00
|
|
|
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")
|
2020-03-02 06:31:19 +00:00
|
|
|
|
2023-06-24 00:10:23 +00:00
|
|
|
data = {"unread_count": request.user.notifications.unread().count(), "unread_list": unread_list}
|
2015-07-19 14:35:34 +00:00
|
|
|
return JsonResponse(data)
|
2018-06-08 02:37:51 +00:00
|
|
|
|
|
|
|
|
|
2019-09-18 11:43:50 +00:00
|
|
|
@never_cache
|
2018-06-08 02:37:51 +00:00
|
|
|
def live_all_notification_list(request):
|
2023-06-24 00:10:23 +00:00
|
|
|
"""Return a json with a unread notification list"""
|
2023-06-22 23:42:40 +00:00
|
|
|
if not request.user.is_authenticated:
|
2023-06-24 00:10:23 +00:00
|
|
|
data = {"all_count": 0, "all_list": []}
|
2018-06-08 02:37:51 +00:00
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
2020-03-02 06:31:19 +00:00
|
|
|
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}
|
2018-06-08 02:37:51 +00:00
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def live_all_notification_count(request):
|
2023-06-22 23:42:40 +00:00
|
|
|
if not request.user.is_authenticated:
|
2023-06-24 00:10:23 +00:00
|
|
|
data = {"all_count": 0}
|
2018-06-08 02:37:51 +00:00
|
|
|
else:
|
|
|
|
|
data = {
|
2023-06-24 00:10:23 +00:00
|
|
|
"all_count": request.user.notifications.count(),
|
2018-06-08 02:37:51 +00:00
|
|
|
}
|
|
|
|
|
return JsonResponse(data)
|