ernestophone.ens.fr/instruments/views.py

130 lines
4.4 KiB
Python
Raw Normal View History

2021-04-29 00:33:58 +02:00
from django.contrib.auth.mixins import LoginRequiredMixin
2021-04-29 00:27:33 +02:00
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy
2021-04-01 21:27:17 +02:00
from django.utils.safestring import mark_safe
2021-04-29 00:33:58 +02:00
from django.views.generic import (CreateView, DeleteView, TemplateView,
UpdateView)
2021-04-29 00:27:33 +02:00
2022-01-06 13:11:16 +01:00
from gestion.mixins import ChefInstruRequiredMixin, ChefRequiredMixin
2021-04-29 00:27:33 +02:00
from gestion.models import Photo
from instruments.forms import ChefEditInstrumentForm, ChefReparationForm
from instruments.models import Instrument, Reparation
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
class ListeInstru(LoginRequiredMixin, TemplateView):
template_name = "instruments/instru_liste.html"
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
context["photo"] = Photo.objects.filter(cat="instru").order_by("?").first()
context["instrus_dispo"] = (
Instrument.objects.all().order_by("type").filter(statut="Disponible")
)
context["instrus_prete"] = (
Instrument.objects.all().order_by("type").filter(statut="Prêté")
)
2021-04-29 00:27:33 +02:00
return context
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
2021-10-23 11:16:07 +02:00
class CreateInstru(ChefInstruRequiredMixin, CreateView):
2021-04-01 21:27:17 +02:00
model = Instrument
2022-01-06 13:11:16 +01:00
fields = [
"owner",
"user",
"etat",
"type",
"marque",
"model",
"serial",
"annee",
"prix",
]
2021-04-01 21:27:17 +02:00
template_name = "instruments/create_instru.html"
success_url = reverse_lazy("instruments:liste")
2021-10-23 11:16:07 +02:00
class CreateRep(ChefInstruRequiredMixin, TemplateView):
2021-04-29 00:27:33 +02:00
form_class = ChefReparationForm
template_name = "instruments/create_rep.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = self.form_class()
2021-04-29 00:33:58 +02:00
context["pk"] = self.kwargs["pk"]
2021-04-29 00:27:33 +02:00
return context
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
2021-05-27 14:27:30 +02:00
2021-04-01 21:27:17 +02:00
if form.is_valid():
2021-05-27 14:27:30 +02:00
rep = form.save(commit=False)
2021-04-29 00:33:58 +02:00
rep.instru = get_object_or_404(Instrument, id=self.kwargs["pk"])
2021-04-01 21:27:17 +02:00
rep.save()
2021-04-29 00:33:58 +02:00
return redirect("instruments:fiche_instru", pk=self.kwargs["pk"])
2021-04-29 00:27:33 +02:00
else:
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["form"] = form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2021-04-01 21:27:17 +02:00
2021-10-23 11:16:07 +02:00
class DeleteRep(ChefInstruRequiredMixin, TemplateView):
2021-04-29 00:27:33 +02:00
model = Reparation
template_name = "instruments/delete_instru.html"
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
def get(self, request, *args, **kwargs):
2021-04-01 21:27:17 +02:00
2021-04-29 00:33:58 +02:00
rep = get_object_or_404(self.model, id=self.kwargs["pk"])
2021-04-29 00:27:33 +02:00
id_instru = rep.instru.id
rep.delete()
return redirect("instruments:fiche_instru", pk=id_instru)
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
class FicheInstru(LoginRequiredMixin, TemplateView):
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
template_name = "instruments/update_instru.html"
model = Instrument
2021-04-01 21:27:17 +02:00
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
instru = get_object_or_404(self.model, id=self.kwargs["pk"])
reparations = Reparation.objects.filter(instru=self.kwargs["pk"]).order_by(
"-date"
)
2021-04-29 00:27:33 +02:00
form = ChefEditInstrumentForm(instance=instru)
infos = mark_safe(instru.infos)
infos_en = mark_safe(instru.infos_en)
context["reparations"] = reparations
context["instru"] = instru
context["form"] = form
context["infos"] = infos
context["infos_en"] = infos_en
return context
def post(self, request, *args, **kwargs):
2021-04-29 00:33:58 +02:00
instru = get_object_or_404(self.model, id=self.kwargs["pk"])
2021-04-29 00:27:33 +02:00
form = ChefEditInstrumentForm(request.POST, instance=instru)
if request.user.profile.is_chef:
if form.is_valid():
form.save()
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["form"] = form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2021-10-23 11:16:07 +02:00
class UpdateRep(ChefInstruRequiredMixin, UpdateView):
2021-04-01 21:27:17 +02:00
model = Reparation
2021-04-29 00:27:33 +02:00
fields = ["date", "description", "description_en", "prix", "lieux"]
2021-04-01 21:27:17 +02:00
template_name = "instruments/update_rep.html"
def get_success_url(self):
2021-04-29 00:33:58 +02:00
id_instru = get_object_or_404(Reparation, id=self.kwargs["pk"]).instru.id
return reverse_lazy("instruments:fiche_instru", kwargs={"pk": id_instru})
2021-04-29 00:27:33 +02:00
2021-04-01 21:27:17 +02:00
2021-10-23 11:16:07 +02:00
class DeleteInstru(ChefInstruRequiredMixin, DeleteView):
2021-04-01 21:27:17 +02:00
model = Instrument
template_name = "instruments/delete_instru.html"
success_url = reverse_lazy("instruments:liste")