Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
- added recent correspondents filter
Browse files Browse the repository at this point in the history
- sortable document_count fields
- added last correspondence field to CorrespondentAdmin
  • Loading branch information
Jonas Winkler committed Aug 28, 2018
1 parent 01fed4f commit 781a1da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
4 changes: 4 additions & 0 deletions paperless.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 44 additions & 6 deletions src/documents/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta

from django.conf import settings
from django.contrib import admin, messages
Expand All @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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):
Expand All @@ -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):

Expand All @@ -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):

Expand All @@ -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"]

Expand Down
2 changes: 2 additions & 0 deletions src/paperless/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 781a1da

Please sign in to comment.