From f6a86162d37280b773214e97ba3aebb4c4e511d4 Mon Sep 17 00:00:00 2001 From: KyzukY Date: Tue, 22 Oct 2024 14:20:53 +0300 Subject: [PATCH 1/5] add enum,change view and serializers --- BackEnd/administration/serializers.py | 16 ++++------ .../admin_feedback_template.html | 2 +- BackEnd/administration/views.py | 30 ++++++++----------- .../utils/administration/feedback_category.py | 11 +++++++ FrontEnd/src/components/Contact/Contact.jsx | 8 ++--- 5 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 BackEnd/utils/administration/feedback_category.py diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 911ca4af..17c36271 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -1,5 +1,6 @@ from django.contrib.auth import get_user_model from rest_framework import serializers +from utils.administration.feedback_category import FeedbackCategory from authentication.models import CustomUser from profiles.models import ( Profile, @@ -198,24 +199,19 @@ class FeedbackSerializer(serializers.Serializer): email = serializers.EmailField( required=True, error_messages={ - "required": "Будь ласка, вкажіть правильну адресу електронної скриньки." + "required": "Please provide a valid email address." }, ) message = serializers.CharField( min_length=10, required=True, error_messages={ - "required": "Повідомлення не може бути порожнім.", - "min_length": "Повідомлення не може бути коротшим за 10 символів.", + "required": "Message cannot be empty.", + "min_length": "Message must be at least 10 characters long.", }, ) category = serializers.ChoiceField( - choices=[ - ("Технічне питання", "Технічне питання"), - ("Рекомендації", "Рекомендації"), - ("Питання", "Питання"), - ("Інше", "Інше"), - ], + choices=FeedbackCategory.choices(), required=True, - error_messages={"required": "Будь ласка, оберіть тип повідомлення."}, + error_messages={"required": "Please select a category."}, ) diff --git a/BackEnd/administration/templates/administration/admin_feedback_template.html b/BackEnd/administration/templates/administration/admin_feedback_template.html index e0a06979..002256b5 100644 --- a/BackEnd/administration/templates/administration/admin_feedback_template.html +++ b/BackEnd/administration/templates/administration/admin_feedback_template.html @@ -26,7 +26,7 @@
CRAFTMERGE -

Нове повідомлення від {{ user_email }}

+

Нове повідомлення від {{ user_email }}

Категорія: {{ category }}

Повідомлення:

diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index d5d59d79..1f98e11b 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -183,20 +183,16 @@ class CreateAdminUserView(CreateAPIView): serializer_class = AdminRegistrationSerializer -class FeedbackView(APIView): - def post(self, request): - serializer = FeedbackSerializer(data=request.data) - - if serializer.is_valid(): - email = serializer.validated_data["email"] - message = serializer.validated_data["message"] - category = serializer.validated_data["category"] - - send_email_feedback(email, message, category) - - return Response( - {"message": "Ваше повідомлення надіслано успішно!"}, - status=status.HTTP_200_OK, - ) - - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) +class FeedbackView(CreateAPIView): + serializer_class = FeedbackSerializer + + def perform_create(self, serializer): + email = serializer.validated_data["email"] + message = serializer.validated_data["message"] + category = serializer.validated_data["category"] + + send_email_feedback(email, message, category) + + def handle_exception(self, exc): + response = super().handle_exception(exc) + return response diff --git a/BackEnd/utils/administration/feedback_category.py b/BackEnd/utils/administration/feedback_category.py new file mode 100644 index 00000000..5f5340e2 --- /dev/null +++ b/BackEnd/utils/administration/feedback_category.py @@ -0,0 +1,11 @@ +from enum import Enum + +class FeedbackCategory(Enum): + TECHNICAL = "Технічне питання" + RECOMMENDATION = "Рекомендації" + QUESTION = "Питання" + OTHER = "Інше" + + @classmethod + def choices(cls): + return [(category.value, category.value) for category in cls] diff --git a/FrontEnd/src/components/Contact/Contact.jsx b/FrontEnd/src/components/Contact/Contact.jsx index ea9eff07..0852a1fc 100644 --- a/FrontEnd/src/components/Contact/Contact.jsx +++ b/FrontEnd/src/components/Contact/Contact.jsx @@ -36,10 +36,10 @@ const Contact = () => { }, [percent]); const categoryOptions = [ - { id: 1, name: 'Технічне питання' }, - { id: 2, name: 'Рекомендації' }, - { id: 3, name: 'Питання' }, - { id: 4, name: 'Інше' }, + { id: 'TECHNICAL', name: 'Технічне питання' }, + { id: 'RECOMMENDATION', name: 'Рекомендації' }, + { id: 'QUESTION', name: 'Питання' }, + { id: 'OTHER', name: 'Інше' }, ]; const handleEmailChange = (e) => { From c2e18728529fd85743baece245fc0bd3b5f18e8d Mon Sep 17 00:00:00 2001 From: KyzukY Date: Tue, 22 Oct 2024 14:25:46 +0300 Subject: [PATCH 2/5] add documentation for FeedbackView --- BackEnd/administration/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 1f98e11b..3b21cc20 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -187,6 +187,18 @@ class FeedbackView(CreateAPIView): serializer_class = FeedbackSerializer def perform_create(self, serializer): + """ + Performs the creation of a new feedback record and sends an email notification. + + Parameters: + - serializer (FeedbackSerializer): The serializer instance containing validated data. + + Returns: + None + + This method extracts the email, message, and category from the validated data in the serializer. + It then calls the `send_email_feedback` function to send an email notification with the provided feedback details. + """ email = serializer.validated_data["email"] message = serializer.validated_data["message"] category = serializer.validated_data["category"] From 3f22e8418df2233b1bd4c609db0a7254ef763e73 Mon Sep 17 00:00:00 2001 From: KyzukY Date: Tue, 22 Oct 2024 15:18:54 +0300 Subject: [PATCH 3/5] fix lint --- BackEnd/administration/serializers.py | 4 +--- BackEnd/administration/views.py | 4 ++-- BackEnd/utils/administration/feedback_category.py | 1 + 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 17c36271..24cda9be 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -198,9 +198,7 @@ class Meta: class FeedbackSerializer(serializers.Serializer): email = serializers.EmailField( required=True, - error_messages={ - "required": "Please provide a valid email address." - }, + error_messages={"required": "Please provide a valid email address."}, ) message = serializers.CharField( min_length=10, diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 3b21cc20..04272b28 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -195,14 +195,14 @@ def perform_create(self, serializer): Returns: None - + This method extracts the email, message, and category from the validated data in the serializer. It then calls the `send_email_feedback` function to send an email notification with the provided feedback details. """ email = serializer.validated_data["email"] message = serializer.validated_data["message"] category = serializer.validated_data["category"] - + send_email_feedback(email, message, category) def handle_exception(self, exc): diff --git a/BackEnd/utils/administration/feedback_category.py b/BackEnd/utils/administration/feedback_category.py index 5f5340e2..d83e0b7d 100644 --- a/BackEnd/utils/administration/feedback_category.py +++ b/BackEnd/utils/administration/feedback_category.py @@ -1,5 +1,6 @@ from enum import Enum + class FeedbackCategory(Enum): TECHNICAL = "Технічне питання" RECOMMENDATION = "Рекомендації" From 191603690163144bc4abde6e9812b9bfffaaf03c Mon Sep 17 00:00:00 2001 From: KyzukY Date: Wed, 23 Oct 2024 15:25:58 +0300 Subject: [PATCH 4/5] delete function --- BackEnd/administration/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 04272b28..baca0671 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -203,8 +203,4 @@ def perform_create(self, serializer): message = serializer.validated_data["message"] category = serializer.validated_data["category"] - send_email_feedback(email, message, category) - - def handle_exception(self, exc): - response = super().handle_exception(exc) - return response + send_email_feedback(email, message, category) \ No newline at end of file From 628cab9fdc7ff6460339b781e90f2070cf259448 Mon Sep 17 00:00:00 2001 From: KyzukY Date: Wed, 23 Oct 2024 15:28:54 +0300 Subject: [PATCH 5/5] delete function --- BackEnd/administration/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index baca0671..aaf4acb3 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -203,4 +203,4 @@ def perform_create(self, serializer): message = serializer.validated_data["message"] category = serializer.validated_data["category"] - send_email_feedback(email, message, category) \ No newline at end of file + send_email_feedback(email, message, category)