Skip to content

Commit

Permalink
Merge pull request #824 from ita-social-projects/799-admin-admin-regi…
Browse files Browse the repository at this point in the history
…stration

[Admin] [BackEnd] admin registration
  • Loading branch information
AlexanderSychev2005 authored Oct 12, 2024
2 parents a1cacec + eb05f01 commit 56d004a
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 8 deletions.
40 changes: 37 additions & 3 deletions BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from django.contrib.auth import get_user_model
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 .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 +21,35 @@ class Meta:
)


class AdminRegistrationSerializer(serializers.Serializer):
email = serializers.EmailField(
write_only=True,
)

def validate(self, value):
email = value.get("email").lower()

if User.objects.filter(email=email).exists():
raise serializers.ValidationError(
{"email": "Email is already registered"}
)

return value

def create(self, validated_data):
email = validated_data.get("email")
password = generate_password()
admin = User.objects.create(
email=email,
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 +82,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 +110,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"),
]
17 changes: 13 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 Down Expand Up @@ -148,3 +146,14 @@ class ContactsView(View):

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


class CreateAdminUserView(CreateAPIView):
"""
View for creating an admin user.
"""

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)

0 comments on commit 56d004a

Please sign in to comment.