98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
import logging
|
||
|
||
from django.contrib.auth.decorators import login_required
|
||
from django.contrib.auth.forms import PasswordResetForm
|
||
from django.contrib.auth.models import User
|
||
from django.contrib.auth.tokens import default_token_generator
|
||
from django.contrib.auth.views import LoginView
|
||
from django.shortcuts import render
|
||
from django.urls import reverse_lazy
|
||
from django_registration.views import RegistrationView
|
||
|
||
from access_controller.settings import EMAIL_HOST_USER
|
||
from main.extra_func import check_user_exist, update_profile, get_user_organization
|
||
from main.forms import CustomRegistrationForm, CustomAuthenticationForm
|
||
|
||
|
||
class CustomRegistrationView(RegistrationView):
|
||
"""
|
||
Отображение и логика работы страницы регистрации пользователя
|
||
"""
|
||
form_class = CustomRegistrationForm
|
||
template_name = 'django_registration/registration_form.html'
|
||
success_url = reverse_lazy('django_registration_complete')
|
||
is_allowed = True
|
||
|
||
def register(self, form):
|
||
self.is_allowed = True
|
||
if check_user_exist(form.data['email']) and get_user_organization(form.data['email']) == 'SYSTEM':
|
||
forms = PasswordResetForm(self.request.POST)
|
||
if forms.is_valid():
|
||
opts = {
|
||
'use_https': self.request.is_secure(),
|
||
'token_generator': default_token_generator,
|
||
'from_email': EMAIL_HOST_USER,
|
||
'email_template_name': 'registration/password_reset_email.html',
|
||
'subject_template_name': 'registration/password_reset_subject.txt',
|
||
'request': self.request,
|
||
'html_email_template_name': None,
|
||
'extra_email_context': None,
|
||
}
|
||
user = User.objects.create_user(
|
||
username=form.data['email'],
|
||
email=form.data['email'],
|
||
password=User.objects.make_random_password(length=50)
|
||
)
|
||
forms.save(**opts)
|
||
update_profile(user.userprofile)
|
||
return user
|
||
else:
|
||
raise ValueError('Непредвиденная ошибка')
|
||
else:
|
||
self.is_allowed = False
|
||
|
||
def get_success_url(self, user=None):
|
||
"""
|
||
Возвращает url-адрес страницы, куда нужно перейти после успешной/неуспешной регистрации
|
||
Используется самой django-registration
|
||
"""
|
||
if self.is_allowed:
|
||
return reverse_lazy('password_reset_done')
|
||
else:
|
||
return reverse_lazy('django_registration_disallowed')
|
||
|
||
|
||
@login_required()
|
||
def profile_page(request):
|
||
"""
|
||
Отображение страницы профиля
|
||
|
||
:param request: объект с деталями запроса
|
||
:type request: :class:`django.http.HttpResponse`
|
||
:return: объект ответа сервера с HTML-кодом внутри
|
||
"""
|
||
user_profile = request.user.userprofile
|
||
update_profile(user_profile)
|
||
|
||
context = {
|
||
'email': user_profile.user.email,
|
||
'name': user_profile.name,
|
||
'role': user_profile.role,
|
||
'image_url': user_profile.image,
|
||
'pagename': 'Страница профиля'
|
||
}
|
||
return render(request, 'pages/profile.html', context)
|
||
|
||
|
||
def main_page(request):
|
||
logger = logging.getLogger('main.index')
|
||
logger.info('Index page opened')
|
||
return render(request, 'pages/index.html')
|
||
|
||
|
||
class CustomLoginView(LoginView):
|
||
"""
|
||
Отображение страницы авторизации пользователя
|
||
"""
|
||
form_class = CustomAuthenticationForm
|