from django.shortcuts import render from django.urls import reverse_lazy from main.extra_func import check_user_exist, check_user_auth, update_profile 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): self.is_allowed = True 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 = user.userprofile update_profile(profile) else: self.is_allowed = False def get_success_url(self, user=None): """ Возвращает 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-кодом внутри """ 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') def registration_failed(request): return render(request, 'pages/registration_failed.html')