91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
from django.contrib.auth.forms import PasswordResetForm
|
||
from django.contrib.auth.tokens import default_token_generator
|
||
from django.contrib.auth.views import PasswordResetView
|
||
from django.shortcuts import render
|
||
from django.urls import reverse_lazy
|
||
from django_registration.backends.one_step.views import RegistrationView
|
||
|
||
from main.extra_func import check_user_exist, check_user_auth, update_profile, get_user_organization
|
||
from main.models import UserProfile
|
||
|
||
from django.contrib.auth.models import User
|
||
from main.forms import CustomRegistrationForm
|
||
from django.contrib.auth.decorators import login_required
|
||
|
||
|
||
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
|
||
|
||
forms = PasswordResetForm(initial={'email': form.data['email']})
|
||
if forms.is_valid():
|
||
opts = {
|
||
'use_https': self.request.is_secure(),
|
||
'token_generator': default_token_generator,
|
||
'from_email': form.data['email'],
|
||
'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,
|
||
}
|
||
forms.full_clean()
|
||
forms.save(**opts)
|
||
|
||
if get_user_organization(form.data['email']) == 'SYSTEM' and check_user_exist(form.data['email']):
|
||
user = User.objects.create_user(
|
||
username=form.data['email'],
|
||
email=form.data['email'],
|
||
)
|
||
|
||
f = PasswordResetView
|
||
profile = user.userprofile
|
||
update_profile(profile)
|
||
return user
|
||
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')
|