Добавление папки platform_admin
This commit is contained in:
71
myproject/platform_admin/forms.py
Normal file
71
myproject/platform_admin/forms.py
Normal 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
|
||||
Reference in New Issue
Block a user