Commit 068eb445 authored by jocelyn's avatar jocelyn
Browse files

Allow giving back an item from its the loan inline

parent b6e52e8e
......@@ -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,6 +19,21 @@ User = get_user_model()
admin.site.register(ItemType)
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
redirect_url = request.META['HTTP_REFERER']
loan = get_object_or_404(Loan, pk=id)
loan.item.give_back()
messages.success(
request,
"{} a bien été marqué comme rendu".format(loan.item),
)
return HttpResponseRedirect(redirect_url)
class OwnerFilter(admin.SimpleListFilter):
title = "Propriétaire"
parameter_name = 'owner'
......@@ -58,8 +76,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 +91,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=[obj.pk]))
else:
return ''
action_buttons.short_description = 'Actions'
action_buttons.allow_tags = True
class LoanHistoryInline(admin.TabularInline):
model = Loan
......@@ -169,6 +196,16 @@ class ItemAdmin(admin.ModelAdmin):
item.give_back()
give_back.short_description = 'Rendre le matériel'
def get_urls(self):
urls = super(ItemAdmin, self).get_urls()
my_urls = [
url(
r'^give_back/(?P<id>.+)$',
self.admin_site.admin_view(give_back_loan),
name='loan-give_back'),
]
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