2019-08-02 12:57:53 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
|
|
from django.db import transaction
|
2019-08-02 12:57:53 +00:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.template.context_processors import csrf
|
|
|
|
from django.utils import timezone
|
2017-06-30 01:25:07 +00:00
|
|
|
from reversion import revisions as reversion
|
|
|
|
|
2017-07-03 00:32:13 +00:00
|
|
|
from med.settings import PAGINATION_NUMBER
|
2019-08-02 12:57:53 +00:00
|
|
|
from users.models import User
|
2019-08-08 19:08:24 +00:00
|
|
|
from .forms import EmpruntForm, EditEmpruntForm
|
2019-08-02 12:57:53 +00:00
|
|
|
from .models import Auteur, Media, Jeu, Emprunt
|
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
|
|
|
|
def form(ctx, template, request):
|
|
|
|
c = ctx
|
|
|
|
c.update(csrf(request))
|
|
|
|
return render(request, template, c)
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
@login_required
|
2019-08-08 14:33:05 +00:00
|
|
|
@permission_required('media.add_emprunt')
|
2017-06-30 01:25:07 +00:00
|
|
|
def add_emprunt(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
2019-08-02 12:57:53 +00:00
|
|
|
messages.error(request, u"Entrée inexistante")
|
2017-06-30 01:25:07 +00:00
|
|
|
return redirect("/media/index_emprunts/")
|
2019-08-08 14:33:05 +00:00
|
|
|
emprunts_en_cours = Emprunt.objects.filter(date_rendu=None,
|
|
|
|
user=user).count()
|
2017-06-30 01:25:07 +00:00
|
|
|
if emprunts_en_cours >= user.maxemprunt:
|
2019-08-08 14:33:05 +00:00
|
|
|
messages.error(request,
|
|
|
|
"Maximum d'emprunts atteint de l'user %s" % user.maxemprunt)
|
2017-06-30 01:25:07 +00:00
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
emprunt = EmpruntForm(request.POST or None)
|
|
|
|
if emprunt.is_valid():
|
|
|
|
emprunt = emprunt.save(commit=False)
|
|
|
|
emprunt.user = user
|
|
|
|
emprunt.permanencier_emprunt = request.user
|
|
|
|
emprunt.date_emprunt = timezone.now()
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
emprunt.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
|
|
|
messages.success(request, "Le emprunt a été ajouté")
|
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
return form({'mediaform': emprunt}, 'media/media.html', request)
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
@login_required
|
2019-08-08 17:16:54 +00:00
|
|
|
@permission_required('media.change_emprunt')
|
2017-06-30 01:25:07 +00:00
|
|
|
def edit_emprunt(request, empruntid):
|
|
|
|
try:
|
|
|
|
emprunt_instance = Emprunt.objects.get(pk=empruntid)
|
|
|
|
except Emprunt.DoesNotExist:
|
2019-08-02 12:57:53 +00:00
|
|
|
messages.error(request, u"Entrée inexistante")
|
2017-06-30 01:25:07 +00:00
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
emprunt = EditEmpruntForm(request.POST or None, instance=emprunt_instance)
|
|
|
|
if emprunt.is_valid():
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
emprunt.save()
|
|
|
|
reversion.set_user(request.user)
|
2019-08-08 14:33:05 +00:00
|
|
|
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for field in emprunt.changed_data))
|
2017-06-30 01:25:07 +00:00
|
|
|
messages.success(request, "Emprunt modifié")
|
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
return form({'mediaform': emprunt}, 'media/media.html', request)
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
@login_required
|
2019-08-08 17:16:54 +00:00
|
|
|
@permission_required('media.change_emprunt')
|
2017-06-30 01:25:07 +00:00
|
|
|
def retour_emprunt(request, empruntid):
|
|
|
|
try:
|
|
|
|
emprunt_instance = Emprunt.objects.get(pk=empruntid)
|
|
|
|
except Emprunt.DoesNotExist:
|
2019-08-02 12:57:53 +00:00
|
|
|
messages.error(request, u"Entrée inexistante")
|
2017-06-30 01:25:07 +00:00
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
emprunt_instance.permanencier_rendu = request.user
|
|
|
|
emprunt_instance.date_rendu = timezone.now()
|
|
|
|
emprunt_instance.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
messages.success(request, "Retour enregistré")
|
|
|
|
return redirect("/media/index_emprunts/")
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-07-03 16:33:24 +00:00
|
|
|
@login_required
|
|
|
|
def index_jeux(request):
|
|
|
|
jeux_list = Jeu.objects.all()
|
|
|
|
paginator = Paginator(jeux_list, PAGINATION_NUMBER)
|
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
jeux_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
jeux_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
jeux_list = paginator.page(paginator.num_pages)
|
2019-08-02 12:57:53 +00:00
|
|
|
return render(request, 'media/index_jeux.html', {'jeux_list': jeux_list})
|
|
|
|
|
2017-07-03 16:33:24 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
@login_required
|
|
|
|
def index_auteurs(request):
|
2017-07-03 23:12:51 +00:00
|
|
|
auteurs_list = Auteur.objects.all().order_by('nom')
|
2017-07-03 00:32:13 +00:00
|
|
|
paginator = Paginator(auteurs_list, PAGINATION_NUMBER)
|
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
auteurs_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
auteurs_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
auteurs_list = paginator.page(paginator.num_pages)
|
2019-08-08 14:33:05 +00:00
|
|
|
return render(request, 'media/index_auteurs.html',
|
|
|
|
{'auteurs_list': auteurs_list})
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def index_medias(request):
|
|
|
|
medias_list = Media.objects.all()
|
2017-07-03 00:32:13 +00:00
|
|
|
paginator = Paginator(medias_list, PAGINATION_NUMBER)
|
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
medias_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
medias_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
medias_list = paginator.page(paginator.num_pages)
|
2019-08-08 14:33:05 +00:00
|
|
|
return render(request, 'media/index_medias.html',
|
|
|
|
{'medias_list': medias_list})
|
2017-06-30 01:25:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2019-08-08 17:16:54 +00:00
|
|
|
def index_emprunt(request):
|
2017-06-30 01:25:07 +00:00
|
|
|
if request.user.has_perms(['perm']):
|
|
|
|
emprunts_list = Emprunt.objects.all()
|
|
|
|
else:
|
|
|
|
emprunts_list = Emprunt.objects.filter(user=request.user)
|
2019-08-08 14:33:05 +00:00
|
|
|
paginator = Paginator(emprunts_list.order_by('date_emprunt').reverse(),
|
|
|
|
PAGINATION_NUMBER)
|
2017-07-03 15:48:56 +00:00
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
emprunts_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
emprunts_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
emprunts_list = paginator.page(paginator.num_pages)
|
2019-08-08 14:33:05 +00:00
|
|
|
return render(request, 'media/index_emprunts.html',
|
|
|
|
{'emprunts_list': emprunts_list})
|
2019-08-08 17:16:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
"""
|
|
|
|
Home page with user's borrowed items
|
|
|
|
"""
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
borrowed_items = Emprunt.objects.filter(user=request.user)
|
|
|
|
else:
|
|
|
|
borrowed_items = []
|
|
|
|
|
|
|
|
return render(request, 'media/index.html', {
|
|
|
|
'borrowed_items': borrowed_items,
|
|
|
|
})
|