kadenios/shared/auth/views.py

49 lines
1.7 KiB
Python
Raw Normal View History

from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import get_user_model
2020-12-21 00:07:07 +01:00
from django.contrib.auth import views as auth_views
from django.contrib.auth.hashers import make_password
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic.edit import CreateView
2020-12-21 00:07:07 +01:00
from .forms import ElectionAuthForm, PwdUserForm
from .utils import generate_password
User = get_user_model()
2020-12-21 00:07:07 +01:00
# #############################################################################
# Election Specific Login
# #############################################################################
2020-12-21 00:07:07 +01:00
class ElectionLoginView(auth_views.LoginView):
template_name = "auth/election_login.html"
2020-12-21 00:07:07 +01:00
authentication_form = ElectionAuthForm
def get_initial(self):
return {"election_id": self.kwargs.get("election_id")}
2020-12-21 00:07:07 +01:00
def get_context_data(self, **kwargs):
kwargs.update({"election_id": self.kwargs.get("election_id")})
return super().get_context_data(**kwargs)
# #############################################################################
# Creation of Password Accounts
# #############################################################################
@method_decorator(staff_member_required, name="dispatch")
class CreatePwdAccount(CreateView):
model = User
form_class = PwdUserForm
template_name = "auth/create-user.html"
success_url = reverse_lazy("auth.create-account")
def form_valid(self, form):
# On enregistre un mot de passe aléatoire
form.instance.password = make_password(generate_password(32))
# On envoie un mail pour réinitialiser le mot de passe
return super().form_valid(form)