Newer
Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
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
"""Check if user is test user.
To be removed after test-phase is over.
"""
return True
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()
if hasattr(notification.target, 'get_notification_url'):
return redirect(notification.target.get_notification_url(notification.url_code))
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,
# 'list': []
# }
# return JsonResponse(data)