91 lines
3.2 KiB
Python
91 lines
3.2 KiB
Python
|
# coding: utf-8
|
||
|
|
||
|
from django import forms
|
||
|
from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage
|
||
|
from widget import LatLonField
|
||
|
|
||
|
class StageForm(forms.ModelForm):
|
||
|
date_widget = forms.DateInput(attrs={"class":"datepicker"})
|
||
|
date_debut = forms.DateField(input_formats=["%d/%m/%Y"], widget=date_widget)
|
||
|
date_fin = forms.DateField(input_formats=["%d/%m/%Y"], widget=date_widget)
|
||
|
|
||
|
class Meta:
|
||
|
model = Stage
|
||
|
fields = ['sujet', 'date_debut', 'date_fin', 'type_stage', 'thematiques', 'matieres', 'encadrants', 'lieux']
|
||
|
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
if "request" in kwargs:
|
||
|
self.request = kwargs.pop("request")
|
||
|
super(StageForm, self).__init__(*args, **kwargs)
|
||
|
if hasattr(self, 'instance'):
|
||
|
self.prev_lieux = self.instance.avislieu_set.all()
|
||
|
else:
|
||
|
self.prev_lieux = None
|
||
|
|
||
|
def save(self, commit=True):
|
||
|
if self.instance.id is None:
|
||
|
self.instance.auteur = self.request.user.profil
|
||
|
|
||
|
stage = super(StageForm, self).save(commit=False)
|
||
|
|
||
|
if commit:
|
||
|
stage.save()
|
||
|
|
||
|
# Lecture des lieux, conservation des critiques précédentes
|
||
|
alieux = self.cleaned_data['lieux']
|
||
|
new_lieux = []
|
||
|
#print self.instance.lieux
|
||
|
if not (self.prev_lieux is None) and len(self.prev_lieux) > 0:
|
||
|
old_avislieux = {avis.lieu_id: avis for avis in self.prev_lieux}
|
||
|
for (k, nlieu) in enumerate(alieux):
|
||
|
if nlieu in old_avislieux:
|
||
|
# Ce lien existait déjà avant
|
||
|
# mise à jour de l'ordre
|
||
|
old_avislieu[nlieu].order = k
|
||
|
old_avi_lieu[nlieu].save()
|
||
|
# Rien d'autre à faire
|
||
|
del old_avislieux[nlieu]
|
||
|
else:
|
||
|
# C'est un nouveau lien
|
||
|
new_lieux.append((k, nlieu))
|
||
|
|
||
|
old_avislieux = old_avislieux.values()
|
||
|
for k, avislieu in enumerate(old_avislieux):
|
||
|
if k > len(new_lieux):
|
||
|
# Avis à supprimer
|
||
|
avislieu.delete()
|
||
|
else:
|
||
|
# On récupère le(s) avis pour le(s) nouveau(x) lieu(x)
|
||
|
avislieu.lieu, avislieu.order = new_lieux[k]
|
||
|
avislieu.save()
|
||
|
new_lieux = new_lieux[len(old_avislieux):]
|
||
|
else:
|
||
|
new_lieux = enumerate(alieux)
|
||
|
print(new_lieux)
|
||
|
# On crée les nouveaux avislieu qui vont bien
|
||
|
AvisLieu.objects.bulk_create([AvisLieu(lieu=lieu, stage=stage, order=k)
|
||
|
for (lieu, k) in new_lieux])
|
||
|
|
||
|
|
||
|
class LieuForm(forms.ModelForm):
|
||
|
coord = LatLonField()
|
||
|
|
||
|
class Meta:
|
||
|
model = Lieu
|
||
|
fields = ['nom', 'type_lieu', 'ville', 'pays', 'coord']
|
||
|
|
||
|
|
||
|
|
||
|
class AvisStageForm(forms.ModelForm):
|
||
|
|
||
|
class Meta:
|
||
|
model = AvisStage
|
||
|
fields = ['chapo', 'avis_ambiance', 'avis_sujet', 'avis_admin', 'les_plus', 'les_moins']
|
||
|
|
||
|
class AvisLieuForm(forms.ModelForm):
|
||
|
|
||
|
class Meta:
|
||
|
model = AvisLieu
|
||
|
fields = ['chapo', 'avis_lieustage', 'avis_pratique', 'avis_tourisme', 'les_plus', 'les_moins']
|
||
|
|