From a03e2402711238fb4c0755b345a8916741387bf3 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 2 Oct 2024 17:51:22 +0300 Subject: [PATCH 01/21] Created function for generating password for admin --- BackEnd/utils/administration/create_password.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BackEnd/utils/administration/create_password.py diff --git a/BackEnd/utils/administration/create_password.py b/BackEnd/utils/administration/create_password.py new file mode 100644 index 00000000..9e22cdf8 --- /dev/null +++ b/BackEnd/utils/administration/create_password.py @@ -0,0 +1,5 @@ +from passlib import pwd + + +def generate_password(): + return pwd.genword() From ac5786dea34ed18fc6fb527fd37fd1de47cf4866 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 8 Oct 2024 22:40:53 +0300 Subject: [PATCH 02/21] Added custom serializer for admin registration --- BackEnd/administration/urls.py | 2 ++ BackEnd/administration/views.py | 33 ++++++++++++++++++++++ BackEnd/authentication/serializers.py | 40 ++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/BackEnd/administration/urls.py b/BackEnd/administration/urls.py index 8935d141..7a699a1f 100644 --- a/BackEnd/administration/urls.py +++ b/BackEnd/administration/urls.py @@ -8,6 +8,7 @@ UserDetailView, AutoModerationHoursView, ModerationEmailView, + CreateAdminUserView, ) app_name = "administration" @@ -24,4 +25,5 @@ ), path("email/", ModerationEmailView.as_view(), name="moderation-email"), path("contacts/", ContactsView.as_view(), name="contacts"), + path("admin_create/", CreateAdminUserView.as_view(), name="admin-create"), ] diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 8399daee..6746834c 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -5,6 +5,7 @@ OpenApiExample, OpenApiResponse, ) +from rest_framework import status from rest_framework.permissions import ( BasePermission, @@ -14,8 +15,11 @@ ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, + CreateAPIView, ) +from rest_framework.views import APIView +from authentication.serializers import UserRegistrationSerializer, AdminRegistrationSerializer from forum.settings import CONTACTS_INFO from administration.serializers import ( AdminCompanyListSerializer, @@ -29,6 +33,7 @@ from administration.models import AutoModeration, ModerationEmail from authentication.models import CustomUser from profiles.models import Profile +from utils.administration.create_password import generate_password from .permissions import IsStaffUser, IsStaffUserOrReadOnly, IsSuperUser @@ -148,3 +153,31 @@ class ContactsView(View): def get(self, request): return JsonResponse(CONTACTS_INFO) + + +class CreateAdminUserView(APIView): + def post(self, request): + name = "admin" + surname = "admin" + email = request.data["email"] + password = generate_password() + data = dict( + name=name, + surname=surname, + email=email, + password=password, + ) + serialized = AdminRegistrationSerializer(data=data) + if serialized.is_valid(): + admin = CustomUser.objects.create_user( + email=email, + name=name, + surname=surname, + password=password, + is_staff=True, + is_active=True, + ) + admin.save() + else: + return JsonResponse(serialized.errors) + return JsonResponse(serialized.data) diff --git a/BackEnd/authentication/serializers.py b/BackEnd/authentication/serializers.py index 3aa6814b..75c30607 100644 --- a/BackEnd/authentication/serializers.py +++ b/BackEnd/authentication/serializers.py @@ -13,7 +13,6 @@ from ratelimit.decorators import RateLimitDecorator from ratelimit.exception import RateLimitException - from profiles.models import Profile from validation.validate_password import ( validate_password_long, @@ -34,6 +33,45 @@ class Meta: fields = ("name", "is_registered", "is_startup", "is_fop") +class AdminRegistrationSerializer(serializers.ModelSerializer): + email = serializers.EmailField( + write_only=True, + ) + password = serializers.CharField( + style={"input_type": "password"}, write_only=True + ) + + class Meta: + model = User + fields = ("email", "password", "name", "surname") + + def validate(self, value): + custom_errors = defaultdict(list) + email = value.get("email").lower() + password = value.get("password") + if User.objects.filter(email=email).exists(): + custom_errors["email"].append("Email is already registered") + else: + value["email"] = email + try: + validate_password_long(password) + except ValidationError as error: + custom_errors["password"].append(error.message) + try: + validate_password_include_symbols(password) + except ValidationError as error: + custom_errors["password"].append(error.message) + if custom_errors: + raise serializers.ValidationError(custom_errors) + return value + + def create(self, validated_data): + user = User.objects.create(**validated_data) + user.set_password(validated_data["password"]) + user.save() + return user + + class UserRegistrationSerializer(UserCreatePasswordRetypeSerializer): company = CustomProfileSerializer(write_only=True) email = serializers.EmailField( From f0732adfe1009c7aa0bb80d7cc0607fabda412d3 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Oct 2024 16:28:31 +0300 Subject: [PATCH 03/21] Additional password validation for admin serializer was removed --- BackEnd/authentication/serializers.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/BackEnd/authentication/serializers.py b/BackEnd/authentication/serializers.py index 75c30607..d9612a78 100644 --- a/BackEnd/authentication/serializers.py +++ b/BackEnd/authentication/serializers.py @@ -57,10 +57,6 @@ def validate(self, value): validate_password_long(password) except ValidationError as error: custom_errors["password"].append(error.message) - try: - validate_password_include_symbols(password) - except ValidationError as error: - custom_errors["password"].append(error.message) if custom_errors: raise serializers.ValidationError(custom_errors) return value From 4da3968ddc97002c39a4a997b3dba3f80bd4347c Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Oct 2024 16:30:21 +0300 Subject: [PATCH 04/21] Useless imports were removed --- BackEnd/administration/views.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 6746834c..4f1729c8 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -5,17 +5,11 @@ OpenApiExample, OpenApiResponse, ) -from rest_framework import status -from rest_framework.permissions import ( - BasePermission, -) from rest_framework.generics import ( ListAPIView, - ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, - CreateAPIView, ) from rest_framework.views import APIView From 0427b6048e4e50d51191661ab35814f97c696c49 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Oct 2024 16:30:38 +0300 Subject: [PATCH 05/21] Useless imports were removed --- 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 4f1729c8..2c4c04df 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -13,7 +13,7 @@ ) from rest_framework.views import APIView -from authentication.serializers import UserRegistrationSerializer, AdminRegistrationSerializer +from authentication.serializers import AdminRegistrationSerializer from forum.settings import CONTACTS_INFO from administration.serializers import ( AdminCompanyListSerializer, From d479761ebcc201debbc7684fd45673a73fe6c82b Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Oct 2024 16:50:47 +0300 Subject: [PATCH 06/21] The configuration added with sending the email about admin's password , added template --- .../administration/email_template.html | 25 +++++++++++++++ BackEnd/administration/views.py | 2 ++ BackEnd/utils/administration/send_email.py | 31 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 BackEnd/administration/templates/administration/email_template.html create mode 100644 BackEnd/utils/administration/send_email.py diff --git a/BackEnd/administration/templates/administration/email_template.html b/BackEnd/administration/templates/administration/email_template.html new file mode 100644 index 00000000..c6963912 --- /dev/null +++ b/BackEnd/administration/templates/administration/email_template.html @@ -0,0 +1,25 @@ + + + + + + +
+ CRAFTMERGE +

Доброго дня,

+

Ваш пароль для входу на платформу: {{ password }}

+

Посилання для входу: {{ domain }}/login

+ +

З повагою,

+

Команда CraftMerge

+
+ + \ No newline at end of file diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 2c4c04df..72cc9ca8 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -28,6 +28,7 @@ from authentication.models import CustomUser from profiles.models import Profile from utils.administration.create_password import generate_password +from utils.administration.send_email import send_email_about_admin_registration from .permissions import IsStaffUser, IsStaffUserOrReadOnly, IsSuperUser @@ -172,6 +173,7 @@ def post(self, request): is_active=True, ) admin.save() + send_email_about_admin_registration(email, password) else: return JsonResponse(serialized.errors) return JsonResponse(serialized.data) diff --git a/BackEnd/utils/administration/send_email.py b/BackEnd/utils/administration/send_email.py new file mode 100644 index 00000000..dbe65c34 --- /dev/null +++ b/BackEnd/utils/administration/send_email.py @@ -0,0 +1,31 @@ +from decouple import config +from django.conf import settings +from django.core.mail import EmailMultiAlternatives +from django.template.loader import render_to_string + + +EMAIL_CONTENT_SUBTYPE = "html" +PROTOCOL = "http" +DOMAIN = config("ALLOWED_ENV_HOST") + + +def send_email_about_admin_registration(email, password): + context = { + "protocol": PROTOCOL, + "password": password, + "domain": DOMAIN, + } + + recipient = email + email_body = render_to_string("administration/email_template.html", context) + email = EmailMultiAlternatives( + subject="Information about number and size of banners and logos", + body=email_body, + from_email=settings.EMAIL_HOST_USER, + to=[ + recipient, + ], + ) + + email.content_subtype = EMAIL_CONTENT_SUBTYPE + email.send(fail_silently=False) From d936cdcf5c83ca7670b824620c80991585ce61fc Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Oct 2024 16:52:39 +0300 Subject: [PATCH 07/21] Changed email's subject --- BackEnd/utils/administration/send_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BackEnd/utils/administration/send_email.py b/BackEnd/utils/administration/send_email.py index dbe65c34..4d3b06f0 100644 --- a/BackEnd/utils/administration/send_email.py +++ b/BackEnd/utils/administration/send_email.py @@ -19,7 +19,7 @@ def send_email_about_admin_registration(email, password): recipient = email email_body = render_to_string("administration/email_template.html", context) email = EmailMultiAlternatives( - subject="Information about number and size of banners and logos", + subject="Generated password for administrator", body=email_body, from_email=settings.EMAIL_HOST_USER, to=[ From 108424b524ebbec30f7e0760520ac9e8ee48cccc Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:38:11 +0300 Subject: [PATCH 08/21] Transformed APIView to CreateModelMixin and GenericAPIView --- BackEnd/administration/serializers.py | 48 +++++++++++++++++++++++++-- BackEnd/administration/views.py | 37 +++++---------------- BackEnd/authentication/serializers.py | 35 ------------------- 3 files changed, 54 insertions(+), 66 deletions(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index f306660f..18d3b3aa 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -1,13 +1,22 @@ +from collections import defaultdict + +from django.contrib.auth import get_user_model +from django.core.exceptions import ValidationError from rest_framework import serializers from authentication.models import CustomUser from profiles.models import ( Profile, Region, ) +from utils.administration.create_password import generate_password +from utils.administration.send_email import send_email_about_admin_registration +from validation.validate_password import validate_password_long from .models import AutoModeration, ModerationEmail +User = get_user_model() + -class AdminRegionSerialaizer(serializers.ModelSerializer): +class AdminRegionSerializer(serializers.ModelSerializer): class Meta: model = Region fields = ( @@ -16,6 +25,39 @@ class Meta: ) +class AdminRegistrationSerializer(serializers.Serializer): + email = serializers.EmailField( + write_only=True, + ) + + def validate(self, value): + custom_errors = defaultdict(list) + email = value.get("email").lower() + + if User.objects.filter(email=email).exists(): + custom_errors["email"].append("Email is already registered") + + if custom_errors: + raise serializers.ValidationError(custom_errors) + return value + + def create(self, validated_data): + email = validated_data.get("email") + password = generate_password() + name = "admin" + surname = "admin" + admin = User.objects.create( + email=email, + name=name, + surname=surname, + password=password, + + ) + admin.save() + send_email_about_admin_registration(email, password) + return admin + + class AdminUserListSerializer(serializers.ModelSerializer): class Meta: model = CustomUser @@ -48,7 +90,7 @@ def get_company_name(self, obj) -> bool: class AdminCompanyListSerializer(serializers.ModelSerializer): person = AdminUserDetailSerializer(read_only=True) - regions = AdminRegionSerialaizer(many=True, read_only=True) + regions = AdminRegionSerializer(many=True, read_only=True) class Meta: model = Profile @@ -76,7 +118,7 @@ class AdminCompanyDetailSerializer(serializers.ModelSerializer): activities = serializers.SlugRelatedField( many=True, slug_field="name", read_only=True ) - regions = AdminRegionSerialaizer(many=True, read_only=True) + regions = AdminRegionSerializer(many=True, read_only=True) banner_image = serializers.ImageField( source="banner.image_path", required=False ) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 72cc9ca8..325c6357 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -5,15 +5,17 @@ OpenApiExample, OpenApiResponse, ) +from rest_framework import generics from rest_framework.generics import ( ListAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, ) +from rest_framework.mixins import CreateModelMixin from rest_framework.views import APIView -from authentication.serializers import AdminRegistrationSerializer +from administration.serializers import AdminRegistrationSerializer from forum.settings import CONTACTS_INFO from administration.serializers import ( AdminCompanyListSerializer, @@ -150,30 +152,9 @@ def get(self, request): return JsonResponse(CONTACTS_INFO) -class CreateAdminUserView(APIView): - def post(self, request): - name = "admin" - surname = "admin" - email = request.data["email"] - password = generate_password() - data = dict( - name=name, - surname=surname, - email=email, - password=password, - ) - serialized = AdminRegistrationSerializer(data=data) - if serialized.is_valid(): - admin = CustomUser.objects.create_user( - email=email, - name=name, - surname=surname, - password=password, - is_staff=True, - is_active=True, - ) - admin.save() - send_email_about_admin_registration(email, password) - else: - return JsonResponse(serialized.errors) - return JsonResponse(serialized.data) +class CreateAdminUserView(CreateModelMixin, generics.GenericAPIView): + serializer_class = AdminRegistrationSerializer + queryset = CustomUser.objects.all() + + def post(self, request, *args, **kwargs): + return self.create(request, *args, **kwargs) diff --git a/BackEnd/authentication/serializers.py b/BackEnd/authentication/serializers.py index d9612a78..345511ff 100644 --- a/BackEnd/authentication/serializers.py +++ b/BackEnd/authentication/serializers.py @@ -33,41 +33,6 @@ class Meta: fields = ("name", "is_registered", "is_startup", "is_fop") -class AdminRegistrationSerializer(serializers.ModelSerializer): - email = serializers.EmailField( - write_only=True, - ) - password = serializers.CharField( - style={"input_type": "password"}, write_only=True - ) - - class Meta: - model = User - fields = ("email", "password", "name", "surname") - - def validate(self, value): - custom_errors = defaultdict(list) - email = value.get("email").lower() - password = value.get("password") - if User.objects.filter(email=email).exists(): - custom_errors["email"].append("Email is already registered") - else: - value["email"] = email - try: - validate_password_long(password) - except ValidationError as error: - custom_errors["password"].append(error.message) - if custom_errors: - raise serializers.ValidationError(custom_errors) - return value - - def create(self, validated_data): - user = User.objects.create(**validated_data) - user.set_password(validated_data["password"]) - user.save() - return user - - class UserRegistrationSerializer(UserCreatePasswordRetypeSerializer): company = CustomProfileSerializer(write_only=True) email = serializers.EmailField( From 9cd95c22cb13a9961cb0016c62e88fa2fb181860 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:43:35 +0300 Subject: [PATCH 09/21] Import are optimised --- BackEnd/administration/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 325c6357..737817d0 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -10,7 +10,7 @@ from rest_framework.generics import ( ListAPIView, RetrieveUpdateDestroyAPIView, - RetrieveUpdateAPIView, + RetrieveUpdateAPIView, GenericAPIView, ) from rest_framework.mixins import CreateModelMixin from rest_framework.views import APIView @@ -152,7 +152,7 @@ def get(self, request): return JsonResponse(CONTACTS_INFO) -class CreateAdminUserView(CreateModelMixin, generics.GenericAPIView): +class CreateAdminUserView(CreateModelMixin, GenericAPIView): serializer_class = AdminRegistrationSerializer queryset = CustomUser.objects.all() From 3b1cdd2c0f78566f8028c43dca2093b72a1dbad6 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:43:51 +0300 Subject: [PATCH 10/21] Import are optimised --- BackEnd/administration/views.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 737817d0..1a04d613 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -5,7 +5,6 @@ OpenApiExample, OpenApiResponse, ) -from rest_framework import generics from rest_framework.generics import ( ListAPIView, @@ -13,7 +12,6 @@ RetrieveUpdateAPIView, GenericAPIView, ) from rest_framework.mixins import CreateModelMixin -from rest_framework.views import APIView from administration.serializers import AdminRegistrationSerializer from forum.settings import CONTACTS_INFO From 1692c4b05c5ae3e35a261363ff6e3637b0b09271 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:52:36 +0300 Subject: [PATCH 11/21] Optimised view --- BackEnd/administration/views.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 1a04d613..e7a5b46c 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -9,7 +9,7 @@ from rest_framework.generics import ( ListAPIView, RetrieveUpdateDestroyAPIView, - RetrieveUpdateAPIView, GenericAPIView, + RetrieveUpdateAPIView, GenericAPIView, CreateAPIView, ) from rest_framework.mixins import CreateModelMixin @@ -150,9 +150,8 @@ def get(self, request): return JsonResponse(CONTACTS_INFO) -class CreateAdminUserView(CreateModelMixin, GenericAPIView): +class CreateAdminUserView(CreateAPIView): serializer_class = AdminRegistrationSerializer - queryset = CustomUser.objects.all() - def post(self, request, *args, **kwargs): - return self.create(request, *args, **kwargs) + + From f050e571d71f1f1712d548285ef56b77c669c2a0 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:55:58 +0300 Subject: [PATCH 12/21] Fixed serializer --- BackEnd/administration/serializers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 18d3b3aa..39405552 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -50,9 +50,8 @@ def create(self, validated_data): email=email, name=name, surname=surname, - password=password, - ) + admin.set_password(password) admin.save() send_email_about_admin_registration(email, password) return admin From ffcb03515c1f027125411b52cd5a67f6ddc857ec Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 12:59:33 +0300 Subject: [PATCH 13/21] Added permission classes --- BackEnd/administration/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index e7a5b46c..8d514b9a 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -151,6 +151,7 @@ def get(self, request): class CreateAdminUserView(CreateAPIView): + permission_classes = [IsSuperUser, ] serializer_class = AdminRegistrationSerializer From bb1caeb2372a926b5ccce1a84598dd87324cb2e1 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 13:25:31 +0300 Subject: [PATCH 14/21] Updated requirements and removed useless imports --- BackEnd/administration/views.py | 3 +-- BackEnd/requirements.txt | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 8d514b9a..8f84edd4 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -9,9 +9,8 @@ from rest_framework.generics import ( ListAPIView, RetrieveUpdateDestroyAPIView, - RetrieveUpdateAPIView, GenericAPIView, CreateAPIView, + RetrieveUpdateAPIView, CreateAPIView, ) -from rest_framework.mixins import CreateModelMixin from administration.serializers import AdminRegistrationSerializer from forum.settings import CONTACTS_INFO diff --git a/BackEnd/requirements.txt b/BackEnd/requirements.txt index c59c62de..d6329625 100644 --- a/BackEnd/requirements.txt +++ b/BackEnd/requirements.txt @@ -20,5 +20,6 @@ drf-spectacular==0.26.5 ratelimit==2.2.1 django-debug-toolbar==4.3.0 celery==5.4.0 +passlib==1.7.4 From 708ef7e128dbcbc5079587aa36c16bcc2daa3c57 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 11:35:54 +0300 Subject: [PATCH 15/21] Adjusted admin registration, is_staff, is_active --- BackEnd/administration/serializers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 39405552..acb20595 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -50,6 +50,8 @@ def create(self, validated_data): email=email, name=name, surname=surname, + is_staff=True, + is_active=True, ) admin.set_password(password) admin.save() From 2746cd740d921acb38383f4627fa0f03eca8d26a Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 11:39:51 +0300 Subject: [PATCH 16/21] Black formatted --- BackEnd/administration/views.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 8f84edd4..e75f1281 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -9,7 +9,8 @@ from rest_framework.generics import ( ListAPIView, RetrieveUpdateDestroyAPIView, - RetrieveUpdateAPIView, CreateAPIView, + RetrieveUpdateAPIView, + CreateAPIView, ) from administration.serializers import AdminRegistrationSerializer @@ -150,8 +151,7 @@ def get(self, request): class CreateAdminUserView(CreateAPIView): - permission_classes = [IsSuperUser, ] + permission_classes = [ + IsSuperUser, + ] serializer_class = AdminRegistrationSerializer - - - From 7387a514ce46d5ffcfcab28ffde67aa354fa892c Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 12:59:36 +0300 Subject: [PATCH 17/21] Black formatted --- BackEnd/utils/administration/send_email.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/BackEnd/utils/administration/send_email.py b/BackEnd/utils/administration/send_email.py index 4d3b06f0..a5e7557c 100644 --- a/BackEnd/utils/administration/send_email.py +++ b/BackEnd/utils/administration/send_email.py @@ -17,7 +17,9 @@ def send_email_about_admin_registration(email, password): } recipient = email - email_body = render_to_string("administration/email_template.html", context) + email_body = render_to_string( + "administration/email_template.html", context + ) email = EmailMultiAlternatives( subject="Generated password for administrator", body=email_body, From e8cfbee68bd5abdc4eb3a577c946b1d2fbeb78a5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 15:55:29 +0300 Subject: [PATCH 18/21] Got rid of unnecessary imports , adjusted serializer --- BackEnd/administration/serializers.py | 13 +------------ BackEnd/administration/views.py | 2 -- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index acb20595..dd7969f4 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -1,7 +1,4 @@ -from collections import defaultdict - from django.contrib.auth import get_user_model -from django.core.exceptions import ValidationError from rest_framework import serializers from authentication.models import CustomUser from profiles.models import ( @@ -10,7 +7,6 @@ ) from utils.administration.create_password import generate_password from utils.administration.send_email import send_email_about_admin_registration -from validation.validate_password import validate_password_long from .models import AutoModeration, ModerationEmail User = get_user_model() @@ -31,25 +27,18 @@ class AdminRegistrationSerializer(serializers.Serializer): ) def validate(self, value): - custom_errors = defaultdict(list) email = value.get("email").lower() if User.objects.filter(email=email).exists(): - custom_errors["email"].append("Email is already registered") + raise serializers.ValidationError("Email is already registered") - if custom_errors: - raise serializers.ValidationError(custom_errors) return value def create(self, validated_data): email = validated_data.get("email") password = generate_password() - name = "admin" - surname = "admin" admin = User.objects.create( email=email, - name=name, - surname=surname, is_staff=True, is_active=True, ) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index e75f1281..9475437a 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -27,8 +27,6 @@ from administration.models import AutoModeration, ModerationEmail from authentication.models import CustomUser from profiles.models import Profile -from utils.administration.create_password import generate_password -from utils.administration.send_email import send_email_about_admin_registration from .permissions import IsStaffUser, IsStaffUserOrReadOnly, IsSuperUser From 28a647a47f5e8b8506bcf9e53a6c0dccbdc78523 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 15:59:51 +0300 Subject: [PATCH 19/21] Added doc string for the view --- BackEnd/administration/views.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 9475437a..5540f0e9 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -149,6 +149,9 @@ def get(self, request): class CreateAdminUserView(CreateAPIView): + """ + View for creating an admin user. + """ permission_classes = [ IsSuperUser, ] From c8fa079e986c2cd6e84fc9fb4402e8d3f1262439 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 16:10:33 +0300 Subject: [PATCH 20/21] Error --- BackEnd/administration/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index dd7969f4..42f0154b 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -30,7 +30,7 @@ def validate(self, value): email = value.get("email").lower() if User.objects.filter(email=email).exists(): - raise serializers.ValidationError("Email is already registered") + raise serializers.ValidationError({"email": "Email is already registered"}) return value From eb05f0149fc4f1ebf2d3085f8ab76f54f8c3c011 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 12 Oct 2024 20:05:26 +0300 Subject: [PATCH 21/21] Formatted black --- BackEnd/administration/serializers.py | 4 +++- BackEnd/administration/views.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 42f0154b..baf85ea3 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -30,7 +30,9 @@ def validate(self, value): email = value.get("email").lower() if User.objects.filter(email=email).exists(): - raise serializers.ValidationError({"email": "Email is already registered"}) + raise serializers.ValidationError( + {"email": "Email is already registered"} + ) return value diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 5540f0e9..e2bf80da 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -152,6 +152,7 @@ class CreateAdminUserView(CreateAPIView): """ View for creating an admin user. """ + permission_classes = [ IsSuperUser, ]