Skip to content

json_views

django_spire.notification.app.views.json_views

check_for_new_notifications_view

Source code in django_spire/notification/app/views/json_views.py
@login_required()
def check_for_new_notifications_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_view

Source code in django_spire/notification/app/views/json_views.py
@login_required()
def set_notifications_as_viewed_view(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 successfully marked as viewed.'})