Commit 6c25ff36 authored by jocelyn's avatar jocelyn

Allow giving back an item from its the loan inline

parent 9eb5394b
......@@ -2,9 +2,12 @@
from __future__ import unicode_literals
from django.contrib import admin
from django.conf.urls import url
from django.shortcuts import get_object_or_404
from django.contrib import admin, messages
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
import autocomplete_light
from .models import ItemType, Item, Loan, Storage
......@@ -16,8 +19,47 @@ User = get_user_model()
def _give_back_success_messages(items, request):
for item in items:
messages.success(request, "{} a bien été marqué comme rendu.".format(item))
if len(items) > 1:
"N'oubliez pas de renseigner, pour chaque retour, le nouvel emprunteur "
+ "ou le nouveau lieu de stockage.",
"N'oubliez pas de renseigner le nouvel emprunteur "
+ "ou le nouveau lieu de stockage.",
def give_back_loan(request, id):
# could be better : could not be a POST, and could not rely on HTTP_REFERER
# We could for that rely on django-inline-actions, but the version
# we could use with our Django 1.9 is outdated and buggy
# We could also offer an intermediate page to specify the storage
loan = get_object_or_404(Loan, pk=id)
redirect_url = request.META['HTTP_REFERER']
except KeyError:
# Fallback if no referer header is present
redirect_url = reverse(
_give_back_success_messages([loan.item], request)
return HttpResponseRedirect(redirect_url)
class OwnerFilter(admin.SimpleListFilter):
title = "Propriétaire"
title = "propriétaire"
parameter_name = 'owner'
def lookups(self, request, model_admin):
......@@ -58,8 +100,8 @@ class AvailabilityFilter(admin.SimpleListFilter):
class CurrentLoanInline(admin.TabularInline):
model = Loan
extra = 0
fields = ('user', 'item', 'short_date', 'notes')
readonly_fields = ('user', 'item', 'short_date', 'notes')
fields = ('user', 'item', 'short_date', 'notes', 'action_buttons')
readonly_fields = ('user', 'item', 'short_date', 'notes', 'action_buttons')
verbose_name_plural = "Emprunt en cours"
show_change_link = True
......@@ -73,6 +115,15 @@ class CurrentLoanInline(admin.TabularInline):
def has_delete_permission(self, request, obj=None):
return False
def action_buttons(self, obj):
if obj.is_running():
return """<a class="button "href="{}">Déclarer rendu</a>""".format(
reverse('admin:loan-give_back', args=[]))
return ''
action_buttons.short_description = 'Actions'
action_buttons.allow_tags = True
class LoanHistoryInline(admin.TabularInline):
model = Loan
......@@ -165,10 +216,22 @@ class ItemAdmin(admin.ModelAdmin):
inlines = [AddLoanInline, CurrentLoanInline, LoanHistoryInline]
def give_back(self, request, queryset):
for item in queryset.filter(loans__loan_date_end=None):
items = queryset.filter(loans__loan_date_end=None)
for item in items:
_give_back_success_messages(items, request)
give_back.short_description = 'Rendre le matériel'
def get_urls(self):
urls = super(ItemAdmin, self).get_urls()
my_urls = [
return my_urls + urls
class StatusFilter(admin.SimpleListFilter):
title = 'Statut'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment