Newer
Older
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.forms import model_to_dict
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect
from .models import Notification
from .utils import id2slug, slug2id
def is_test_user(user):
return user.groups.filter(name='Testers').exists()
def forward(request, slug):
"""
Open the url of the target object of the notification and redirect.
In addition, mark the notification as read.
"""
notification = get_object_or_404(Notification, recipient=request.user, id=slug2id(slug))
notification.mark_as_read()
return redirect(notification.target.get_absolute_url())
@user_passes_test(is_test_user)
def mark_toggle(request, slug=None):
id = slug2id(slug)
notification = get_object_or_404(Notification, recipient=request.user, id=id)
_next = request.GET.get('next')
if _next:
return redirect(_next)
if request.GET.get('json'):
return JsonResponse({'unread': notification.unread})
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()}
return JsonResponse(data)
if not request.user.is_authenticated():
data = {
'unread_count': 0,
}
return JsonResponse(data)
try:
# Default to 5 as a max number of notifications
num_to_fetch = min(num_to_fetch, 100)
except ValueError:
num_to_fetch = 5
struct['actor'] = '{f} {l}'.format(f=n.actor.first_name, l=n.actor.last_name)
else:
struct['actor'] = str(n.actor)
if hasattr(n.target, 'notification_name'):
struct['target'] = n.target.notification_name
else:
struct['target'] = str(n.target)
struct['forward_link'] = n.get_absolute_url()
if n.action_object:
struct['action_object'] = str(n.action_object)
if request.GET.get('mark_as_read'):
# Mark all as read
request.user.notifications.mark_all_as_read()
data = {
'unread_count': request.user.notifications.unread().count(),