@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.'})