Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] [BackEnd] admin registration #824

Merged
merged 21 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a03e240
Created function for generating password for admin
AlexanderSychev2005 Oct 2, 2024
ac5786d
Added custom serializer for admin registration
AlexanderSychev2005 Oct 8, 2024
f0732ad
Additional password validation for admin serializer was removed
AlexanderSychev2005 Oct 10, 2024
4da3968
Useless imports were removed
AlexanderSychev2005 Oct 10, 2024
0427b60
Useless imports were removed
AlexanderSychev2005 Oct 10, 2024
d479761
The configuration added with sending the email about admin's password…
AlexanderSychev2005 Oct 10, 2024
d936cdc
Changed email's subject
AlexanderSychev2005 Oct 10, 2024
108424b
Transformed APIView to CreateModelMixin and GenericAPIView
AlexanderSychev2005 Oct 11, 2024
9cd95c2
Import are optimised
AlexanderSychev2005 Oct 11, 2024
3b1cdd2
Import are optimised
AlexanderSychev2005 Oct 11, 2024
1692c4b
Optimised view
AlexanderSychev2005 Oct 11, 2024
f050e57
Fixed serializer
AlexanderSychev2005 Oct 11, 2024
ffcb035
Added permission classes
AlexanderSychev2005 Oct 11, 2024
bb1caeb
Updated requirements and removed useless imports
AlexanderSychev2005 Oct 11, 2024
708ef7e
Adjusted admin registration, is_staff, is_active
AlexanderSychev2005 Oct 12, 2024
2746cd7
Black formatted
AlexanderSychev2005 Oct 12, 2024
7387a51
Black formatted
AlexanderSychev2005 Oct 12, 2024
e8cfbee
Got rid of unnecessary imports , adjusted serializer
AlexanderSychev2005 Oct 12, 2024
28a647a
Added doc string for the view
AlexanderSychev2005 Oct 12, 2024
c8fa079
Error
AlexanderSychev2005 Oct 12, 2024
eb05f01
Formatted black
AlexanderSychev2005 Oct 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -16,6 +25,40 @@ 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,
is_staff=True,
is_active=True,
)
admin.set_password(password)
admin.save()
send_email_about_admin_registration(email, password)
return admin


class AdminUserListSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
Expand Down Expand Up @@ -48,7 +91,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
Expand Down Expand Up @@ -76,7 +119,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
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="uk">
<head>
<style>
body {
color: black;
font-family: Arial, sans-serif;
}
p, b {
color: black;
}
</style>
</head>
<body>
<div>
<img src="{{protocol}}://178.212.110.52/craftMerge-logo.png" alt="CRAFTMERGE"/>
<p>Доброго дня,</p>
<p>Ваш пароль для входу на платформу: {{ password }} </p>
<p>Посилання для входу: {{ domain }}/login </p>

<p>З повагою,</p>
<p>Команда CraftMerge</p>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions BackEnd/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
UserDetailView,
AutoModerationHoursView,
ModerationEmailView,
CreateAdminUserView,
)

app_name = "administration"
Expand All @@ -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"),
]
15 changes: 11 additions & 4 deletions BackEnd/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
OpenApiResponse,
)

from rest_framework.permissions import (
BasePermission,
)
from rest_framework.generics import (
ListAPIView,
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
RetrieveUpdateAPIView,
CreateAPIView,
)

from administration.serializers import AdminRegistrationSerializer
from forum.settings import CONTACTS_INFO
from administration.serializers import (
AdminCompanyListSerializer,
Expand All @@ -29,6 +27,8 @@
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


Expand Down Expand Up @@ -148,3 +148,10 @@ class ContactsView(View):

def get(self, request):
return JsonResponse(CONTACTS_INFO)


class CreateAdminUserView(CreateAPIView):
permission_classes = [
IsSuperUser,
]
serializer_class = AdminRegistrationSerializer
1 change: 0 additions & 1 deletion BackEnd/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions BackEnd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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


5 changes: 5 additions & 0 deletions BackEnd/utils/administration/create_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from passlib import pwd


def generate_password():
return pwd.genword()
33 changes: 33 additions & 0 deletions BackEnd/utils/administration/send_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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="Generated password for administrator",
body=email_body,
from_email=settings.EMAIL_HOST_USER,
to=[
recipient,
],
)

email.content_subtype = EMAIL_CONTENT_SUBTYPE
email.send(fail_silently=False)
Loading