From 8613dd28104d4a3dbdd3a2fae34711b81ece88bd Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Sat, 10 Aug 2019 15:53:23 +0200 Subject: [PATCH] Move emprunt button --- med/admin.py | 4 +++- media/admin.py | 21 ++++++++++++++++++++- media/urls.py | 2 -- media/views.py | 32 -------------------------------- theme/templates/admin/index.html | 2 +- users/admin.py | 13 +------------ 6 files changed, 25 insertions(+), 49 deletions(-) diff --git a/med/admin.py b/med/admin.py index b3662bd..e6365ff 100644 --- a/med/admin.py +++ b/med/admin.py @@ -22,7 +22,9 @@ class DatabaseAdmin(AdminSite): response = super().index(request, extra_context) # User is always authenticated - user_borrowed = Emprunt.objects.filter(user=request.user) + # Get currently borrowed items + user_borrowed = Emprunt.objects.filter(user=request.user, + date_rendu=None) response.context_data["borrowed_items"] = user_borrowed return response diff --git a/media/admin.py b/media/admin.py index 1e7abb1..3d0d846 100644 --- a/media/admin.py +++ b/media/admin.py @@ -2,6 +2,9 @@ # Copyright (C) 2017-2019 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from django.urls import reverse +from django.utils.html import format_html +from django.utils.translation import ugettext_lazy as _ from reversion.admin import VersionAdmin from med.admin import admin_site @@ -24,13 +27,29 @@ class MediaAdmin(VersionAdmin): class EmpruntAdmin(VersionAdmin): list_display = ('media', 'user', 'date_emprunt', 'date_rendu', - 'permanencier_emprunt', 'permanencier_rendu') + 'permanencier_emprunt', 'permanencier_rendu_custom') search_fields = ('media__titre', 'media__cote', 'user__username', 'date_emprunt', 'date_rendu') date_hierarchy = 'date_emprunt' autocomplete_fields = ('media', 'user', 'permanencier_emprunt', 'permanencier_rendu') + def permanencier_rendu_custom(self, obj): + """ + Show a button if item has not been returned yet + """ + if obj.permanencier_rendu: + return obj.permanencier_rendu + else: + return format_html( + '{}', + reverse('media:retour-emprunt', args=[obj.pk]), + _('Turn back') + ) + + permanencier_rendu_custom.short_description = _('permanencier rendu') + permanencier_rendu_custom.allow_tags = True + class JeuAdmin(VersionAdmin): list_display = ('nom', 'proprietaire', 'duree', 'nombre_joueurs_min', diff --git a/media/urls.py b/media/urls.py index 429fd4f..1e7a45a 100644 --- a/media/urls.py +++ b/media/urls.py @@ -8,8 +8,6 @@ from . import views app_name = 'media' urlpatterns = [ - url(r'^add_emprunt/(?P[0-9]+)$', views.add_emprunt, - name='add-emprunt'), url(r'^retour_emprunt/(?P[0-9]+)$', views.retour_emprunt, name='retour-emprunt'), ] diff --git a/media/views.py b/media/views.py index 2e6950d..a225a17 100644 --- a/media/views.py +++ b/media/views.py @@ -11,8 +11,6 @@ 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 @@ -22,36 +20,6 @@ def form(ctx, template, 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): diff --git a/theme/templates/admin/index.html b/theme/templates/admin/index.html index d8bf3a8..54fbf34 100644 --- a/theme/templates/admin/index.html +++ b/theme/templates/admin/index.html @@ -7,7 +7,7 @@ SPDX-License-Identifier: GPL-3.0-or-later {% block content %}
-

Welcome to procrastination haven !

+

Welcome to procrastination heaven !

Le site va subir progressivement des mises à jour pendant ces vacances. Si vous rencontrez des instabilités, diff --git a/users/admin.py b/users/admin.py index c98a02e..f4adab3 100644 --- a/users/admin.py +++ b/users/admin.py @@ -59,7 +59,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): (_('Important dates'), {'fields': ('last_login', 'date_joined')}), ) list_display = ('username', 'email', 'first_name', 'last_name', - 'maxemprunt', 'is_adherent', 'is_staff', 'actions_btn') + 'maxemprunt', 'is_adherent', 'is_staff') list_filter = (IsAdherentFilter, 'is_staff', 'is_superuser', 'is_active', 'groups') @@ -114,17 +114,6 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): is_adherent.short_description = _('is adherent') is_adherent.allow_tags = True - def actions_btn(self, obj): - # TODO permit adhere only if perms.media.add_emprunt - return format_html( - '{}', - reverse('media:add-emprunt', args=[obj.pk]), - _('Register borrowed item') - ) - - actions_btn.short_description = _('actions') - actions_btn.allow_tags = True - admin_site.register(User, UserAdmin) admin_site.register(Adhesion, AdhesionAdmin)