From 5cb6d2dd5e211be2eae5724c9d327ae4f85b1d56 Mon Sep 17 00:00:00 2001 From: jluiselli Date: Thu, 17 Sep 2020 16:30:41 +0200 Subject: [PATCH] handles mail and adresses --- fiches/forms.py | 19 ----------- fiches/migrations/0006_auto_20200917_1420.py | 24 ++++++++++++++ fiches/migrations/0007_auto_20200917_1421.py | 24 ++++++++++++++ fiches/templates/fiches/fiche.html | 16 ++++++++- fiches/templates/fiches/fiches_modif.html | 8 +++++ fiches/views.py | 34 +++++++++++++------- 6 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 fiches/migrations/0006_auto_20200917_1420.py create mode 100644 fiches/migrations/0007_auto_20200917_1421.py diff --git a/fiches/forms.py b/fiches/forms.py index 16f1d0e..ffd7b3b 100644 --- a/fiches/forms.py +++ b/fiches/forms.py @@ -26,29 +26,10 @@ class SearchForm(forms.Form): raise forms.ValidationError(("Tous les champs sont vides"), code="invalid") -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/migrations/0006_auto_20200917_1420.py b/fiches/migrations/0006_auto_20200917_1420.py new file mode 100644 index 0000000..5636fdc --- /dev/null +++ b/fiches/migrations/0006_auto_20200917_1420.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.16 on 2020-09-17 14:20 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('fiches', '0005_profile_pronoun'), + ] + + operations = [ + migrations.AlterField( + model_name='phone', + name='profile', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='numeros', to='fiches.Profile', verbose_name='profil'), + ), + migrations.AlterField( + model_name='social', + name='profile', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='socials', to='fiches.Profile', verbose_name='profil'), + ), + ] diff --git a/fiches/migrations/0007_auto_20200917_1421.py b/fiches/migrations/0007_auto_20200917_1421.py new file mode 100644 index 0000000..ef14593 --- /dev/null +++ b/fiches/migrations/0007_auto_20200917_1421.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.16 on 2020-09-17 14:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('fiches', '0006_auto_20200917_1420'), + ] + + operations = [ + migrations.AlterField( + model_name='phone', + name='profile', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='fiches.Profile', verbose_name='profil'), + ), + migrations.AlterField( + model_name='social', + name='profile', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='fiches.Profile', verbose_name='profil'), + ), + ] diff --git a/fiches/templates/fiches/fiche.html b/fiches/templates/fiches/fiche.html index 8391e6e..2eb0b17 100644 --- a/fiches/templates/fiches/fiche.html +++ b/fiches/templates/fiches/fiche.html @@ -56,12 +56,26 @@

{% endif %} {% if profile.social_set.exists %} -

+

{{ profile.social_set.count|pluralize:_("Réseau social,Réseaux sociaux") }} {% for p in profile.social_set.all %}{{ p }}{% if not forloop.last %},
{% endif %}{% endfor %}

{% endif %} + {% if profile.mail_set.exists %} +

+ {{ profile.mail_set.count|pluralize:_("Mail,Mails") }} + + {% for p in profile.mail_set.all %}{{ p }}{% if not forloop.last %},
{% endif %}{% endfor %}
+

+ {% endif %} + {% if profile.address_set.exists %} +

+ {{ profile.address_set.count|pluralize:_("Adresse,Adresses") }} + + {% for p in profile.address_set.all %}{{ p }}{% if not forloop.last %},
{% endif %}{% endfor %}
+

+ {% endif %} {% if profile.text_field %} diff --git a/fiches/templates/fiches/fiches_modif.html b/fiches/templates/fiches/fiches_modif.html index 4e79634..5f6f068 100644 --- a/fiches/templates/fiches/fiches_modif.html +++ b/fiches/templates/fiches/fiches_modif.html @@ -71,6 +71,14 @@ {{ social_form }} +
+ + {{ mail_form }} +
+
+ + {{ address_form }} +
diff --git a/fiches/views.py b/fiches/views.py index d779895..660050f 100644 --- a/fiches/views.py +++ b/fiches/views.py @@ -1,8 +1,8 @@ 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, PhoneFormSet, SocialFormSet +from fiches.models import Profile, Phone, Social, Mail, Address +from fiches.forms import ProfileForm, SearchForm, PhoneFormSet, SocialFormSet, MailFormSet, AddressFormSet from django.forms import formset_factory from django.forms.models import model_to_dict from django.urls import reverse @@ -37,12 +37,16 @@ class EditView(UpdateView): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) - phone_form = PhoneFormSet() + phone_form = PhoneFormSet(queryset=Phone.objects.filter(profile=self.object)) social_form = SocialFormSet() + mail_form = MailFormSet() + address_form = AddressFormSet() return self.render_to_response( self.get_context_data(form=form, phone_form=phone_form, - social_form=social_form)) + social_form=social_form, + mail_form=mail_form, + address_form=address_form)) def post(self, request, *args, **kwargs): self.object = self.get_object() @@ -50,18 +54,24 @@ class EditView(UpdateView): 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) + mail_form = MailFormSet(self.request.POST) + address_form = AddressFormSet(self.request.POST) + if (form.is_valid() and phone_form.is_valid() and social_form.is_valid() + and mail_form.is_valid() and address_form.is_valid()): + return self.form_valid(form, phone_form, social_form, mail_form, address_form) else: - return self.form_invalid(form, phone_form, social_form) - self.form_save(phone_form) + return self.form_invalid(form, phone_form, social_form, mail_form, address_form) - def form_valid(self, form, phone_form, social_form): + def form_valid(self, form, phone_form, social_form, mail_form, address_form): self.object = form.save() phone_form.instance = self.object phone_form.save() social_form.instance = self.object social_form.save() + mail_form.instance = self.object + mail_form.save() + address_form.instance = self.object + address_form.save() send_mail( "Fiche annuaire modifée", render_to_string("fiches/mail/mail_modif.txt", {"profile": self.get_object()}), @@ -71,11 +81,13 @@ class EditView(UpdateView): ) return HttpResponseRedirect(self.get_success_url()) - def form_invalid(self, form, phone_form, social_form): + def form_invalid(self, form, phone_form, social_form, mail_form, address_form): return self.render_to_response( self.get_context_data(form=form, phone_form=phone_form, - social_form=social_form)) + social_form=social_form, + mail_form=mail_form, + address_form=address_form)) def get_object(self): return self.request.user.profile