Добавление папки platform_admin

This commit is contained in:
2026-01-08 22:17:22 +03:00
parent 969e49f4b5
commit 741db3a792
13 changed files with 700 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
"""
Формы для аутентификации администраторов платформы.
"""
from django import forms
from django.contrib.auth import authenticate
class PlatformAdminLoginForm(forms.Form):
"""
Форма входа для администраторов платформы.
"""
email = forms.EmailField(
label="Email",
widget=forms.EmailInput(attrs={
'class': 'form-control',
'placeholder': 'admin@platform.com',
'autofocus': True,
})
)
password = forms.CharField(
label="Пароль",
strip=False,
widget=forms.PasswordInput(attrs={
'class': 'form-control',
'placeholder': 'Пароль',
})
)
error_messages = {
'invalid_login': "Неверный email или пароль.",
'inactive': "Этот аккаунт деактивирован.",
}
def __init__(self, request=None, *args, **kwargs):
self.request = request
self.user_cache = None
super().__init__(*args, **kwargs)
def clean(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
if email is not None and password:
self.user_cache = authenticate(
self.request,
username=email,
password=password
)
if self.user_cache is None:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
)
else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data
def confirm_login_allowed(self, user):
"""
Проверка что пользователь может войти.
"""
if not user.is_active:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',
)
def get_user(self):
return self.user_cache