From 06f6587050b320867fe8eac2c181b1455e489873 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 29 Nov 2021 01:08:06 +0100 Subject: [PATCH] add endpoint + buttons to mark a notification read --- InvenTree/common/api.py | 36 ++++++++++++++++++- InvenTree/common/serializers.py | 8 +++++ .../notifications/notifications.html | 5 +-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/InvenTree/common/api.py b/InvenTree/common/api.py index 8d8c9e244b..2f468702a2 100644 --- a/InvenTree/common/api.py +++ b/InvenTree/common/api.py @@ -9,6 +9,7 @@ from django.conf.urls import url, include from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters, generics, permissions +from rest_framework import serializers import common.models import common.serializers @@ -186,6 +187,33 @@ class NotificationDetail(generics.RetrieveDestroyAPIView): ] +class NotificationRead(generics.CreateAPIView): + """ + API endpoint to mark a notification as read. + """ + + queryset = common.models.NotificationMessage.objects.all() + serializer_class = common.serializers.NotificationReadSerializer + + permission_classes = [ + UserSettingsPermissions, + ] + + def get_serializer_context(self): + context = super().get_serializer_context() + if self.request: + context['instance'] = self.get_object() + return context + + def perform_create(self, serializer): + message = self.get_object() + try: + message.read = True + message.save() + except Exception as exc: + raise serializers.ValidationError(detail=serializers.as_serializer_error(exc)) + + settings_api_urls = [ # User settings url(r'^user/', include([ @@ -210,7 +238,13 @@ common_api_urls = [ # Notifications url(r'^notifications/', include([ - url(r'^(?P\d+)/', NotificationDetail.as_view(), name='api-notifications-detail'), + # Individual purchase order detail URLs + url(r'^(?P\d+)/', include([ + url(r'^read/', NotificationRead.as_view(), name='api-notifications-read'), + url(r'.*$', NotificationDetail.as_view(), name='api-notifications-detail'), + ])), + + # Notification messages list url(r'^.*$', NotificationList.as_view(), name='api-notifications-list'), ])), diff --git a/InvenTree/common/serializers.py b/InvenTree/common/serializers.py index b1880c4339..71ccac8a4d 100644 --- a/InvenTree/common/serializers.py +++ b/InvenTree/common/serializers.py @@ -144,3 +144,11 @@ class NotificationMessageSerializer(InvenTreeModelSerializer): 'age_human', 'read', ] + + +class NotificationReadSerializer(NotificationMessageSerializer): + + def is_valid(self, raise_exception=False): + self.instance = self.context['instance'] # set instance that should be returned + self._validated_data = True + return True diff --git a/InvenTree/templates/InvenTree/notifications/notifications.html b/InvenTree/templates/InvenTree/notifications/notifications.html index e412eef922..cd48584d71 100644 --- a/InvenTree/templates/InvenTree/notifications/notifications.html +++ b/InvenTree/templates/InvenTree/notifications/notifications.html @@ -91,9 +91,10 @@ function loadNotificationTable(table, options={}) { }); $(table).on('click', '.notification-read', function() { - var url = "/notifications/" + $(this).attr('pk') + "/"; + var url = "/api/notifications/" + $(this).attr('pk') + "/read/"; - inventreeDelete(url, { + inventreePut(url, {}, { + method: 'POST', success: function() { $(table).bootstrapTable('refresh'); }