django-notifications/notifications/views.py

166 lines
4.4 KiB
Python
Raw Normal View History

2015-11-26 10:17:43 +00:00
from django import get_version
2015-07-20 09:51:00 +00:00
from django.conf import settings
from django.contrib.auth.decorators import login_required
2015-07-20 09:51:00 +00:00
from django.forms import model_to_dict
2015-11-26 10:17:43 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
2015-11-26 10:17:12 +00:00
from django.views.generic import ListView
2015-07-20 09:51:00 +00:00
from .utils import slug2id
from .models import Notification
from distutils.version import StrictVersion
if StrictVersion(get_version()) >= StrictVersion('1.7.0'):
from django.http import JsonResponse
else:
# Django 1.6 doesn't have a proper JsonResponse
import json
2015-07-20 23:11:04 +00:00
from django.http import HttpResponse
2015-11-26 10:17:43 +00:00
2015-07-21 00:07:48 +00:00
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
2015-11-26 10:17:43 +00:00
def JsonResponse(data):
2015-11-26 10:17:43 +00:00
return HttpResponse(
json.dumps(data, default=date_handler),
content_type="application/json")
2015-11-26 10:17:12 +00:00
class NotificationViewList(ListView):
template_name = 'notifications/list.html'
context_object_name = 'notifications'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(NotificationViewList, self).dispatch(
request, *args, **kwargs)
class AllNotificationsList(NotificationViewList):
"""
Index page for authenticated user
"""
2015-11-26 10:17:12 +00:00
def get_queryset(self):
if getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False):
qs = self.request.user.notifications.active()
else:
qs = self.request.user.notifications.all()
return qs
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
_next = request.GET.get('next')
2014-10-14 01:31:53 +00:00
if _next:
return redirect(_next)
return redirect('notifications:unread')
2015-11-26 10:17:43 +00:00
@login_required
def mark_as_read(request, slug=None):
id = slug2id(slug)
2015-11-26 10:17:43 +00:00
notification = get_object_or_404(
Notification, recipient=request.user, id=id)
notification.mark_as_read()
_next = request.GET.get('next')
if _next:
return redirect(_next)
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):
id = slug2id(slug)
2015-11-26 10:17:43 +00:00
notification = get_object_or_404(
Notification, recipient=request.user, id=id)
2013-12-12 01:07:35 +00:00
notification.mark_as_unread()
_next = request.GET.get('next')
2013-12-12 01:07:35 +00:00
if _next:
return redirect(_next)
2013-12-12 01:07:35 +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):
_id = slug2id(slug)
2014-10-14 01:31:53 +00:00
2015-11-26 10:17:43 +00:00
notification = get_object_or_404(
Notification, recipient=request.user, id=_id)
if getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False):
notification.deleted = True
notification.save()
else:
notification.delete()
2014-10-14 01:31:53 +00:00
_next = request.GET.get('next')
2014-10-14 01:31:53 +00:00
if _next:
return redirect(_next)
2014-10-14 01:31:53 +00:00
return redirect('notifications:all')
2015-11-26 10:17:43 +00:00
def live_unread_notification_count(request):
if not request.user.is_authenticated():
data = {'unread_count':0}
else:
data = {
'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
2015-07-20 09:51:00 +00:00
def live_unread_notification_list(request):
if not request.user.is_authenticated():
data = {
'unread_count':0,
'unread_list':[]
}
return JsonResponse(data)
2015-11-26 10:17:43 +00:00
2015-07-20 09:51:00 +00:00
try:
2015-11-26 10:17:43 +00:00
num_to_fetch = request.GET.get('max', 5) # If they don't specify, make it 5.
2015-07-20 09:51:00 +00:00
num_to_fetch = int(num_to_fetch)
2016-01-01 02:59:51 +00:00
num_to_fetch = max(1, num_to_fetch) # if num_to_fetch is negative, force at least one fetched notifications
num_to_fetch = min(num_to_fetch, 100) # put a sane ceiling on the number retrievable
2015-07-20 09:51:00 +00:00
except ValueError:
2015-11-26 10:17:43 +00:00
num_to_fetch = 5 # If casting to an int fails, just make it 5.
2015-07-20 09:51:00 +00:00
2015-11-11 11:04:12 +00:00
unread_list = []
for n in request.user.notifications.unread()[0:num_to_fetch]:
struct = model_to_dict(n)
if n.actor:
struct['actor'] = str(n.actor)
if n.target:
struct['target'] = str(n.target)
if n.action_object:
struct['action_object'] = str(n.action_object)
unread_list.append(struct)
2016-09-24 21:22:33 +00:00
if request.GET.get('mark_as_read'):
n.mark_as_read()
2015-07-20 09:51:00 +00:00
data = {
2016-01-01 02:59:51 +00:00
'unread_count': request.user.notifications.unread().count(),
'unread_list': unread_list
}
return JsonResponse(data)