added phones and social forms

This commit is contained in:
jluiselli 2020-09-17 15:46:16 +02:00
parent f31247e03a
commit c898a084fc
4 changed files with 60 additions and 36 deletions

View file

@ -1,4 +1,5 @@
from django import forms from django import forms
from django.forms.models import inlineformset_factory
from fiches.models import Profile, Department, Phone, Social, Mail, Address 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") raise forms.ValidationError(("Tous les champs sont vides"), code="invalid")
class PhoneForm(forms.BaseModelFormSet): class PhoneForm(forms.ModelForm):
class Meta: class Meta:
model = Phone model = Phone
exclude = [] exclude = []
PhoneFormSet = inlineformset_factory(Profile, Phone, exclude=[])
class SocialForm(forms.ModelForm): class SocialForm(forms.ModelForm):
class Meta: class Meta:
model = Social model = Social
exclude = [] exclude = []
SocialFormSet = inlineformset_factory(Profile, Social, exclude=[])
class MailForm(forms.ModelForm): class MailForm(forms.ModelForm):
class Meta: class Meta:
model = Mail model = Mail
exclude = [] exclude = []
MailFormSet = inlineformset_factory(Profile, Mail, exclude=[])
class AddressForm(forms.ModelForm): class AddressForm(forms.ModelForm):
class Meta: class Meta:
model = Address model = Address
exclude = [] exclude = []
AddressFormSet = inlineformset_factory(Profile, Address, exclude=[])

View file

@ -49,7 +49,7 @@ class Department(models.Model):
class Phone(models.Model): class Phone(models.Model):
profile = models.ForeignKey( 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")) name = models.CharField(max_length=255, verbose_name=_("type"))
number = models.CharField(max_length=1023, verbose_name=_("numéro")) number = models.CharField(max_length=1023, verbose_name=_("numéro"))

View file

@ -63,6 +63,14 @@
<label for="id_thurne">{% trans "Thurne :" %}</label> <label for="id_thurne">{% trans "Thurne :" %}</label>
{{ form.thurne }} {{ form.thurne }}
</div> </div>
<div class="form-entry">
<label for="id_phone">{% trans "Numéro(s) de téléphone :" %}</label>
{{ phone_form }}
</div>
<div class="form-entry">
<label for="id_social">{% trans "Réseaux sociaux :" %}</label>
{{ social_form }}
</div>
<div class="form-entry"> <div class="form-entry">
<label for="id_text_field">{% trans "Champ libre :" %}</label> <label for="id_text_field">{% trans "Champ libre :" %}</label>
<div id="free-text-edit-form" class="wide-form-entry"> <div id="free-text-edit-form" class="wide-form-entry">

View file

@ -2,7 +2,9 @@ from django.shortcuts import render
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from fiches.models import Profile 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.urls import reverse
from django.db.models import Q from django.db.models import Q
from django.utils import timezone 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.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import FormView, UpdateView from django.views.generic.edit import FormView, UpdateView
from django.http import HttpResponseRedirect
@method_decorator(login_required, name="dispatch") @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")) 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") @method_decorator(login_required, name="dispatch")
class EditView(UpdateView): class EditView(UpdateView):
model = Profile
template_name = "fiches/fiches_modif.html" template_name = "fiches/fiches_modif.html"
model = Profile
form_class = ProfileForm form_class = ProfileForm
def get_object(self): def get(self, request, *args, **kwargs):
return self.request.user.profile 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): def post(self, request, *args, **kwargs):
return reverse("fiche", args=(self.get_object().user,)) 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): 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( send_mail(
"Fiche annuaire modifée", "Fiche annuaire modifée",
render_to_string("fiches/mail/mail_modif.txt", {"profile": self.get_object()}), 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)], ["{}@clipper.ens.psl.eu".format(self.get_object().user.username)],
fail_silently=False, 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") @method_decorator(login_required, name="dispatch")