2018-10-13 20:01:45 +00:00
|
|
|
from django.contrib.auth import login, authenticate
|
2018-10-27 00:33:45 +00:00
|
|
|
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
|
2018-12-29 15:11:20 +00:00
|
|
|
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
|
|
|
|
|
2019-12-16 20:19:50 +00:00
|
|
|
from YtManagerApp.services import Services
|
2018-12-29 15:11:20 +00:00
|
|
|
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):
|
2019-01-02 21:11:39 +00:00
|
|
|
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
|
|
|
|
|
2018-12-29 15:11:20 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2020-04-10 21:42:44 +00:00
|
|
|
if not Services.appConfig.allow_registrations:
|
2018-12-29 15:11:20 +00:00
|
|
|
return HttpResponseForbidden("Registrations are disabled!")
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
class RegisterDoneView(LoginRequiredMixin, TemplateView):
|
2018-10-13 20:01:45 +00:00
|
|
|
template_name = 'registration/register_done.html'
|