# coding: utf-8 from django.shortcuts import render, redirect, get_object_or_404 from django.views.generic import DetailView, ListView 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 django.http import JsonResponse, HttpResponseForbidden from django.core.mail import send_mail from django.db.models import Q from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage from avisstage.forms import StageForm, LieuForm, AvisStageForm, AvisLieuForm, FeedbackForm import random, math # # LECTURE # # 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 #login_required class ProfilView(LoginRequiredMixin, DetailView): model = Normalien template_name = 'avisstage/detail/profil.html' # Récupération du profil def get_object(self): return Normalien.objects.get(user__username=self.kwargs.get('username')) # Stage #login_required class StageView(LoginRequiredMixin, DetailView): 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(LoginRequiredMixin, ListView): 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') # FAQ def faq(request): return render(request, 'avisstage/faq.html') # # EDITION # # Profil #login_required class ProfilEdit(LoginRequiredMixin, UpdateView): model = Normalien fields = ['nom', 'promotion', 'mail', 'contactez_moi', 'bio'] template_name = 'avisstage/formulaires/profil.html' # Limitation à son propre profil def get_object(self): return self.request.user.profil def get_success_url(self): return reverse('avisstage:perso') # Stage @login_required def manage_stage(request, pk=None): # Objet de base if pk is None: stage = Stage(auteur=request.user.profil) avis_stage = AvisStage(stage=stage) c_del = False else: try: stage = Stage.objects.filter(auteur=request.user.profil).get(pk=pk) except Stage.DoesNotExist: return HttpResponseForbidden() avis_stage, _ = AvisStage.objects.get_or_create(stage=stage) c_del = True # Formset pour les avis des lieux AvisLieuFormSet = forms.inlineformset_factory( Stage, AvisLieu, form=AvisLieuForm, can_delete=c_del, extra=0) if request.method == "POST": # Lecture des données 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") # Validation et enregistrement 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() print request.POST if "continuer" in request.POST: if pk is None: return redirect(reverse('avisstage:stage_edit',kwargs={'pk':stage.id})) else: 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") # Affichage du formulaire return render(request, "avisstage/formulaires/stage.html", {'form': form, 'avis_stage_form': avis_stage_form, 'avis_lieu_formset': avis_lieu_formset, 'creation': pk is None}) # Ajout d'un lieu de stage #login_required # Stage @login_required def save_lieu(request): normalien = request.user.profil if request.method == "POST": pk = request.POST.get("id", None) print request.POST jitter = False if pk is None or pk == '': lieu = Lieu() else: # Modification du lieu lieu = get_object_or_404(Lieu, pk=pk) # On regarde si les stages associés à ce lieu "appartiennent" tous à l'utilisateur not_same_user = lieu.stages.exclude(auteur=normalien).count() # Si d'autres personnes ont un stage à cet endroit, on crée un nouveau lieu, un peu à côté if not_same_user > 0: lieu = Lieu() # Servira à bouger un peu le lieu jitter = True # Lecture des données form = LieuForm(request.POST, instance=lieu) # Validation et enregistrement if form.is_valid(): lieu = form.save() if jitter: cdx, cdy = lieu.coord.get_coords() ang = random.random() * 6.29; rad = (random.random() + 0.5) * 3e-4 cdx += math.cos(ang) * rad; cdy += math.sin(ang) * rad; lieu.coord.set_coords((cdx, cdy)) lieu.save() return JsonResponse({"success": True, "id": lieu.id}) else: return JsonResponse({"success": False, "errors": form.errors}) else: return JsonResponse({"erreur": "Aucune donnée POST"}) class LieuAjout(LoginRequiredMixin, CreateView): model = Lieu form_class = LieuForm template_name = 'avisstage/formulaires/lieu.html' # 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) # Passage d'un stage en mode public @login_required def publier_stage(request, pk): if request.method != "POST": return HttpResponseForbidden() stage = get_object_or_404(Stage, pk=pk) # Stage non possédé par l'utilisateur if stage.auteur != request.user.profil: return HttpResponseForbidden() # Mise à jour du statut if "publier" in request.POST: stage.public = True else: stage.public = False stage.save() return redirect(reverse("avisstage:stage", kwargs={"pk": pk})) # # FEEDBACK # @login_required def feedback(request): 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})