+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'
+
+
+class ChildrenFilter(admin.SimpleListFilter):
+ title = 'Status utworu podrzędnego'
+ parameter_name = 'book_children'
+
+ def lookups(self, requesrt, model_admin):
+ return [
+ ('no', 'bez podrzędnych'),
+ ('only', 'tylko podrzędne'),
+ ]
+
+ def queryset(self, request, queryset):
+ v = self.value()
+ if v == 'no':
+ return queryset.filter(parent=None)
+ elif v == 'only':
+ return queryset.exclude(parent=None)
+ else:
+ return queryset
+
+
+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 EditorNoteInline(admin.TabularInline):
+ model = models.EditorNote
+ extra = 1
+
+
+class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin, VersionAdmin):
+ inlines = [EditorNoteInline, SourcesInline]