from django.contrib.admin.views.decorators import staff_member_required from django.contrib.auth import get_user_model 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 from .forms import ElectionAuthForm, PwdUserForm from .utils import generate_password User = get_user_model() # ############################################################################# # Election Specific Login # ############################################################################# class ElectionLoginView(auth_views.LoginView): template_name = "auth/election_login.html" authentication_form = ElectionAuthForm def get_initial(self): return {"election_id": self.kwargs.get("election_id")} 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)