ernestophone.ens.fr/instruments/views.py

105 lines
4.1 KiB
Python
Raw Normal View History

2021-04-01 21:27:17 +02:00
from django.shortcuts import render, HttpResponse, get_object_or_404, redirect, reverse
from instruments.models import Instrument,Reparation
from gestion.models import Photo
from django.contrib.auth.decorators import login_required
from django.forms.models import modelform_factory
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.core.files import File
from django.utils.encoding import smart_str
from django.http import Http404
from instruments.decorators import chef_required
from django.conf import settings
from django.db.models import Q
import os
import zipfile
import io
from gestion.mixins import ChefRequiredMixin
from django.views.generic import CreateView, DeleteView, ListView, UpdateView
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.http import HttpResponseRedirect
@login_required
def liste(request):
photo = Photo.objects.filter(cat='instru').order_by('?').first()
instrus_dispo = Instrument.objects.all().order_by('type').filter(statut='Disponible')
instrus_prete = Instrument.objects.all().order_by('type').filter(statut='Prêté')
return render(request, 'instruments/instru_liste.html', {"instrus_dispo":instrus_dispo,"instrus_prete":instrus_prete,"photo":photo})
class InstruCreate(ChefRequiredMixin, CreateView):
model = Instrument
fields = ["owner",'etat','type','marque','model','serial','annee','prix']
template_name = "instruments/create_instru.html"
success_url = reverse_lazy("instruments:liste")
@chef_required
def create_rep(request, pk):
ChefEditForm = modelform_factory(Reparation,
fields=("date","description","description_en","prix",'lieux'))
if request.method == "POST":
form = ChefEditForm(request.POST)
if form.is_valid():
rep = Reparation()
rep.date = form.cleaned_data['date']
rep.description = form.cleaned_data['description']
rep.description_en = form.cleaned_data['description_en']
rep.prix = form.cleaned_data['prix']
rep.lieux = form.cleaned_data['lieux']
rep.id_instru = get_object_or_404(Instrument, id=pk)
rep.save()
return redirect( 'instruments:fiche_instru', pk=pk)
else:
form = ChefEditForm(request.POST)
return render(request, 'instruments/create_rep.html', locals())
@chef_required
def delete_rep(request,pk):
rep = get_object_or_404(Reparation, id=pk)
id_instru = rep.instru.id
rep.delete()
suppression = _("Réparation supprimée")
return redirect('instruments:fiche_instru',pk=id_instru)
@login_required
def fiche_instru(request, pk):
instru = get_object_or_404(Instrument, id=pk)
reparations= Reparation.objects.filter(instru = pk).order_by('-date')
ChefEditForm = modelform_factory(Instrument,
fields=("statut","user","infos","type","owner","marque","model","serial","annee","prix","infos_en"))
if request.method == "POST" and request.user.profile.is_chef:
form = ChefEditForm(request.POST, instance=instru)
if form.is_valid():
form.save()
form = ChefEditForm(instance=instru)
infos = mark_safe(instru.infos)
infos_en = mark_safe(instru.infos_en)
return render(request, 'instruments/update_instru.html', locals())
class RepUpdate(ChefRequiredMixin, UpdateView):
model = Reparation
fields = ["date","description","description_en","prix",'lieux']
template_name = "instruments/update_rep.html"
def get_success_url(self):
# if you are passing 'pk' from 'urls' to 'DeleteView' for company
# capture that 'pk' as companyid and pass it to 'reverse_lazy()' function
id_instru=get_object_or_404(Reparation, id=self.kwargs['pk']).instru.id
return reverse_lazy('instruments:fiche_instru', kwargs={'pk': id_instru})
class InstruDelete(ChefRequiredMixin, DeleteView):
model = Instrument
template_name = "instruments/delete_instru.html"
success_url = reverse_lazy("instruments:liste")