91656f1362fb3d27be83d00b9e608b98e318d89f
[redakcja.git] / src / catalogue / admin.py
1 from django.contrib import admin
2 from . import models
3 from .wikidata import WikidataAdminMixin
4
5
6 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
7     list_display = [
8         "first_name",
9         "last_name",
10         "status",
11         "year_of_death",
12         "priority",
13         "wikidata_link",
14         "slug",
15     ]
16     list_filter = ["year_of_death", "priority", "collections", "status"]
17     search_fields = ["first_name", "last_name", "wikidata"]
18     prepopulated_fields = {"slug": ("first_name", "last_name")}
19     autocomplete_fields = ["collections"]
20
21
22 admin.site.register(models.Author, AuthorAdmin)
23
24
25 class BookAdmin(WikidataAdminMixin, admin.ModelAdmin):
26     list_display = [
27         "title",
28         "authors_str",
29         "translators_str",
30         "language",
31         "pd_year",
32         "priority",
33         "wikidata_link",
34     ]
35     search_fields = ["title", "wikidata"]
36     autocomplete_fields = ["authors", "translators", "based_on", "collections"]
37     prepopulated_fields = {"slug": ("title",)}
38     list_filter = ["language", "pd_year", "collections"]
39     readonly_fields = ["wikidata_link"]
40     fieldsets = [
41         (None, {"fields": [("wikidata", "wikidata_link")]}),
42         (
43             "Identification",
44             {
45                 "fields": [
46                     "title",
47                     "slug",
48                     "authors",
49                     "translators",
50                     "language",
51                     "based_on",
52                     "pd_year",
53                 ]
54             },
55         ),
56         (
57             "Plan",
58             {
59                 "fields": [
60                     "scans_source",
61                     "text_source",
62                     "priority",
63                     "collections",
64                     "notes",
65                 ]
66             },
67         ),
68     ]
69
70     def get_queryset(self, request):
71         qs = super().get_queryset(request)
72         if request.resolver_match.view_name.endswith("changelist"):
73             qs = qs.prefetch_related("authors", "translators")
74         return qs
75
76
77 admin.site.register(models.Book, BookAdmin)
78
79
80 class AuthorInline(admin.TabularInline):
81     model = models.Author.collections.through
82     autocomplete_fields = ["author"]
83
84
85 class BookInline(admin.TabularInline):
86     model = models.Book.collections.through
87     autocomplete_fields = ["book"]
88
89
90 class CollectionAdmin(admin.ModelAdmin):
91     list_display = ["name"]
92     autocomplete_fields = []
93     prepopulated_fields = {"slug": ("name",)}
94     search_fields = ["name"]
95     inlines = [AuthorInline, BookInline]
96
97
98 admin.site.register(models.Collection, CollectionAdmin)