User creation views

This commit is contained in:
Ludovic Stephan 2020-07-21 14:49:37 +02:00
parent f33416b712
commit 5e5b224f89
3 changed files with 104 additions and 4 deletions

View file

@ -1,5 +1,6 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from bds.models import BDSProfile
@ -12,6 +13,22 @@ class UserForm(forms.ModelForm):
fields = ["email", "first_name", "last_name"]
class UserFromClipperForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["username"].disabled = True
class Meta:
model = User
fields = ["username", "email", "first_name", "last_name"]
class UserFromScratchForm(UserCreationForm):
class Meta:
model = User
fields = ["username", "email", "first_name", "last_name"]
class ProfileForm(forms.ModelForm):
class Meta:
model = BDSProfile

View file

@ -0,0 +1,33 @@
{% extends "bds/base_layout.html" %}
{% load i18n %}
{% block content %}
{% for form in forms.values %}
{% for error in form.non_field_errors %}
<div class="notification is-danger">
{{ error }}
</div>
{% endfor %}
{% endfor %}
<h1 class="title">{% trans "Création d'un profil" %}</h1>
<div class="container">
<form action="" method="post">
{% csrf_token %}
{% for form in forms.values %}
{% include "bds/forms/form.html" with form=form errors=False %}
{% endfor %}
<div class="field">
<p class="control">
<input class="button is-fullwidth" type="submit" value="Enregistrer">
</p>
</div>
</form>
</div>
{% endblock %}

View file

@ -6,7 +6,7 @@ from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from bds.autocomplete import bds_search
from bds.forms import ProfileForm, UserForm
from bds.forms import ProfileForm, UserForm, UserFromClipperForm, UserFromScratchForm
from bds.mixins import MultipleFormView, StaffRequiredMixin
from shared.views import AutocompleteView
@ -55,7 +55,57 @@ class UserUpdateView(StaffRequiredMixin, MultipleFormView):
def form_invalid(self, forms):
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
return super().form_invalid(forms)
else:
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
return self.render_to_response(self.get_context_data(user_form, profile_form))
class UserCreateView(StaffRequiredMixin, MultipleFormView):
template_name = "bds/user_create.html"
def get_form_classes(self):
profile_class = ProfileForm
if "clipper" in self.kwargs:
user_class = UserFromClipperForm
else:
user_class = UserFromScratchForm
return {"user": user_class, "profile": profile_class}
def get_user_initial(self):
if "clipper" in self.kwargs:
clipper = self.kwargs["clipper"]
email = "{}@clipper.ens.fr".format(clipper)
fullname = self.request.GET.get("fullname", None)
if fullname:
# Heuristique : le premier mot est le prénom
first_name = fullname.split()[0]
last_name = " ".join(fullname.split()[1:])
else:
first_name = ""
last_name = ""
return dict(
username=clipper,
email=email,
first_name=first_name,
last_name=last_name,
)
else:
return {}
def get_success_url(self):
return reverse("bds:user.update", args=(self.user.pk,))
def form_valid(self, forms):
# On redéfinit self.user pour get_success_url
self.user = forms["user"].save()
profile = forms["profile"].save(commit=False)
profile.user = self.user
profile.save()
messages.success(self.request, _("Profil créé avec succès !"))
return super().form_valid(forms)
def form_invalid(self, forms):
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
return super().form_invalid(forms)