77 lines
2.9 KiB
Python
77 lines
2.9 KiB
Python
from django.shortcuts import render
|
||
from django.urls import reverse_lazy
|
||
|
||
from main.extra_func import set_and_get_name, set_and_get_email, load_and_get_image, set_and_get_role, check_user_exist, \
|
||
check_user_auth
|
||
from main.models import UserProfile
|
||
|
||
from django.contrib.auth.models import User
|
||
from main.forms import CustomRegistrationForm
|
||
from django_registration.views import RegistrationView
|
||
|
||
from django.contrib.auth.decorators import login_required
|
||
|
||
from zenpy import Zenpy
|
||
|
||
|
||
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):
|
||
if check_user_exist(form.data['email']) and check_user_auth(form.data['email'], form.data['password_zen']):
|
||
user = User.objects.create_user(
|
||
username=form.data['username'],
|
||
email=form.data['email'],
|
||
password=form.data['password1']
|
||
)
|
||
profile = UserProfile(
|
||
image='None.png',
|
||
user=user,
|
||
role=0,
|
||
)
|
||
set_and_get_name(profile)
|
||
set_and_get_email(profile)
|
||
set_and_get_role(profile)
|
||
load_and_get_image(profile)
|
||
profile.save()
|
||
else:
|
||
self.is_allowed = False
|
||
|
||
def get_success_url(self, request):
|
||
"""
|
||
Вовзращет url-адресс страницы, куда нужно перейти после успешной/неуспешной регистрации
|
||
Используется самой django-registration
|
||
"""
|
||
if self.is_allowed:
|
||
return reverse_lazy('django_registration_complete')
|
||
else:
|
||
return reverse_lazy('django_registration_disallowed')
|
||
|
||
#@login_required()
|
||
def profile_page(request):
|
||
"""
|
||
Отображение страницы профиля
|
||
|
||
:param request: объект с деталями запроса
|
||
:type request: :class:`django.http.HttpResponse`
|
||
:return: объект ответа сервера с HTML-кодом внутри
|
||
"""
|
||
if request.user.is_authenticated:
|
||
UP = UserProfile.objects.get(user=request.user)
|
||
else: # TODO: Убрать после появления регистрации и авторизации, добавить login_required()
|
||
UP = UserProfile.objects.get(user=1)
|
||
context = {
|
||
'name': set_and_get_name(UP),
|
||
'email': set_and_get_email(UP),
|
||
'role': set_and_get_role(UP),
|
||
'image_name': load_and_get_image(UP),
|
||
'pagename': 'Страница профиля'
|
||
}
|
||
return render(request, 'pages/profile.html', context)
|