med/media/admin.py

162 lines
5.6 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.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from polymorphic.admin import PolymorphicChildModelAdmin, \
PolymorphicParentModelAdmin
2019-08-10 06:04:46 +00:00
from med.admin import admin_site
2020-12-28 22:12:27 +00:00
from reversion.admin import VersionAdmin
2020-09-25 08:27:43 +00:00
2019-08-11 08:40:39 +00:00
from .forms import MediaAdminForm
2021-11-14 13:26:41 +00:00
from .models import Author, Borrow, Borrowable, CD, Comic, FutureMedium, \
Game, Manga, Novel, Review, Vinyl
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2021-10-23 16:39:51 +00:00
class AuthorAdmin(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
class BorrowableAdmin(PolymorphicParentModelAdmin):
search_fields = ('title',)
child_models = (CD, Comic, Manga, Novel, Review, Vinyl,)
2021-10-26 13:07:29 +00:00
def get_model_perms(self, request):
# We don't want that the borrowable items appear directly in
# main menu, but we still want search borrowable items.
return {}
class MediumAdmin(VersionAdmin, PolymorphicChildModelAdmin):
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
show_in_index = True
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
2021-10-23 16:39:51 +00:00
class FutureMediumAdmin(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, PolymorphicChildModelAdmin):
2020-05-22 16:04:41 +00:00
list_display = ('title', 'authors_list', 'side_identifier',)
search_fields = ('title', 'authors__name', 'side_identifier',)
autocomplete_fields = ('authors',)
show_in_index = True
2020-05-22 16:04:41 +00:00
def authors_list(self, obj):
return ", ".join([a.name for a in obj.authors.all()])
authors_list.short_description = _('authors')
class VinylAdmin(VersionAdmin, PolymorphicChildModelAdmin):
2020-05-24 15:47:37 +00:00
list_display = ('title', 'authors_list', 'side_identifier', 'rpm',)
search_fields = ('title', 'authors__name', 'side_identifier', 'rpm',)
autocomplete_fields = ('authors',)
show_in_index = True
2020-05-24 15:47:37 +00:00
def authors_list(self, obj):
return ", ".join([a.name for a in obj.authors.all()])
authors_list.short_description = _('authors')
class ReviewAdmin(VersionAdmin, PolymorphicChildModelAdmin):
2020-05-24 13:37:57 +00:00
list_display = ('__str__', 'number', 'year', 'month', 'day', 'double',)
search_fields = ('title', 'number', 'year',)
show_in_index = True
2020-05-24 13:37:57 +00:00
2021-11-14 13:26:41 +00:00
class BorrowAdmin(VersionAdmin):
list_display = ('borrowable', 'user', 'borrow_date', 'borrowed_with',
'given_back_to')
search_fields = ('borrowable__isbn', 'borrowable__title',
'borrowable__medium__side_identifier',
'user__username', 'borrow_date', 'given_back')
date_hierarchy = 'borrow_date'
autocomplete_fields = ('borrowable', 'user', 'borrowed_with',
'given_back_to')
2019-08-10 13:53:23 +00:00
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()
2021-11-14 13:26:41 +00:00
data['borrowed_with'] = request.user
2019-08-16 12:34:16 +00:00
request.GET = data
return super().add_view(request, form_url, extra_context)
2017-06-30 01:25:07 +00:00
class GameAdmin(VersionAdmin, PolymorphicChildModelAdmin):
list_display = ('title', 'owner', 'duration', 'players_min',
2021-11-14 15:41:38 +00:00
'players_max', 'comment', 'isbn')
search_fields = ('isbn', 'title', 'owner__username', 'duration', 'comment')
2021-10-23 16:39:51 +00:00
autocomplete_fields = ('owner',)
2021-11-02 11:30:44 +00:00
show_in_index = True
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
2021-10-23 16:39:51 +00:00
admin_site.register(Author, AuthorAdmin)
admin_site.register(Borrowable, BorrowableAdmin)
2021-10-23 16:39:51 +00:00
admin_site.register(Comic, MediumAdmin)
admin_site.register(Manga, MediumAdmin)
admin_site.register(Novel, MediumAdmin)
2020-05-22 16:04:41 +00:00
admin_site.register(CD, CDAdmin)
2021-10-23 16:39:51 +00:00
admin_site.register(Vinyl, VinylAdmin)
admin_site.register(Review, ReviewAdmin)
admin_site.register(FutureMedium, FutureMediumAdmin)
2021-11-14 13:26:41 +00:00
admin_site.register(Borrow, BorrowAdmin)
2021-10-23 16:39:51 +00:00
admin_site.register(Game, GameAdmin)