# coding: utf-8 from django.shortcuts import render, redirect 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 avisstage.forms import StageForm, LieuForm, AvisStageForm, AvisLieuForm # 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 @login_required def manage_stage(request, id_stage=None): if id_stage is None: stage = Stage(auteur=request.user.profil) avis_stage = AvisStage(stage=stage) c_del = False else: stage = Stage.objects.filter(auteur=request.user.profil).get(pk=id_stage) avis_stage, _ = AvisStage.objects.get_or_create(stage=stage) c_del = True AvisLieuFormSet = forms.inlineformset_factory( Stage, AvisLieu, form=AvisLieuForm, can_delete=c_del) if request.method == "POST": form = StageForm(request.POST, request=request, instance=stage, prefix="stage") avis_stage_form = AvisStageForm(request.POST, instance=avis_stage, prefix="avis") avis_lieu_formset = AvisLieuFormSet(request.POST, instance=stage, prefix="lieux") if (form.is_valid() and avis_stage_form.is_valid() and avis_lieu_formset.is_valid()): stage = form.save() avis_stage_form.instance.stage = stage avis_stage_form.save() avis_lieu_formset.save() return redirect(reverse('avisstage:stage', kwargs={'pk':stage.id})) else: form = StageForm(instance=stage, prefix="stage") avis_stage_form = AvisStageForm(instance=avis_stage, prefix="avis") avis_lieu_formset = AvisLieuFormSet(instance=stage, prefix="lieux") return render(request, "avisstage/formulaires/stage.html", {'form': form, 'avis_stage_form': avis_stage_form, 'avis_lieu_formset': avis_lieu_formset}) 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) def get_context_data(self, *args, **kwargs): ctx = super(StageEdit, self).get_context_data(*args, **kwargs) print ctx return ctx class StageView(DetailView, LoginRequiredMixin): model = Stage template_name = 'avisstage/detail/stage.html' # Lieux des stages 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')