ytsm/app/YtManagerApp/views/auth.py

47 lines
1.6 KiB
Python
Raw Normal View History

2018-10-13 20:01:45 +00:00
from django.contrib.auth import login, authenticate
from django.contrib.auth.mixins import LoginRequiredMixin
2018-10-13 20:01:45 +00:00
from django.contrib.auth.models import User
from django.contrib.auth.views import LoginView
from django.http import HttpResponseForbidden
2018-10-13 20:01:45 +00:00
from django.urls import reverse_lazy
from django.views.generic import FormView, TemplateView
from YtManagerApp.services import Services
from YtManagerApp.views.forms.auth import ExtendedAuthenticationForm, ExtendedUserCreationForm
2018-10-13 20:01:45 +00:00
class ExtendedLoginView(LoginView):
form_class = ExtendedAuthenticationForm
class RegisterView(FormView):
template_name = 'registration/register.html'
form_class = ExtendedUserCreationForm
success_url = reverse_lazy('register_done')
def form_valid(self, form):
form.apply_session_expiry(self.request)
2018-12-21 10:28:06 +00:00
form.save()
2018-10-13 20:01:45 +00:00
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(self.request, user)
return super().form_valid(form)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_first_user'] = (User.objects.count() == 0)
return context
def post(self, request, *args, **kwargs):
2019-12-18 22:27:06 +00:00
if not Services.appConfig().allow_registrations:
return HttpResponseForbidden("Registrations are disabled!")
return super().post(request, *args, **kwargs)
2018-10-13 20:01:45 +00:00
class RegisterDoneView(LoginRequiredMixin, TemplateView):
2018-10-13 20:01:45 +00:00
template_name = 'registration/register_done.html'