diff --git a/fiches/forms.py b/fiches/forms.py index 6273c99..16f1d0e 100644 --- a/fiches/forms.py +++ b/fiches/forms.py @@ -1,4 +1,5 @@ from django import forms +from django.forms.models import inlineformset_factory from fiches.models import Profile, Department, Phone, Social, Mail, Address @@ -25,27 +26,29 @@ class SearchForm(forms.Form): raise forms.ValidationError(("Tous les champs sont vides"), code="invalid") -class PhoneForm(forms.BaseModelFormSet): +class PhoneForm(forms.ModelForm): class Meta: model = Phone exclude = [] - +PhoneFormSet = inlineformset_factory(Profile, Phone, exclude=[]) class SocialForm(forms.ModelForm): class Meta: model = Social exclude = [] +SocialFormSet = inlineformset_factory(Profile, Social, exclude=[]) class MailForm(forms.ModelForm): class Meta: model = Mail exclude = [] +MailFormSet = inlineformset_factory(Profile, Mail, exclude=[]) class AddressForm(forms.ModelForm): class Meta: model = Address exclude = [] - +AddressFormSet = inlineformset_factory(Profile, Address, exclude=[]) diff --git a/fiches/models.py b/fiches/models.py index 26b367e..3dbc3cf 100644 --- a/fiches/models.py +++ b/fiches/models.py @@ -49,7 +49,7 @@ class Department(models.Model): class Phone(models.Model): profile = models.ForeignKey( - Profile, related_name="numeros", on_delete=models.CASCADE, verbose_name=_("profil") + Profile, on_delete=models.CASCADE, verbose_name=_("profil") ) name = models.CharField(max_length=255, verbose_name=_("type")) number = models.CharField(max_length=1023, verbose_name=_("numéro")) diff --git a/fiches/templates/fiches/fiches_modif.html b/fiches/templates/fiches/fiches_modif.html index ecbfdb9..4e79634 100644 --- a/fiches/templates/fiches/fiches_modif.html +++ b/fiches/templates/fiches/fiches_modif.html @@ -63,6 +63,14 @@ {{ form.thurne }} +
+ + {{ phone_form }} +
+
+ + {{ social_form }} +
diff --git a/fiches/views.py b/fiches/views.py index 0911b26..d779895 100644 --- a/fiches/views.py +++ b/fiches/views.py @@ -2,7 +2,9 @@ from django.shortcuts import render from django.shortcuts import get_object_or_404, redirect from django.contrib.auth.decorators import login_required from fiches.models import Profile -from fiches.forms import ProfileForm, SearchForm, PhoneForm +from fiches.forms import ProfileForm, SearchForm, PhoneFormSet, SocialFormSet +from django.forms import formset_factory +from django.forms.models import model_to_dict from django.urls import reverse from django.db.models import Q from django.utils import timezone @@ -13,6 +15,7 @@ from django.template.loader import render_to_string from django.views.generic.detail import DetailView from django.views.generic.list import ListView from django.views.generic.edit import FormView, UpdateView +from django.http import HttpResponseRedirect @method_decorator(login_required, name="dispatch") @@ -24,42 +27,41 @@ class FicheView(DetailView): return get_object_or_404(Profile, user__username=self.kwargs.get("user")) -@login_required -def fiche_modif(request): - profile = request.user.profile - if request.method == "POST": - form = ProfileForm(request.POST, request.FILES, instance=profile) - #form_phone = PhoneForm(request.POST, instance=) - if form.is_valid(): - form.save() - send_mail( - "Fiche annuaire modifée", - render_to_string("fiches/mail/mail_modif.txt", {"profile": profile}), - "klub-dev@ens.psl.eu", - ["{}@clipper.ens.psl.eu".format(request.user.username)], - fail_silently=False, - ) - return redirect(reverse("fiche", args=(profile.user,))) - - else: - form = ProfileForm(instance=profile) - - return render(request, "fiches/fiches_modif.html", {"form": form}) - - @method_decorator(login_required, name="dispatch") class EditView(UpdateView): - model = Profile template_name = "fiches/fiches_modif.html" + model = Profile form_class = ProfileForm - def get_object(self): - return self.request.user.profile + def get(self, request, *args, **kwargs): + self.object = self.get_object() + form_class = self.get_form_class() + form = self.get_form(form_class) + phone_form = PhoneFormSet() + social_form = SocialFormSet() + return self.render_to_response( + self.get_context_data(form=form, + phone_form=phone_form, + social_form=social_form)) - def get_success_url(self): - return reverse("fiche", args=(self.get_object().user,)) - - def form_valid(self, form): + def post(self, request, *args, **kwargs): + self.object = self.get_object() + form_class = self.get_form_class() + form = self.get_form(form_class) + phone_form = PhoneFormSet(self.request.POST) + social_form = SocialFormSet(self.request.POST) + if (form.is_valid() and phone_form.is_valid() and social_form.is_valid()): + return self.form_valid(form, phone_form, social_form) + else: + return self.form_invalid(form, phone_form, social_form) + self.form_save(phone_form) + + def form_valid(self, form, phone_form, social_form): + self.object = form.save() + phone_form.instance = self.object + phone_form.save() + social_form.instance = self.object + social_form.save() send_mail( "Fiche annuaire modifée", render_to_string("fiches/mail/mail_modif.txt", {"profile": self.get_object()}), @@ -67,8 +69,19 @@ class EditView(UpdateView): ["{}@clipper.ens.psl.eu".format(self.get_object().user.username)], fail_silently=False, ) - return super().form_valid(form) + return HttpResponseRedirect(self.get_success_url()) + def form_invalid(self, form, phone_form, social_form): + return self.render_to_response( + self.get_context_data(form=form, + phone_form=phone_form, + social_form=social_form)) + + def get_object(self): + return self.request.user.profile + + def get_success_url(self): + return reverse("fiche", args=(self.get_object().user,)) @method_decorator(login_required, name="dispatch")