From 164efe902db83a99e6ecc6023ebd411366499180 Mon Sep 17 00:00:00 2001 From: David Martin Date: Fri, 25 Aug 2017 17:36:09 +1000 Subject: [PATCH] Return no filter results if financial year dates are not set. This is a lot cleaner than trying to hack around whether or not the FinancialYearFilter is part of the available filters. This way it will show up if there are result for it and the dates are set, and it will not if any of those conditions is not set. --- src/documents/admin.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/documents/admin.py b/src/documents/admin.py index 17f78ffb3..7c4938c64 100644 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -41,13 +41,11 @@ class FinancialYearFilter(admin.SimpleListFilter): def _fy_start(self, year): """Return date of the start of financial year for the given year.""" - assert settings.FY_START fy_start = "{}-{}".format(str(year), settings.FY_START) return datetime.strptime(fy_start, "%Y-%m-%d").date() def _fy_end(self, year): """Return date of the end of financial year for the given year.""" - assert settings.FY_END fy_end = "{}-{}".format(str(year), settings.FY_END) return datetime.strptime(fy_end, "%Y-%m-%d").date() @@ -65,6 +63,9 @@ class FinancialYearFilter(admin.SimpleListFilter): return (query, query) def lookups(self, request, model_admin): + if not settings.FY_START or not settings.FY_END: + return None + r = [] for document in Document.objects.all(): r.append(self._determine_fy(document.created)) @@ -72,7 +73,7 @@ class FinancialYearFilter(admin.SimpleListFilter): return sorted(set(r), key=lambda x: x[0], reverse=True) def queryset(self, request, queryset): - if not self.value(): + if not self.value() or not settings.FY_START or not settings.FY_END: return None start, end = self.value().split("-") @@ -107,10 +108,8 @@ class DocumentAdmin(CommonAdmin): search_fields = ("correspondent__name", "title", "content") list_display = ("title", "created", "thumbnail", "correspondent", "tags_") - list_filter = ("tags", "correspondent") - if settings.FY_START and settings.FY_END: - list_filter += (FinancialYearFilter,) - list_filter += (MonthListFilter,) + list_filter = ("tags", "correspondent", FinancialYearFilter, + MonthListFilter) ordering = ["-created", "correspondent"]