Réorganisation et sauvegarde avant changement de paradigme
This commit is contained in:
parent
3e9d26eef0
commit
ebfd293b0e
2 changed files with 92 additions and 69 deletions
90
avisstage/forms.py
Normal file
90
avisstage/forms.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
# 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']
|
||||
|
|
@ -10,7 +10,7 @@ from django.contrib.auth.decorators import login_required
|
|||
from braces.views import LoginRequiredMixin
|
||||
|
||||
from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage
|
||||
from widget import LatLonField
|
||||
from avisstage.forms import StageForm, LieuForm, AvisStageForm, AvisLieuForm
|
||||
|
||||
# Page d'accueil
|
||||
def index(request):
|
||||
|
@ -45,66 +45,6 @@ class ProfilView(DetailView, LoginRequiredMixin):
|
|||
return Normalien.objects.get(user__username=self.kwargs.get('username'))
|
||||
|
||||
# Stages
|
||||
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 : rien à faire
|
||||
del old_avislieux[nlieu]
|
||||
else:
|
||||
# C'est un nouveau lien
|
||||
new_lieux.append(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 = new_lieux[k]
|
||||
avislieu.save()
|
||||
new_lieux = new_lieux[len(old_avislieux):]
|
||||
else:
|
||||
new_lieux = alieux
|
||||
print(new_lieux)
|
||||
# On crée les nouveaux avislieu qui vont bien
|
||||
AvisLieu.objects.bulk_create([AvisLieu(lieu=lieu, stage=stage) for lieu in new_lieux])
|
||||
|
||||
|
||||
|
||||
|
||||
class StageAjout(CreateView, LoginRequiredMixin):
|
||||
model = Stage
|
||||
form_class = StageForm
|
||||
|
@ -139,19 +79,12 @@ class StageView(DetailView, LoginRequiredMixin):
|
|||
|
||||
|
||||
# Lieux des stages
|
||||
class LieuForm(forms.ModelForm):
|
||||
coord = LatLonField()
|
||||
|
||||
class Meta:
|
||||
model = Lieu
|
||||
fields = ['nom', 'type_lieu', 'ville', 'pays', 'coord']
|
||||
|
||||
class LieuAjout(CreateView, LoginRequiredMixin):
|
||||
model = Lieu
|
||||
form_class = LieuForm
|
||||
template_name = 'avisstage/formulaires/lieu.html'
|
||||
|
||||
|
||||
|
||||
def recherche(request):
|
||||
return render(request, 'avisstage/recherche.html')
|
||||
|
||||
|
|
Loading…
Reference in a new issue