1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-12-26 05:02:24 +00:00
med/media/admin.py

110 lines
3.7 KiB
Python
Raw Normal View History

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
2019-08-10 13:53:23 +00:00
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
2019-08-02 12:57:53 +00:00
from reversion.admin import VersionAdmin
2017-06-30 01:25:07 +00:00
2019-08-10 06:04:46 +00:00
from med.admin import admin_site
2019-08-11 08:40:39 +00:00
from .forms import MediaAdminForm
2019-08-09 21:22:20 +00:00
from .models import Auteur, Emprunt, Jeu, Media
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
class AuteurAdmin(VersionAdmin):
2019-08-16 12:01:13 +00:00
list_display = ('name',)
search_fields = ('name',)
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
class MediaAdmin(VersionAdmin):
2019-08-15 09:37:10 +00:00
list_display = ('__str__', 'authors_list', 'side_identifier', 'isbn',
2019-08-11 08:40:39 +00:00
'external_link')
2019-08-16 12:01:13 +00:00
search_fields = ('title', 'authors__name', 'side_identifier', 'subtitle',
2019-08-11 08:49:04 +00:00
'isbn')
2019-08-11 07:22:22 +00:00
autocomplete_fields = ('authors',)
date_hierarchy = 'publish_date'
2019-08-11 08:40:39 +00:00
form = MediaAdminForm
2019-08-08 19:08:24 +00:00
2019-08-11 07:22:22 +00:00
def authors_list(self, obj):
2019-08-16 12:01:13 +00:00
return ", ".join([a.name for a in obj.authors.all()])
2019-08-11 07:22:22 +00:00
authors_list.short_description = _('authors')
2019-08-02 12:57:53 +00:00
2019-08-11 08:40:39 +00:00
def external_link(self, obj):
2019-08-11 08:49:04 +00:00
if obj.external_url:
return format_html('<a href="{}" target="about:blank">{}</a>',
obj.external_url, obj.external_url)
else:
return "-"
2019-08-11 08:40:39 +00:00
external_link.allow_tags = True
external_link.short_description = _('external url')
def get_form(self, request, obj=None, **kwargs):
"""
Pass request to form (for ISBN magic)
"""
form = super().get_form(request, obj=obj, **kwargs)
form.request = request
return form
def changeform_view(self, request, object_id=None, form_url='',
extra_context=None):
"""
We use _continue for ISBN fetching, so remove continue button
"""
extra_context = extra_context or {}
extra_context['show_save_and_continue'] = False
return super().changeform_view(request, object_id, form_url,
extra_context=extra_context)
2017-06-30 01:25:07 +00:00
class EmpruntAdmin(VersionAdmin):
2019-08-08 19:08:24 +00:00
list_display = ('media', 'user', 'date_emprunt', 'date_rendu',
2019-08-10 13:53:23 +00:00
'permanencier_emprunt', 'permanencier_rendu_custom')
2019-08-11 08:49:04 +00:00
search_fields = ('media__title', 'media__side_identifier',
'user__username', 'date_emprunt', 'date_rendu')
2019-08-10 08:44:17 +00:00
date_hierarchy = 'date_emprunt'
2019-08-10 09:42:27 +00:00
autocomplete_fields = ('media', 'user', 'permanencier_emprunt',
'permanencier_rendu')
2019-08-02 12:57:53 +00:00
2019-08-10 13:53:23 +00:00
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')
)
2019-08-16 12:01:13 +00:00
permanencier_rendu_custom.short_description = _('given back to')
2019-08-10 13:53:23 +00:00
permanencier_rendu_custom.allow_tags = True
2019-08-16 12:34:16 +00:00
def add_view(self, request, form_url='', extra_context=None):
"""
Autoselect keyholder registering a new borrowed item
"""
# Make GET data mutable
data = request.GET.copy()
data['permanencier_emprunt'] = request.user
request.GET = data
return super().add_view(request, form_url, extra_context)
2017-06-30 01:25:07 +00:00
2017-07-03 16:33:24 +00:00
class JeuAdmin(VersionAdmin):
2019-08-16 12:01:13 +00:00
list_display = ('name', 'proprietaire', 'duree', 'nombre_joueurs_min',
2019-08-08 19:08:24 +00:00
'nombre_joueurs_max', 'comment')
2019-08-16 12:01:13 +00:00
search_fields = ('name', 'proprietaire__username', 'duree', 'comment')
2019-08-10 09:42:27 +00:00
autocomplete_fields = ('proprietaire',)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
2019-08-10 06:04:46 +00:00
admin_site.register(Auteur, AuteurAdmin)
admin_site.register(Media, MediaAdmin)
admin_site.register(Emprunt, EmpruntAdmin)
admin_site.register(Jeu, JeuAdmin)