2018-08-28 15:42:39 +02:00
|
|
|
from datetime import datetime, timedelta
|
2017-08-24 20:20:00 +10:00
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
from django.conf import settings
|
2018-07-16 16:08:51 +02:00
|
|
|
from django.contrib import admin, messages
|
|
|
|
|
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
2018-09-23 12:41:28 +01:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
|
from django.db import models
|
2018-09-06 10:15:15 +02:00
|
|
|
from django.http import HttpResponseRedirect
|
2015-12-26 13:21:19 +00:00
|
|
|
from django.templatetags.static import static
|
2018-09-23 12:41:28 +01:00
|
|
|
from django.urls import reverse
|
2018-09-06 10:15:15 +02:00
|
|
|
from django.utils.html import format_html, format_html_join
|
2018-07-16 16:08:51 +02:00
|
|
|
from django.utils.http import urlquote
|
2018-07-04 17:03:59 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
2019-01-26 22:10:13 +01:00
|
|
|
from djangoql.admin import DjangoQLSearchMixin
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2018-09-25 14:47:12 +02:00
|
|
|
from .models import Correspondent, Document, DocumentType, Log, Tag
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
|
2017-08-24 20:20:00 +10:00
|
|
|
class FinancialYearFilter(admin.SimpleListFilter):
|
|
|
|
|
|
|
|
|
|
title = "Financial Year"
|
|
|
|
|
parameter_name = "fy"
|
2017-08-26 19:45:39 +10:00
|
|
|
_fy_wraps = None
|
2017-08-24 20:20:00 +10:00
|
|
|
|
|
|
|
|
def _fy_start(self, year):
|
|
|
|
|
"""Return date of the start of financial year for the given year."""
|
2017-08-24 20:51:09 +10:00
|
|
|
fy_start = "{}-{}".format(str(year), settings.FY_START)
|
2017-08-24 20:20:00 +10:00
|
|
|
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."""
|
2017-08-24 20:51:09 +10:00
|
|
|
fy_end = "{}-{}".format(str(year), settings.FY_END)
|
2017-08-24 20:20:00 +10:00
|
|
|
return datetime.strptime(fy_end, "%Y-%m-%d").date()
|
|
|
|
|
|
2017-08-25 20:27:39 +10:00
|
|
|
def _fy_does_wrap(self):
|
|
|
|
|
"""Return whether the financial year spans across two years."""
|
2017-08-26 19:45:39 +10:00
|
|
|
if self._fy_wraps is None:
|
|
|
|
|
start = "{}".format(settings.FY_START)
|
|
|
|
|
start = datetime.strptime(start, "%m-%d").date()
|
|
|
|
|
end = "{}".format(settings.FY_END)
|
|
|
|
|
end = datetime.strptime(end, "%m-%d").date()
|
|
|
|
|
self._fy_wraps = end < start
|
|
|
|
|
|
|
|
|
|
return self._fy_wraps
|
2017-08-25 20:27:39 +10:00
|
|
|
|
2017-08-24 20:20:00 +10:00
|
|
|
def _determine_fy(self, date):
|
|
|
|
|
"""Return a (query, display) financial year tuple of the given date."""
|
2017-08-25 20:27:39 +10:00
|
|
|
if self._fy_does_wrap():
|
2017-08-26 19:50:57 +10:00
|
|
|
fy_start = self._fy_start(date.year)
|
|
|
|
|
|
2017-08-25 20:27:39 +10:00
|
|
|
if date.date() >= fy_start:
|
|
|
|
|
query = "{}-{}".format(date.year, date.year + 1)
|
|
|
|
|
else:
|
|
|
|
|
query = "{}-{}".format(date.year - 1, date.year)
|
2017-08-24 20:20:00 +10:00
|
|
|
|
2017-08-25 20:27:39 +10:00
|
|
|
# To keep it simple we use the same string for both
|
|
|
|
|
# query parameter and the display.
|
2018-11-03 11:05:22 +00:00
|
|
|
return query, query
|
2017-08-25 20:27:39 +10:00
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
query = "{0}-{0}".format(date.year)
|
|
|
|
|
display = "{}".format(date.year)
|
2018-11-03 11:05:22 +00:00
|
|
|
return query, display
|
2017-08-24 20:20:00 +10:00
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
2017-08-25 17:36:09 +10:00
|
|
|
if not settings.FY_START or not settings.FY_END:
|
|
|
|
|
return None
|
|
|
|
|
|
2017-08-24 20:20:00 +10:00
|
|
|
r = []
|
|
|
|
|
for document in Document.objects.all():
|
|
|
|
|
r.append(self._determine_fy(document.created))
|
|
|
|
|
|
|
|
|
|
return sorted(set(r), key=lambda x: x[0], reverse=True)
|
|
|
|
|
|
|
|
|
|
def queryset(self, request, queryset):
|
2017-08-25 17:36:09 +10:00
|
|
|
if not self.value() or not settings.FY_START or not settings.FY_END:
|
2017-08-24 20:20:00 +10:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
start, end = self.value().split("-")
|
|
|
|
|
return queryset.filter(created__gte=self._fy_start(start),
|
|
|
|
|
created__lte=self._fy_end(end))
|
|
|
|
|
|
|
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
class CommonAdmin(admin.ModelAdmin):
|
|
|
|
|
list_per_page = settings.PAPERLESS_LIST_PER_PAGE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CorrespondentAdmin(CommonAdmin):
|
2016-03-28 11:11:15 +01:00
|
|
|
|
2018-09-23 12:41:28 +01:00
|
|
|
list_display = (
|
|
|
|
|
"name",
|
2018-09-25 14:47:12 +02:00
|
|
|
"automatic_classification",
|
2018-09-23 12:41:28 +01:00
|
|
|
"document_count",
|
|
|
|
|
"last_correspondence"
|
|
|
|
|
)
|
2018-09-25 16:09:33 +02:00
|
|
|
list_editable = ("automatic_classification",)
|
2016-03-28 11:11:15 +01:00
|
|
|
|
2018-10-07 16:24:05 +01:00
|
|
|
readonly_fields = ("slug",)
|
2016-03-28 11:11:15 +01:00
|
|
|
|
2018-08-28 15:42:39 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
|
qs = super(CorrespondentAdmin, self).get_queryset(request)
|
2018-09-23 12:41:28 +01:00
|
|
|
qs = qs.annotate(
|
|
|
|
|
document_count=models.Count("documents"),
|
|
|
|
|
last_correspondence=models.Max("documents__created")
|
|
|
|
|
)
|
2018-08-28 15:42:39 +02:00
|
|
|
return qs
|
|
|
|
|
|
2018-07-05 12:56:20 +02:00
|
|
|
def document_count(self, obj):
|
2018-08-28 15:42:39 +02:00
|
|
|
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"
|
2018-07-05 12:56:20 +02:00
|
|
|
|
2016-03-28 11:11:15 +01:00
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
class TagAdmin(CommonAdmin):
|
2016-01-28 18:37:27 +00:00
|
|
|
|
2018-09-25 16:09:33 +02:00
|
|
|
list_display = (
|
|
|
|
|
"name",
|
|
|
|
|
"colour",
|
|
|
|
|
"automatic_classification",
|
|
|
|
|
"document_count")
|
2018-09-04 18:40:26 +02:00
|
|
|
list_filter = ("colour",)
|
|
|
|
|
list_editable = ("colour", "automatic_classification")
|
2016-01-28 18:37:27 +00:00
|
|
|
|
2018-10-07 16:24:05 +01:00
|
|
|
readonly_fields = ("slug",)
|
|
|
|
|
|
2018-12-01 16:21:58 +00:00
|
|
|
class Media:
|
|
|
|
|
js = ("js/colours.js",)
|
|
|
|
|
|
2018-08-28 15:42:39 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
|
qs = super(TagAdmin, self).get_queryset(request)
|
|
|
|
|
qs = qs.annotate(document_count=models.Count("documents"))
|
|
|
|
|
return qs
|
|
|
|
|
|
2018-07-05 12:56:20 +02:00
|
|
|
def document_count(self, obj):
|
2018-08-28 15:42:39 +02:00
|
|
|
return obj.document_count
|
|
|
|
|
document_count.admin_order_field = "document_count"
|
|
|
|
|
|
2018-07-05 12:56:20 +02:00
|
|
|
|
2018-08-24 13:45:15 +02:00
|
|
|
class DocumentTypeAdmin(CommonAdmin):
|
|
|
|
|
|
2018-09-04 18:40:26 +02:00
|
|
|
list_display = ("name", "automatic_classification", "document_count")
|
|
|
|
|
list_editable = ("automatic_classification",)
|
2018-08-24 13:45:15 +02:00
|
|
|
|
2018-12-11 12:06:15 +01:00
|
|
|
readonly_fields = ("slug",)
|
|
|
|
|
|
2018-08-28 15:42:39 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
|
qs = super(DocumentTypeAdmin, self).get_queryset(request)
|
|
|
|
|
qs = qs.annotate(document_count=models.Count("documents"))
|
|
|
|
|
return qs
|
|
|
|
|
|
2018-08-24 13:45:15 +02:00
|
|
|
def document_count(self, obj):
|
2018-08-28 15:42:39 +02:00
|
|
|
return obj.document_count
|
|
|
|
|
document_count.admin_order_field = "document_count"
|
2016-01-28 18:37:27 +00:00
|
|
|
|
2018-09-13 14:15:16 +02:00
|
|
|
|
2019-01-26 22:10:13 +01:00
|
|
|
class DocumentAdmin(DjangoQLSearchMixin, CommonAdmin):
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-02-06 17:27:17 +00:00
|
|
|
class Media:
|
|
|
|
|
css = {
|
|
|
|
|
"all": ("paperless.css",)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-20 17:27:33 +01:00
|
|
|
search_fields = ("correspondent__name", "title", "content", "tags__name")
|
2018-10-07 16:26:05 +01:00
|
|
|
readonly_fields = ("added", "file_type", "storage_type",)
|
2020-10-17 01:57:08 +02:00
|
|
|
list_display = ("title", "created", "added", "correspondent",
|
2018-08-24 13:45:15 +02:00
|
|
|
"tags_", "archive_serial_number", "document_type")
|
2018-09-23 12:41:28 +01:00
|
|
|
list_filter = (
|
2018-09-25 14:47:12 +02:00
|
|
|
"document_type",
|
2018-09-23 12:41:28 +01:00
|
|
|
"tags",
|
2020-10-17 01:57:08 +02:00
|
|
|
"correspondent",
|
2018-09-23 12:41:28 +01:00
|
|
|
FinancialYearFilter
|
|
|
|
|
)
|
2017-08-24 20:51:09 +10:00
|
|
|
|
2018-08-26 14:20:07 +02:00
|
|
|
filter_horizontal = ("tags",)
|
2017-08-24 20:51:09 +10:00
|
|
|
|
2017-01-07 14:57:25 -08:00
|
|
|
ordering = ["-created", "correspondent"]
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2018-09-23 12:41:28 +01:00
|
|
|
date_hierarchy = "created"
|
2018-07-04 17:10:56 +02:00
|
|
|
|
2017-03-05 12:15:18 +00:00
|
|
|
def has_add_permission(self, request):
|
|
|
|
|
return False
|
|
|
|
|
|
2016-02-16 09:28:34 +00:00
|
|
|
def created_(self, obj):
|
|
|
|
|
return obj.created.date().strftime("%Y-%m-%d")
|
2017-03-11 16:37:18 +00:00
|
|
|
created_.short_description = "Created"
|
2016-02-16 09:28:34 +00:00
|
|
|
|
2018-07-04 17:03:59 +02:00
|
|
|
@mark_safe
|
2017-01-07 14:57:25 -08:00
|
|
|
def thumbnail(self, obj):
|
2018-04-13 20:18:16 +01:00
|
|
|
return self._html_tag(
|
|
|
|
|
"a",
|
|
|
|
|
self._html_tag(
|
|
|
|
|
"img",
|
|
|
|
|
src=reverse("fetch", kwargs={"kind": "thumb", "pk": obj.pk}),
|
2020-10-17 01:57:08 +02:00
|
|
|
height=100,
|
2018-04-13 20:18:16 +01:00
|
|
|
alt="Thumbnail of {}".format(obj.file_name),
|
|
|
|
|
title=obj.file_name
|
|
|
|
|
),
|
|
|
|
|
href=obj.download_url
|
2017-01-08 19:05:31 +00:00
|
|
|
)
|
2017-01-07 14:57:25 -08:00
|
|
|
|
2018-07-04 17:03:59 +02:00
|
|
|
@mark_safe
|
2016-01-23 04:40:35 +00:00
|
|
|
def tags_(self, obj):
|
|
|
|
|
r = ""
|
|
|
|
|
for tag in obj.tags.all():
|
2016-02-21 00:14:50 +00:00
|
|
|
colour = tag.get_colour_display()
|
2016-02-27 20:18:50 +00:00
|
|
|
r += self._html_tag(
|
2020-10-17 01:57:08 +02:00
|
|
|
"span",
|
|
|
|
|
tag.slug + ", "
|
2016-01-23 04:40:35 +00:00
|
|
|
)
|
|
|
|
|
return r
|
|
|
|
|
|
2018-07-04 17:03:59 +02:00
|
|
|
@mark_safe
|
2016-02-06 17:27:17 +00:00
|
|
|
def document(self, obj):
|
2018-07-04 17:03:59 +02:00
|
|
|
# TODO: is this method even used anymore?
|
2016-02-27 20:18:50 +00:00
|
|
|
return self._html_tag(
|
2016-02-21 00:14:50 +00:00
|
|
|
"a",
|
2016-02-27 20:18:50 +00:00
|
|
|
self._html_tag(
|
2016-02-21 00:14:50 +00:00
|
|
|
"img",
|
|
|
|
|
src=static("documents/img/{}.png".format(obj.file_type)),
|
|
|
|
|
width=22,
|
|
|
|
|
height=22,
|
|
|
|
|
alt=obj.file_type,
|
|
|
|
|
title=obj.file_name
|
|
|
|
|
),
|
|
|
|
|
href=obj.download_url
|
|
|
|
|
)
|
2016-02-06 17:27:17 +00:00
|
|
|
|
2016-02-27 20:18:50 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def _html_tag(kind, inside=None, **kwargs):
|
2018-08-31 00:17:48 +02:00
|
|
|
attributes = format_html_join(' ', '{}="{}"', kwargs.items())
|
2016-02-27 20:18:50 +00:00
|
|
|
|
|
|
|
|
if inside is not None:
|
2018-08-31 00:04:02 +02:00
|
|
|
return format_html("<{kind} {attributes}>{inside}</{kind}>",
|
2018-08-31 00:17:48 +02:00
|
|
|
kind=kind, attributes=attributes, inside=inside)
|
2016-02-27 20:18:50 +00:00
|
|
|
|
2018-08-31 00:04:02 +02:00
|
|
|
return format_html("<{} {}/>", kind, attributes)
|
2016-02-27 20:18:50 +00:00
|
|
|
|
|
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
class LogAdmin(CommonAdmin):
|
2016-02-27 20:18:50 +00:00
|
|
|
|
2016-07-19 14:13:59 +01:00
|
|
|
list_display = ("created", "message", "level",)
|
|
|
|
|
list_filter = ("level", "created",)
|
2016-02-27 20:18:50 +00:00
|
|
|
|
|
|
|
|
|
2016-03-28 11:11:15 +01:00
|
|
|
admin.site.register(Correspondent, CorrespondentAdmin)
|
2016-01-28 18:37:27 +00:00
|
|
|
admin.site.register(Tag, TagAdmin)
|
2018-08-24 13:45:15 +02:00
|
|
|
admin.site.register(DocumentType, DocumentTypeAdmin)
|
2015-12-20 19:23:33 +00:00
|
|
|
admin.site.register(Document, DocumentAdmin)
|
2016-02-27 20:18:50 +00:00
|
|
|
admin.site.register(Log, LogAdmin)
|
|
|
|
|
|
2016-01-28 08:16:29 +00:00
|
|
|
|
|
|
|
|
# Unless we implement multi-user, these default registrations don't make sense.
|
2020-10-17 01:57:08 +02:00
|
|
|
#admin.site.unregister(Group)
|
|
|
|
|
#admin.site.unregister(User)
|