2021-02-19 17:55:27 +03:00

86 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.shortcuts import render
from django.urls import reverse_lazy
from django_registration.backends.one_step.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
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):
return render(request, 'pages/index.html')