From: Radek Czajka Date: Thu, 12 Jan 2023 08:58:02 +0000 (+0100) Subject: More filters in catalogue. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/13f286427e0ceb7a6c442844c447c7c3d5495871?ds=inline;hp=cf5dddce80a2b4fe4d5f8c87cc709050c0efb36e More filters in catalogue. --- diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 3bc943db..65c04f8c 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -10,7 +10,7 @@ python-slugify python-docx==0.8.11 Wikidata==0.7 -librarian==2.4.9 +librarian==2.4.10 ## Django Django==4.0.6 diff --git a/src/catalogue/admin.py b/src/catalogue/admin.py index 48d5dc53..68a362b5 100644 --- a/src/catalogue/admin.py +++ b/src/catalogue/admin.py @@ -2,10 +2,11 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.contrib import admin +from django.db.models import Min from django.utils.html import escape, format_html from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ -from admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin +from admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin, RangeNumericForm from admin_ordering.admin import OrderableAdmin from fnpdjango.actions import export_as_csv_action from modeltranslation.admin import TabbedTranslationAdmin @@ -137,6 +138,60 @@ def add_title(base_class, 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 BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin): list_display = [ @@ -182,6 +237,8 @@ class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin): CoverLicenseFilter, 'free_license', 'polona_missing', + + FirstPublicationYearFilter, ] list_per_page = 1000000 @@ -215,6 +272,9 @@ class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin): "estimated_verses", "estimate_source", + "document_book__project", + "first_publication_year", + "monthly_views_page", "monthly_views_reader", @@ -278,6 +338,7 @@ class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin): qs = super().get_queryset(request) if request.resolver_match.view_name.endswith("changelist"): qs = qs.prefetch_related("authors", "translators") + qs = qs.annotate(first_publication_year=Min('document_book__publish_log__timestamp__year')) return qs def estimated_costs(self, obj): diff --git a/src/catalogue/models.py b/src/catalogue/models.py index 8bcf4e00..151a13e4 100644 --- a/src/catalogue/models.py +++ b/src/catalogue/models.py @@ -288,6 +288,12 @@ class Book(WikidataModel): def translators_last_names(self): return ', '.join(a.last_name for a in self.translators.all()) + def document_book__project(self): + b = self.document_books.first() + if b is None: return '' + if b.project is None: return '' + return b.project.name + def get_estimated_costs(self): return { work_type: work_type.calculate(self)