1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.contrib import admin
6 from django import forms
8 from newtagging.admin import TaggableModelAdmin, TaggableModelForm
9 from catalogue.models import Tag, Book, Fragment, BookMedia, Collection, Source
12 class TagAdmin(admin.ModelAdmin):
13 list_display = ('name', 'slug', 'sort_key', 'category', 'has_description', 'occurrences')
14 list_filter = ('category',)
15 search_fields = ('name',)
17 readonly_fields = ('occurrences',)
19 def occurrences(self, tag):
20 return tag.items.count()
21 occurrences.short_description = u'Wystąpienia'
23 prepopulated_fields = {'slug': ('name',), 'sort_key': ('name',)}
24 radio_fields = {'category': admin.HORIZONTAL}
27 class MediaInline(admin.TabularInline):
29 readonly_fields = ['source_sha1']
33 class BookAdmin(TaggableModelAdmin):
36 list_display = ('title', 'slug', 'created_at', 'has_epub_file', 'has_html_file', 'has_description',)
37 search_fields = ('title',)
40 inlines = [MediaInline]
42 def change_view(self, request, object_id, extra_context=None):
43 if 'advanced' not in request.GET:
44 self.form = forms.ModelForm
45 self.fields = ('title', 'description', 'wiki_link', 'recommended')
46 self.readonly_fields = ('title',)
48 self.form = TaggableModelForm
50 self.readonly_fields = ()
51 return super(BookAdmin, self).change_view(request, object_id, extra_context=extra_context)
54 class FragmentAdmin(TaggableModelAdmin):
57 list_display = ('book', 'anchor',)
58 ordering = ('book', 'anchor',)
61 class CollectionAdmin(admin.ModelAdmin):
62 prepopulated_fields = {'slug': ('title',)}
65 class SourceAdmin(admin.ModelAdmin):
66 list_display = ('netloc', 'name')
69 admin.site.register(Tag, TagAdmin)
70 admin.site.register(Book, BookAdmin)
71 admin.site.register(Fragment, FragmentAdmin)
72 admin.site.register(Collection, CollectionAdmin)
73 admin.site.register(Source, SourceAdmin)