handles mail and adresses

This commit is contained in:
jluiselli 2020-09-17 16:30:41 +02:00
parent c898a084fc
commit 5cb6d2dd5e
6 changed files with 94 additions and 31 deletions

View file

@ -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=[])

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

@ -56,12 +56,26 @@
</p>
{% endif %}
{% if profile.social_set.exists %}
<p class="phone">
<p class="social">
<span class="label">{{ profile.social_set.count|pluralize:_("Réseau social,Réseaux sociaux") }}</span>
<span class="separator"></span>
<span class="value">{% for p in profile.social_set.all %}{{ p }}{% if not forloop.last %},<br>{% endif %}{% endfor %}</span>
</p>
{% endif %}
{% if profile.mail_set.exists %}
<p class="mail">
<span class="label">{{ profile.mail_set.count|pluralize:_("Mail,Mails") }}</span>
<span class="separator"></span>
<span class="value">{% for p in profile.mail_set.all %}{{ p }}{% if not forloop.last %},<br>{% endif %}{% endfor %}</span>
</p>
{% endif %}
{% if profile.address_set.exists %}
<p class="address">
<span class="label">{{ profile.address_set.count|pluralize:_("Adresse,Adresses") }}</span>
<span class="separator"></span>
<span class="value">{% for p in profile.address_set.all %}{{ p }}{% if not forloop.last %},<br>{% endif %}{% endfor %}</span>
</p>
{% endif %}
</div>
{% if profile.text_field %}

View file

@ -71,6 +71,14 @@
<label for="id_social">{% trans "Réseaux sociaux :" %}</label>
{{ social_form }}
</div>
<div class="form-entry">
<label for="id_mail">{% trans "Mail(s):" %}</label>
{{ mail_form }}
</div>
<div class="form-entry">
<label for="id_address">{% trans "Adresse(s):" %}</label>
{{ address_form }}
</div>
<div class="form-entry">
<label for="id_text_field">{% trans "Champ libre :" %}</label>
<div id="free-text-edit-form" class="wide-form-entry">

View file

@ -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