82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
# -*- mode: python; coding: utf-8 -*-
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
from django.db import transaction
|
|
from django.shortcuts import redirect, render
|
|
from django.template.context_processors import csrf
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
from reversion import revisions as reversion
|
|
|
|
from users.models import User
|
|
from .forms import EmpruntForm
|
|
from .models import Emprunt
|
|
|
|
|
|
def form(ctx, template, request):
|
|
c = ctx
|
|
c.update(csrf(request))
|
|
return render(request, template, c)
|
|
|
|
|
|
@login_required
|
|
@permission_required('media.add_emprunt')
|
|
def add_emprunt(request, userid):
|
|
try:
|
|
user = User.objects.get(pk=userid)
|
|
except User.DoesNotExist:
|
|
messages.error(request, u"Entrée inexistante")
|
|
return redirect("admin:media_emprunt_changelist")
|
|
emprunts_en_cours = Emprunt.objects.filter(date_rendu=None,
|
|
user=user).count()
|
|
if emprunts_en_cours >= user.maxemprunt:
|
|
messages.error(request,
|
|
"Maximum d'emprunts atteint de "
|
|
"l'user %s" % user.maxemprunt)
|
|
return redirect("admin:media_emprunt_changelist")
|
|
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("admin:media_emprunt_changelist")
|
|
return form({'form': emprunt}, 'media/media.html', request)
|
|
|
|
|
|
@login_required
|
|
@permission_required('media.change_emprunt')
|
|
def retour_emprunt(request, empruntid):
|
|
try:
|
|
emprunt_instance = Emprunt.objects.get(pk=empruntid)
|
|
except Emprunt.DoesNotExist:
|
|
messages.error(request, u"Entrée inexistante")
|
|
return redirect("admin:media_emprunt_changelist")
|
|
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("admin:media_emprunt_changelist")
|
|
|
|
|
|
def index(request):
|
|
"""
|
|
Home page which redirect to admin when logged in
|
|
"""
|
|
if request.user.is_authenticated:
|
|
return redirect('admin:index')
|
|
else:
|
|
return render(request, 'admin/index.html', {
|
|
'title': _('Welcome to the Mediatek database'),
|
|
})
|