diff --git a/bds/forms.py b/bds/forms.py
index 59738e06..e54f17d4 100644
--- a/bds/forms.py
+++ b/bds/forms.py
@@ -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
diff --git a/bds/templates/bds/user_create.html b/bds/templates/bds/user_create.html
new file mode 100644
index 00000000..25d97e79
--- /dev/null
+++ b/bds/templates/bds/user_create.html
@@ -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 %}
+
+ {{ error }}
+
+ {% endfor %}
+{% endfor %}
+
+{% trans "Création d'un profil" %}
+
+
+
+{% endblock %}
diff --git a/bds/views.py b/bds/views.py
index 67c1f50c..47651458 100644
--- a/bds/views.py
+++ b/bds/views.py
@@ -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)