2024-07-01 10:18:42 +00:00
|
|
|
import logging
|
|
|
|
|
2024-10-18 10:03:26 +00:00
|
|
|
from django.conf import settings
|
2024-07-01 10:18:42 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.contrib.auth import views as auth_views
|
|
|
|
from django.contrib.auth import login as auth_login
|
|
|
|
from django.contrib.auth import logout as auth_logout
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class LoginView(auth_views.LoginView):
|
|
|
|
template_name = 'login.html'
|
|
|
|
extra_context = {
|
|
|
|
'title': _('Login'),
|
2024-07-09 11:34:57 +00:00
|
|
|
'success_url': reverse_lazy('dashboard:unassigned_devices'),
|
2024-10-18 10:03:26 +00:00
|
|
|
'commit_id': settings.COMMIT,
|
2024-07-01 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.extra_context['success_url'] = request.GET.get(
|
|
|
|
'next',
|
2024-07-09 11:34:57 +00:00
|
|
|
reverse_lazy('dashboard:unassigned_devices')
|
2024-07-01 10:18:42 +00:00
|
|
|
)
|
|
|
|
if not self.request.user.is_anonymous:
|
2024-07-09 11:34:57 +00:00
|
|
|
return redirect(reverse_lazy('dashboard:unassigned_devices'))
|
2024-07-01 10:18:42 +00:00
|
|
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
user = form.get_user()
|
|
|
|
auth_login(self.request, user)
|
|
|
|
|
|
|
|
if user.is_anonymous:
|
|
|
|
return redirect(reverse_lazy("login:login"))
|
|
|
|
|
|
|
|
return redirect(self.extra_context['success_url'])
|
|
|
|
|
|
|
|
|
|
|
|
def LogoutView(request):
|
|
|
|
auth_logout(request)
|
|
|
|
return redirect(reverse_lazy("login:login"))
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordResetConfirmView(auth_views.PasswordResetConfirmView):
|
|
|
|
template_name = 'password_reset_confirm.html'
|
|
|
|
success_url = reverse_lazy('login:password_reset_complete')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
password = form.cleaned_data.get("new_password1")
|
|
|
|
user = form.user
|
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordResetView(auth_views.PasswordResetView):
|
|
|
|
template_name = 'password_reset.html'
|
|
|
|
email_template_name = 'password_reset_email.txt'
|
|
|
|
html_email_template_name = 'password_reset_email.html'
|
|
|
|
subject_template_name = 'password_reset_subject.txt'
|
|
|
|
success_url = reverse_lazy('login:password_reset_done')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
try:
|
2024-10-14 15:02:40 +00:00
|
|
|
response = super().form_valid(form)
|
|
|
|
return response
|
2024-07-01 10:18:42 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.error(err)
|
|
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
|