Skip to content

json_views

django_spire.notification.app.views.json_views

check_new_notifications_ajax_view

Source code in django_spire/notification/app/views/json_views.py
@login_required()
def check_new_notifications_ajax_view(request: WSGIRequest) -> JsonResponse:
    notifications = (
        AppNotification.objects
        .active()
        .is_sent()
        .exclude_viewed_by_user(request.user)
    )

    return JsonResponse({
        'status': 200,
        'has_new_notifications': bool(notifications)
    })

set_notifications_as_viewed_ajax

Source code in django_spire/notification/app/views/json_views.py
@login_required()
def set_notifications_as_viewed_ajax(request: WSGIRequest) -> JsonResponse:
    notification_list = (
        AppNotification.objects
        .active()
        .is_sent()
        .by_user(request.user)
        .exclude_viewed_by_user(request.user)
    )
    ctype = ContentType.objects.get_for_model(AppNotification)
    viewed_model_objects = [
        Viewed(
            user=request.user,
            object_id=notification.id,
            content_type=ctype
        )
        for notification in notification_list
    ]

    Viewed.objects.bulk_create(viewed_model_objects)

    return JsonResponse({'status': 200, 'message': 'Notifications succesfully marked as viewed.'})