1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-12-27 05:42:25 +00:00
med/media/admin.py
2020-05-24 17:47:37 +02:00

160 lines
5.4 KiB
Python

# -*- mode: python; coding: utf-8 -*-
# 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
from .forms import MediaAdminForm
from .models import Auteur, BD, CD, Emprunt, FutureMedia, Jeu, Manga,\
Revue, Roman, Vinyle
class AuteurAdmin(VersionAdmin):
list_display = ('name',)
search_fields = ('name',)
class MediaAdmin(VersionAdmin):
list_display = ('__str__', 'authors_list', 'side_identifier', 'isbn',
'external_link')
search_fields = ('title', 'authors__name', 'side_identifier', 'subtitle',
'isbn')
autocomplete_fields = ('authors',)
date_hierarchy = 'publish_date'
form = MediaAdminForm
def authors_list(self, obj):
return ", ".join([a.name for a in obj.authors.all()])
authors_list.short_description = _('authors')
def external_link(self, obj):
if obj.external_url:
return format_html('<a href="{}" target="about:blank">{}</a>',
obj.external_url, obj.external_url)
else:
return "-"
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)
class FutureMediaAdmin(VersionAdmin):
list_display = ('isbn',)
search_fields = ('isbn',)
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
extra_context['show_save'] = False
return super().changeform_view(request, object_id, form_url,
extra_context=extra_context)
class CDAdmin(VersionAdmin):
list_display = ('title', 'authors_list', 'side_identifier',)
search_fields = ('title', 'authors__name', 'side_identifier',)
autocomplete_fields = ('authors',)
def authors_list(self, obj):
return ", ".join([a.name for a in obj.authors.all()])
authors_list.short_description = _('authors')
class VinyleAdmin(VersionAdmin):
list_display = ('title', 'authors_list', 'side_identifier', 'rpm',)
search_fields = ('title', 'authors__name', 'side_identifier', 'rpm',)
autocomplete_fields = ('authors',)
def authors_list(self, obj):
return ", ".join([a.name for a in obj.authors.all()])
authors_list.short_description = _('authors')
class RevueAdmin(VersionAdmin):
list_display = ('__str__', 'number', 'year', 'month', 'day', 'double',)
search_fields = ('title', 'number', 'year',)
class EmpruntAdmin(VersionAdmin):
list_display = ('media', 'user', 'date_emprunt', 'date_rendu',
'permanencier_emprunt', 'permanencier_rendu_custom')
search_fields = ('media__title', 'media__side_identifier',
'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 = _('given back to')
permanencier_rendu_custom.allow_tags = True
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)
class JeuAdmin(VersionAdmin):
list_display = ('name', 'proprietaire', 'duree', 'nombre_joueurs_min',
'nombre_joueurs_max', 'comment')
search_fields = ('name', 'proprietaire__username', 'duree', 'comment')
autocomplete_fields = ('proprietaire',)
admin_site.register(Auteur, AuteurAdmin)
admin_site.register(BD, MediaAdmin)
admin_site.register(Manga, MediaAdmin)
admin_site.register(Roman, MediaAdmin)
admin_site.register(CD, CDAdmin)
admin_site.register(Vinyle, VinyleAdmin)
admin_site.register(Revue, RevueAdmin)
admin_site.register(FutureMedia, FutureMediaAdmin)
admin_site.register(Emprunt, EmpruntAdmin)
admin_site.register(Jeu, JeuAdmin)