gestiojeux/accounts/views.py
sinavir fee31ab07b feat: switch to authens
Be careful there is no User migration
2024-07-04 18:15:57 +02:00

55 lines
1.7 KiB
Python

from django.views.generic import TemplateView, RedirectView
from django.views.generic.edit import UpdateView
from django.shortcuts import redirect
from django.urls import reverse
from django.dispatch import receiver
from django.contrib.auth import logout as auth_logout
from django.contrib.auth import user_logged_in, user_logged_out, user_login_failed
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.views import PasswordChangeView
from django.contrib import messages
from urllib.parse import quote as urlquote
from .forms import AccountSettingsForm
@receiver(user_logged_in)
def on_login(request, user, **kwargs):
messages.success(request, "Connexion réussie. Bienvenue, {}.".format(user))
@receiver(user_logged_out)
def on_logout(request, **kwargs):
messages.info(request, "Vous avez bien été déconnecté·e.")
@receiver(user_login_failed)
def on_login_failed(request, **kwargs):
messages.error(request, "Connexion échouée.")
class PasswordChangeView(PasswordChangeView):
template_name = "registration/change_password.html"
def __init__(self):
self.form_class.error_css_class = "errorfield"
def get_success_url(self):
messages.info(self.request, "Mot de passe mis à jour")
return reverse("accounts:account_settings")
class AccountSettingsView(LoginRequiredMixin, UpdateView):
template_name = "registration/account_settings.html"
form_class = AccountSettingsForm
def get_object(self):
return self.request.user
def get_success_url(self):
return self.request.get_full_path()
def form_valid(self, form):
messages.success(self.request, "Paramètres du compte mis à jour")
return super().form_valid(form)