SciPost Code Repository

Skip to content
Snippets Groups Projects
views.py 2.23 KiB
Newer Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
Jorran de Wit's avatar
Jorran de Wit committed
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
Jorran de Wit's avatar
Jorran de Wit committed
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


Jorran de Wit's avatar
Jorran de Wit committed
def is_test_user(user):
Jorran de Wit's avatar
Jorran de Wit committed
    """Check if user is test user.

    To be removed after test-phase is over.
    """
    return True
Jorran de Wit's avatar
Jorran de Wit committed
    return user.groups.filter(name='Testers').exists()


Jorran de Wit's avatar
Jorran de Wit committed
@user_passes_test(is_test_user)
def forward(request, slug):
Jorran de Wit's avatar
Jorran de Wit committed
    """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()
Jorran de Wit's avatar
Jorran de Wit committed
    if hasattr(notification.target, 'get_notification_url'):
        return redirect(notification.target.get_notification_url(notification.url_code))
    return redirect(notification.target.get_absolute_url())


Jorran de Wit's avatar
Jorran de Wit committed
@login_required
Jorran de Wit's avatar
Jorran de Wit committed
@user_passes_test(is_test_user)
def mark_toggle(request, slug=None):
Jorran de Wit's avatar
Jorran de Wit committed
    """Toggle mark as read."""
Jorran de Wit's avatar
Jorran de Wit committed
    id = slug2id(slug)

    notification = get_object_or_404(Notification, recipient=request.user, id=id)
Jorran de Wit's avatar
Jorran de Wit committed
    notification.mark_toggle()
Jorran de Wit's avatar
Jorran de Wit committed

    _next = request.GET.get('next')
    if _next:
        return redirect(_next)

Jorran de Wit's avatar
Jorran de Wit committed
    if request.GET.get('json'):
        return JsonResponse({'unread': notification.unread})
Jorran de Wit's avatar
Jorran de Wit committed
    return redirect('notifications:all')
Jorran de Wit's avatar
Jorran de Wit committed


def live_unread_notification_count(request):
Jorran de Wit's avatar
Jorran de Wit committed
    """Return JSON of unread messages count."""
Jorran de Wit's avatar
Jorran de Wit committed
    if not request.user.is_authenticated():
        data = {'unread_count': 0}
    else:
        data = {'unread_count': request.user.notifications.unread().count()}
    return JsonResponse(data)


Jorran de Wit's avatar
Jorran de Wit committed
def live_notification_list(request):
Jorran de Wit's avatar
Jorran de Wit committed
    """Return JSON of unread count and content of messages."""
Jorran de Wit's avatar
Jorran de Wit committed
    # if not request.user.is_authenticated():
    #     data = {
    #         'unread_count': 0,
    #         'list': []
    #     }
    #     return JsonResponse(data)
Jorran de Wit's avatar
Jorran de Wit committed
    data = {
Jorran de Wit's avatar
Jorran de Wit committed
        'unread_count': 0,
        'list': []
Jorran de Wit's avatar
Jorran de Wit committed
    }
    return JsonResponse(data)