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.db import transaction
|
2019-08-09 21:22:20 +00:00
|
|
|
from django.shortcuts import redirect, render
|
2019-08-02 12:57:53 +00:00
|
|
|
from django.template.context_processors import csrf
|
|
|
|
from django.utils import timezone
|
2019-08-10 06:04:46 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2017-06-30 01:25:07 +00:00
|
|
|
from reversion import revisions as reversion
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
from users.models import User
|
2019-08-09 20:26:32 +00:00
|
|
|
from .forms import EmpruntForm
|
|
|
|
from .models import Emprunt
|
2019-08-02 12:57:53 +00:00
|
|
|
|
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")
|
2019-08-10 08:44:17 +00:00
|
|
|
return redirect("admin:media_emprunt_changelist")
|
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,
|
2019-08-09 21:22:20 +00:00
|
|
|
"Maximum d'emprunts atteint de "
|
|
|
|
"l'user %s" % user.maxemprunt)
|
2019-08-10 08:44:17 +00:00
|
|
|
return redirect("admin:media_emprunt_changelist")
|
2017-06-30 01:25:07 +00:00
|
|
|
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é")
|
2019-08-10 08:44:17 +00:00
|
|
|
return redirect("admin:media_emprunt_changelist")
|
2019-08-09 20:26:32 +00:00
|
|
|
return form({'form': emprunt}, 'media/media.html', request)
|
2017-06-30 01:25:07 +00:00
|
|
|
|
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")
|
2019-08-10 08:44:17 +00:00
|
|
|
return redirect("admin:media_emprunt_changelist")
|
2017-06-30 01:25:07 +00:00
|
|
|
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é")
|
2019-08-10 08:44:17 +00:00
|
|
|
return redirect("admin:media_emprunt_changelist")
|
2017-06-30 01:25:07 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2019-08-08 17:16:54 +00:00
|
|
|
def index(request):
|
|
|
|
"""
|
2019-08-10 06:04:46 +00:00
|
|
|
Home page which redirect to admin when logged in
|
2019-08-08 17:16:54 +00:00
|
|
|
"""
|
|
|
|
if request.user.is_authenticated:
|
2019-08-10 06:04:46 +00:00
|
|
|
return redirect('admin:index')
|
2019-08-08 17:16:54 +00:00
|
|
|
else:
|
2019-08-10 06:04:46 +00:00
|
|
|
return render(request, 'admin/index.html', {
|
|
|
|
'title': _('Welcome to the Mediatek database'),
|
|
|
|
})
|