experiENS/avisstage/views.py

201 lines
6.3 KiB
Python
Raw Normal View History

# coding: utf-8
2017-04-20 01:53:29 +02:00
from django.shortcuts import render, redirect, get_object_or_404
2017-04-04 00:31:50 +02:00
2017-04-27 21:42:13 +02:00
from django.views.generic import DetailView, ListView
2017-04-07 03:01:27 +02:00
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-20 01:53:29 +02:00
from django.http import JsonResponse, HttpResponseForbidden
2017-04-20 23:04:07 +02:00
from django.core.mail import send_mail
from django.db.models import Q
2017-04-07 03:01:27 +02:00
from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage
2017-04-20 23:04:07 +02:00
from avisstage.forms import StageForm, LieuForm, AvisStageForm, AvisLieuForm, FeedbackForm
2017-04-07 03:01:27 +02:00
2017-05-02 23:23:26 +02:00
#
# LECTURE
#
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
2017-05-02 23:23:26 +02:00
#login_required
class ProfilView(DetailView, LoginRequiredMixin):
model = Normalien
template_name = 'avisstage/detail/profil.html'
# Récupération de son propre profil
def get_object(self):
return Normalien.objects.get(user__username=self.kwargs.get('username'))
# Stage
#login_required
class StageView(DetailView, LoginRequiredMixin):
model = Stage
template_name = 'avisstage/detail/stage.html'
# Restriction aux stages publics ou personnels
def get_queryset(self):
filtre = Q(auteur__user_id=self.request.user.id) | Q(public=True)
return Stage.objects.filter(filtre)
# Liste des stages par dernière modification
#login_required
class StageListe(ListView, LoginRequiredMixin):
model = Stage
template_name = 'avisstage/liste/stage.html'
def get_queryset(self):
return Stage.objects.filter(public=True).order_by('-date_maj')
# Recherche
@login_required
def recherche(request):
return render(request, 'avisstage/recherche.html')
#
# EDITION
#
# Profil
#login_required
2017-04-07 03:01:27 +02:00
class ProfilEdit(UpdateView, LoginRequiredMixin):
model = Normalien
fields = ['nom', 'promotion', 'mail']
template_name = 'avisstage/formulaires/profil.html'
2017-05-02 23:23:26 +02:00
# Limitation à son propre profil
2017-04-07 03:01:27 +02:00
def get_object(self):
return self.request.user.profil
def get_success_url(self):
return reverse('avisstage:perso')
2017-05-02 23:23:26 +02:00
# Stage
2017-04-13 22:50:00 +02:00
@login_required
2017-04-20 01:53:29 +02:00
def manage_stage(request, pk=None):
2017-05-02 23:23:26 +02:00
# Objet de base
2017-04-20 01:53:29 +02:00
if pk is None:
2017-04-13 22:50:00 +02:00
stage = Stage(auteur=request.user.profil)
avis_stage = AvisStage(stage=stage)
c_del = False
else:
2017-04-20 01:53:29 +02:00
stage = Stage.objects.filter(auteur=request.user.profil).get(pk=pk)
2017-04-13 22:50:00 +02:00
avis_stage, _ = AvisStage.objects.get_or_create(stage=stage)
c_del = True
2017-05-02 23:23:26 +02:00
# Formset pour les avis des lieux
2017-04-13 22:50:00 +02:00
AvisLieuFormSet = forms.inlineformset_factory(
2017-04-15 20:03:08 +02:00
Stage, AvisLieu, form=AvisLieuForm, can_delete=c_del, extra=0)
2017-04-13 22:50:00 +02:00
if request.method == "POST":
2017-05-02 23:23:26 +02:00
# Lecture des données
2017-04-13 22:50:00 +02:00
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")
2017-05-02 23:23:26 +02:00
# Validation et enregistrement
2017-04-13 22:50:00 +02:00
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")
2017-05-02 23:23:26 +02:00
# Affichage du formulaire
2017-04-13 22:50:00 +02:00
return render(request, "avisstage/formulaires/stage.html",
{'form': form, 'avis_stage_form': avis_stage_form,
2017-04-15 20:03:08 +02:00
'avis_lieu_formset': avis_lieu_formset,
2017-04-20 01:53:29 +02:00
'creation': pk is None})
2017-04-27 21:42:13 +02:00
2017-05-02 23:23:26 +02:00
# Ajout d'un lieu de stage
#login_required
class LieuAjout(CreateView, LoginRequiredMixin):
model = Lieu
form_class = LieuForm
template_name = 'avisstage/formulaires/lieu.html'
2017-05-02 23:23:26 +02:00
# Retourne d'un JSON si requête AJAX
def form_valid(self, form):
if self.request.GET.get("format", "") == "json":
self.object = form.save()
return JsonResponse({"success": True,
"id": self.object.id})
else:
super(LieuAjout, self).form_valid(form)
def form_invalid(self, form):
if self.request.GET.get("format", "") == "json":
return JsonResponse({"success": False,
"errors": form.errors})
else:
super(LieuAjout, self).form_valid(form)
2017-04-20 01:53:29 +02:00
2017-05-02 23:23:26 +02:00
# Passage d'un stage en mode public
2017-04-20 01:53:29 +02:00
@login_required
def publier_stage(request, pk):
if request.method != "POST":
return HttpResponseForbidden()
stage = get_object_or_404(Stage, pk=pk)
2017-05-02 23:23:26 +02:00
# Stage non possédé par l'utilisateur
2017-04-20 01:53:29 +02:00
if stage.auteur != request.user.profil:
return HttpResponseForbidden()
2017-05-02 23:23:26 +02:00
# Mise à jour du statut
2017-04-20 01:53:29 +02:00
if "publier" in request.POST:
stage.public = True
else:
stage.public = False
2017-05-02 23:23:26 +02:00
2017-04-20 01:53:29 +02:00
stage.save()
2017-05-02 23:23:26 +02:00
2017-04-20 01:53:29 +02:00
return redirect(reverse("avisstage:stage", kwargs={"pk": pk}))
2017-04-20 23:04:07 +02:00
2017-05-02 23:23:26 +02:00
#
2017-04-20 23:04:07 +02:00
# FEEDBACK
2017-05-02 23:23:26 +02:00
#
2017-04-20 23:04:07 +02:00
@login_required
2017-04-05 00:23:35 +02:00
def feedback(request):
2017-04-20 23:04:07 +02:00
if request.method == "POST":
form = FeedbackForm(request.POST)
if form.is_valid():
objet = form.cleaned_data['objet']
message = form.cleaned_data['message']
send_mail(
"[experiENS] "+ objet,
message,
request.user.username + "@clipper.ens.fr",
['champeno@clipper.ens.fr'],
fail_silently=False,
)
if request.GET.get("format", None) == "json":
return JsonResponse({"success": True})
return redirect(reverse("avisstage:index"))
else:
if request.GET.get("format", None) == "json":
return JsonResponse({"success": False,
"errors": form.errors})
else:
form = FeedbackForm()
return render(request, 'avisstage/formulaire/feedback.html', {"form": form})