From cebeed873abfba31da1004ff102df2db2d16f49a Mon Sep 17 00:00:00 2001 From: KlemenSpruk Date: Wed, 18 Oct 2023 10:25:51 +0200 Subject: [PATCH] Fix for notification create (#158) * fix for notification create * added test --- .../notifications/rest/notification.py | 15 +++++++++++-- .../tests/api/test_create_mail.py | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/django_project_base/notifications/rest/notification.py b/django_project_base/notifications/rest/notification.py index f405cd80..84a40be5 100644 --- a/django_project_base/notifications/rest/notification.py +++ b/django_project_base/notifications/rest/notification.py @@ -409,7 +409,7 @@ def filter_queryset_field(self, queryset, field, value): return super().filter_queryset_field(queryset, field, value) def get_serializer_class(self): - if not self.detail and self.action == "create": + if self.action in ("create", "update"): class NewMessageSerializer(Serializer): message_body = NotificationSerializer().fields.fields["message_body"] @@ -434,7 +434,7 @@ def get_queryset(self): except ProjectNotSelectedError as e: raise NotFound(e.message) - def perform_create(self, serializer): + def _create_notification(self, serializer): host_url = get_host_url(self.request) notification = Notification( message=DjangoProjectBaseMessage( @@ -460,6 +460,17 @@ def perform_create(self, serializer): ) notification.send() + def update(self, request, *args, **kwargs): + kwargs.pop("pk", None) + return super().create(request, *args, **kwargs) + + def create(self, request, *args, **kwargs): + kwargs.pop("pk", None) + return super().create(request, *args, **kwargs) + + def perform_create(self, serializer): + return self._create_notification(serializer) + class ChannelSerializer(Serializer): available = fields.FloatField(read_only=True, display_table=DisplayMode.HIDDEN) diff --git a/django_project_base/notifications/tests/api/test_create_mail.py b/django_project_base/notifications/tests/api/test_create_mail.py index 1b812f2d..429f81ff 100644 --- a/django_project_base/notifications/tests/api/test_create_mail.py +++ b/django_project_base/notifications/tests/api/test_create_mail.py @@ -1,5 +1,11 @@ +import socket + import swapper +from django.urls import reverse +from rest_framework import status +from rest_framework.response import Response +from django_project_base.notifications.base.channels.mail_channel import MailChannel from django_project_base.notifications.email_notification import EMailNotification from django_project_base.notifications.models import DjangoProjectBaseMessage, DjangoProjectBaseNotification from django_project_base.notifications.tests.notifications_transaction_test_case import NotificationsTransactionTestCase @@ -34,3 +40,19 @@ def test_send_mail(self): ).send() ) self.assertEqual(DjangoProjectBaseNotification.objects.all().count(), 1) + + def test_api_create_mail(self): + self._login_to_api_client_with_test_user() + response: Response = self.api_client.put( + reverse("notification-detail", kwargs=dict(pk="new")), + format="json", + data={ + "message_body": "demo", + "message_subject": "demo", + "message_to": [self.test_user.pk], + "send_on_channels": [MailChannel.name], + }, + HTTP_HOST=socket.gethostname().lower(), + HTTP_CURRENT_PROJECT=swapper.load_model("django_project_base", "Project").objects.first().slug, + ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED)