From 781a1dae711b361a9a0304f5256c24c159b60004 Mon Sep 17 00:00:00 2001 From: Jonas Winkler Date: Tue, 28 Aug 2018 15:42:39 +0200 Subject: [PATCH] - added recent correspondents filter - sortable document_count fields - added last correspondence field to CorrespondentAdmin --- paperless.conf.example | 4 ++++ src/documents/admin.py | 50 ++++++++++++++++++++++++++++++++++----- src/paperless/settings.py | 2 ++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/paperless.conf.example b/paperless.conf.example index 424e046fb..d42d5ca5f 100755 --- a/paperless.conf.example +++ b/paperless.conf.example @@ -204,3 +204,7 @@ PAPERLESS_EMAIL_SECRET="" # 100 will be used. #PAPERLESS_LIST_PER_PAGE=100 + +# The number of years for which a correspondent will be included in the recent +# correspondents filter. +#PAPERLESS_RECENT_CORRESPONDENT_YEARS=2 \ No newline at end of file diff --git a/src/documents/admin.py b/src/documents/admin.py index d0cd23e9b..65548d91f 100755 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from django.conf import settings from django.contrib import admin, messages @@ -10,6 +10,7 @@ from django.utils.html import format_html from django.utils.http import urlquote from django.utils.safestring import mark_safe +from django.db import models from documents.actions import add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, \ remove_correspondent_from_selected, set_document_type_on_selected, remove_document_type_from_selected @@ -81,13 +82,27 @@ def queryset(self, request, queryset): created__lte=self._fy_end(end)) +class RecentCorrespondentFilter(admin.RelatedFieldListFilter): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.title = "correspondent (recent)" + + def field_choices(self, field, request, model_admin): + lookups = [] + date_limit = datetime.now() - timedelta(days=365*settings.PAPERLESS_RECENT_CORRESPONDENT_YEARS) + for c in Correspondent.objects.filter(documents__created__gte = date_limit).distinct(): + lookups.append( (c.id, c.name) ) + return lookups + + class CommonAdmin(admin.ModelAdmin): list_per_page = settings.PAPERLESS_LIST_PER_PAGE class CorrespondentAdmin(CommonAdmin): - list_display = ("name", "match", "matching_algorithm", "document_count") + list_display = ("name", "match", "matching_algorithm", "document_count", "last_correspondence") list_filter = ("matching_algorithm",) list_editable = ("match", "matching_algorithm") @@ -99,8 +114,18 @@ def save_model(self, request, obj, form, change): document.correspondent = obj document.save(update_fields=("correspondent",)) + def get_queryset(self, request): + qs = super(CorrespondentAdmin, self).get_queryset(request) + qs = qs.annotate(document_count=models.Count("documents"), last_correspondence=models.Max("documents__created")) + return qs + def document_count(self, obj): - return obj.documents.count() + return obj.document_count + document_count.admin_order_field = "document_count" + + def last_correspondence(self, obj): + return obj.last_correspondence + last_correspondence.admin_order_field = "last_correspondence" class TagAdmin(CommonAdmin): @@ -117,8 +142,15 @@ def save_model(self, request, obj, form, change): if obj.matches(document.content): document.tags.add(obj) + def get_queryset(self, request): + qs = super(TagAdmin, self).get_queryset(request) + qs = qs.annotate(document_count=models.Count("documents")) + return qs + def document_count(self, obj): - return obj.documents.count() + return obj.document_count + document_count.admin_order_field = "document_count" + class DocumentTypeAdmin(CommonAdmin): @@ -134,8 +166,14 @@ def save_model(self, request, obj, form, change): document.document_type = obj document.save(update_fields=("document_type",)) + def get_queryset(self, request): + qs = super(DocumentTypeAdmin, self).get_queryset(request) + qs = qs.annotate(document_count=models.Count("documents")) + return qs + def document_count(self, obj): - return obj.documents.count() + return obj.document_count + document_count.admin_order_field = "document_count" class DocumentAdmin(CommonAdmin): @@ -149,7 +187,7 @@ class Media: readonly_fields = ("added",) list_display = ("title", "created", "added", "thumbnail", "correspondent", "tags_", "archive_serial_number", "document_type") - list_filter = ("document_type", "tags", "correspondent", FinancialYearFilter) + list_filter = ("document_type", "tags", ('correspondent', RecentCorrespondentFilter), "correspondent", FinancialYearFilter) ordering = ["-created", "correspondent"] diff --git a/src/paperless/settings.py b/src/paperless/settings.py index b52cf1ca8..f354d5abf 100755 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -278,3 +278,5 @@ # Specify the default date order (for autodetected dates) DATE_ORDER = os.getenv("PAPERLESS_DATE_ORDER", "DMY") + +PAPERLESS_RECENT_CORRESPONDENT_YEARS = int(os.getenv("PAPERLESS_RECENT_CORRESPONDENT_YEARS", 2))