# coding: utf-8 from django.shortcuts import render 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 from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage from widget import LatLonField # Page d'accueil def index(request): return render(request, 'avisstage/index.html') # Espace personnel @login_required def perso(request): return render(request, 'avisstage/perso.html') # 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 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 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 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) class StageView(DetailView, LoginRequiredMixin): model = Stage template_name = 'avisstage/detail/stage.html' # 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') def feedback(request): return render(request, 'avisstage/feedback.html')