2017-04-11 00:20:14 +02:00
|
|
|
# coding: utf-8
|
|
|
|
|
2017-04-04 00:31:50 +02:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
2017-04-07 03:01:27 +02:00
|
|
|
from django.views.generic import DetailView
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView
|
|
|
|
from django import forms
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from braces.views import LoginRequiredMixin
|
|
|
|
|
2017-04-11 00:20:14 +02:00
|
|
|
from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage
|
|
|
|
from widget import LatLonField
|
2017-04-07 03:01:27 +02:00
|
|
|
|
|
|
|
# Page d'accueil
|
2017-04-05 00:23:35 +02:00
|
|
|
def index(request):
|
|
|
|
return render(request, 'avisstage/index.html')
|
|
|
|
|
2017-04-07 03:01:27 +02:00
|
|
|
# Espace personnel
|
|
|
|
@login_required
|
2017-04-05 00:23:35 +02:00
|
|
|
def perso(request):
|
|
|
|
return render(request, 'avisstage/perso.html')
|
|
|
|
|
2017-04-07 03:01:27 +02:00
|
|
|
# Profil
|
|
|
|
class ProfilEdit(UpdateView, LoginRequiredMixin):
|
|
|
|
model = Normalien
|
|
|
|
fields = ['nom', 'promotion', 'mail']
|
|
|
|
template_name = 'avisstage/formulaires/profil.html'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user.profil
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
print kwargs
|
|
|
|
return super(ProfilEdit, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('avisstage:perso')
|
|
|
|
|
|
|
|
class ProfilView(DetailView, LoginRequiredMixin):
|
|
|
|
model = Normalien
|
|
|
|
template_name = 'avisstage/detail/profil.html'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
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
|
2017-04-11 00:20:14 +02:00
|
|
|
fields = ['sujet', 'date_debut', 'date_fin', 'type_stage', 'thematiques', 'matieres', 'encadrants', 'lieux']
|
2017-04-07 03:01:27 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-04-11 00:20:14 +02:00
|
|
|
if "request" in kwargs:
|
|
|
|
self.request = kwargs.pop("request")
|
2017-04-07 03:01:27 +02:00
|
|
|
super(StageForm, self).__init__(*args, **kwargs)
|
2017-04-11 00:20:14 +02:00
|
|
|
if hasattr(self, 'instance'):
|
|
|
|
self.prev_lieux = self.instance.avislieu_set.all()
|
|
|
|
else:
|
|
|
|
self.prev_lieux = None
|
2017-04-07 03:01:27 +02:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
2017-04-11 00:20:14 +02:00
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 03:01:27 +02:00
|
|
|
|
|
|
|
class StageAjout(CreateView, LoginRequiredMixin):
|
|
|
|
model = Stage
|
|
|
|
form_class = StageForm
|
|
|
|
template_name = 'avisstage/formulaires/stage.html'
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(StageAjout, self).get_form_kwargs()
|
|
|
|
kwargs.update({'request': self.request})
|
|
|
|
return kwargs
|
2017-04-05 00:23:35 +02:00
|
|
|
|
2017-04-11 00:20:14 +02:00
|
|
|
class StageEdit(UpdateView, LoginRequiredMixin):
|
|
|
|
model = Stage
|
|
|
|
form_class = StageForm
|
|
|
|
template_name = 'avisstage/formulaires/stage.html'
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
# Autorise les modifications des seuls stages qu'on "possède"
|
|
|
|
queryset = super(StageEdit, self).get_queryset()
|
|
|
|
return queryset.filter(auteur=self.request.user.profil)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(StageEdit, self).get_form_kwargs()
|
|
|
|
kwargs.update({'request': self.request})
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('avisstage:stage', self.instance.id)
|
|
|
|
|
2017-04-07 03:01:27 +02:00
|
|
|
class StageView(DetailView, LoginRequiredMixin):
|
|
|
|
model = Stage
|
|
|
|
template_name = 'avisstage/detail/stage.html'
|
2017-04-11 00:20:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Lieux des stages
|
|
|
|
class LieuForm(forms.ModelForm):
|
|
|
|
coord = LatLonField()
|
2017-04-07 03:01:27 +02:00
|
|
|
|
2017-04-11 00:20:14 +02:00
|
|
|
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'
|
|
|
|
|
|
|
|
|
2017-04-05 00:23:35 +02:00
|
|
|
def recherche(request):
|
|
|
|
return render(request, 'avisstage/recherche.html')
|
|
|
|
|
|
|
|
def feedback(request):
|
|
|
|
return render(request, 'avisstage/feedback.html')
|