+class LicenseFilter(admin.SimpleListFilter):
+ title = 'Licencja'
+ parameter_name = 'book_license'
+ license_url_field = 'document_book__dc__license'
+ license_name_field = 'document_book__dc__license_description'
+
+ def lookups(self, requesrt, model_admin):
+ return [
+ ('cc', 'CC'),
+ ('fal', 'FAL'),
+ ('pd', 'domena publiczna'),
+ ]
+
+ def queryset(self, request, queryset):
+ v = self.value()
+ if v == 'cc':
+ return queryset.filter(**{
+ self.license_url_field + '__icontains': 'creativecommons.org'
+ })
+ elif v == 'fal':
+ return queryset.filter(**{
+ self.license_url_field + '__icontains': 'artlibre.org'
+ })
+ elif v == 'pd':
+ return queryset.filter(**{
+ self.license_name_field + '__icontains': 'domena publiczna'
+ })
+ else:
+ return queryset
+
+
+class CoverLicenseFilter(LicenseFilter):
+ title = 'Licencja okładki'
+ parameter_name = 'cover_license'
+ license_url_field = 'document_book__dc_cover_image__license_url'
+ license_name_field = 'document_book__dc_cover_image__license_name'
+
+
+def add_title(base_class, suffix):
+ class TitledCategoryFilter(base_class):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.title += suffix
+ return TitledCategoryFilter
+
+
+class FirstPublicationYearFilter(admin.ListFilter):
+ title = 'Rok pierwszej publikacji'
+ parameter_name = 'first_publication_year'
+ template = 'admin/filter_numeric_range.html'
+
+ def __init__(self, request, params, *args, **kwargs):
+ super().__init__(request, params, *args, **kwargs)
+
+ self.request = request
+
+ if self.parameter_name + '_from' in params:
+ value = params.pop(self.parameter_name + '_from')
+ self.used_parameters[self.parameter_name + '_from'] = value
+
+ if self.parameter_name + '_to' in params:
+ value = params.pop(self.parameter_name + '_to')
+ self.used_parameters[self.parameter_name + '_to'] = value
+
+ def has_output(self):
+ return True
+
+ def queryset(self, request, queryset):
+ filters = {}
+
+ value_from = self.used_parameters.get(self.parameter_name + '_from', None)
+ if value_from is not None and value_from != '':
+ filters.update({
+ self.parameter_name + '__gte': self.used_parameters.get(self.parameter_name + '_from', None),
+ })
+
+ value_to = self.used_parameters.get(self.parameter_name + '_to', None)
+ if value_to is not None and value_to != '':
+ filters.update({
+ self.parameter_name + '__lte': self.used_parameters.get(self.parameter_name + '_to', None),
+ })
+
+ return queryset.filter(**filters)
+
+ def choices(self, changelist):
+ return ({
+ 'request': self.request,
+ 'parameter_name': self.parameter_name,
+ 'form': RangeNumericForm(name=self.parameter_name, data={
+ self.parameter_name + '_from': self.used_parameters.get(self.parameter_name + '_from', None),
+ self.parameter_name + '_to': self.used_parameters.get(self.parameter_name + '_to', None),
+ }),
+ }, )
+
+ def expected_parameters(self):
+ return [
+ '{}_from'.format(self.parameter_name),
+ '{}_to'.format(self.parameter_name),
+ ]
+
+
+class SourcesInline(admin.TabularInline):
+ model = sources.models.BookSource
+ extra = 1
+
+
+class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin, VersionAdmin):
+ inlines = [SourcesInline]
+ list_display = [
+ "smart_title",
+ "authors_str",
+ "translators_str",
+ "language",
+ "pd_year",
+ "priority",
+ "wikidata_link",
+ ]
+ search_fields = [
+ "title", "wikidata",
+ "authors__first_name", "authors__last_name",
+ "translators__first_name", "translators__last_name",
+ "scans_source", "text_source", "notes", "estimate_source",
+ ]
+ autocomplete_fields = ["authors", "translators", "based_on", "epochs", "genres", "kinds"]
+ filter_horizontal = ['collections']