Move emprunt button

This commit is contained in:
Alexandre Iooss 2019-08-10 15:53:23 +02:00
parent 82ebc60982
commit 8613dd2810
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
6 changed files with 25 additions and 49 deletions

View File

@ -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

View File

@ -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(
'<a class="button" href="{}">{}</a>',
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',

View File

@ -8,8 +8,6 @@ from . import views
app_name = 'media'
urlpatterns = [
url(r'^add_emprunt/(?P<userid>[0-9]+)$', views.add_emprunt,
name='add-emprunt'),
url(r'^retour_emprunt/(?P<empruntid>[0-9]+)$', views.retour_emprunt,
name='retour-emprunt'),
]

View File

@ -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):

View File

@ -7,7 +7,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% block content %}
<div id="content-main">
<h2>Welcome to procrastination haven !</h2>
<h2>Welcome to procrastination heaven !</h2>
<p>
Le site va subir progressivement des mises à jour pendant ces vacances.
Si vous rencontrez des instabilités,

View File

@ -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(
'<a class="button" href="{}">{}</a>',
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)