1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.contrib import admin
5 from catalogue.models import Tag, Book, Fragment, BookMedia, Collection, Source, Snippet
6 from pz.admin import EmptyFieldListFilter
9 class BlankFieldListFilter(EmptyFieldListFilter):
13 class TagAdmin(admin.ModelAdmin):
14 list_display = ('name', 'slug', 'sort_key', 'category', 'has_description', 'occurrences')
15 list_filter = ('category',)
16 search_fields = ('name',)
18 readonly_fields = ('occurrences',)
19 raw_id_fields = ['user']
21 def occurrences(self, tag):
22 return tag.items.count()
23 occurrences.short_description = 'Wystąpienia'
25 prepopulated_fields = {'slug': ('name',), 'sort_key': ('name',)}
26 radio_fields = {'category': admin.HORIZONTAL}
29 class MediaInline(admin.TabularInline):
31 readonly_fields = ['source_sha1']
35 class BookAdmin(admin.ModelAdmin):
37 'title', 'slug', 'created_at', 'has_epub_file', 'has_html_file', 'has_description',
41 ('wiki_link', BlankFieldListFilter),
42 ('parent', EmptyFieldListFilter),
43 ('media', admin.EmptyFieldListFilter),
45 date_hierarchy = 'created_at'
46 search_fields = ('title',)
49 inlines = [MediaInline]
52 class FragmentAdmin(admin.ModelAdmin):
53 list_display = ('book', 'anchor',)
54 ordering = ('book', 'anchor',)
57 class CollectionAdmin(admin.ModelAdmin):
58 list_display = ('title', 'listed', 'role')
59 prepopulated_fields = {'slug': ('title',)}
62 class SourceAdmin(admin.ModelAdmin):
63 list_display = ('netloc', 'name')
66 admin.site.register(Tag, TagAdmin)
67 admin.site.register(Book, BookAdmin)
68 admin.site.register(Fragment, FragmentAdmin)
69 admin.site.register(Collection, CollectionAdmin)
70 admin.site.register(Source, SourceAdmin)
73 admin.site.register(Snippet)