2017-08-24 20:20:00 +10:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
from django.conf import settings
|
2015-12-20 19:23:33 +00:00
|
|
|
from django.contrib import admin
|
2016-01-28 08:16:29 +00:00
|
|
|
from django.contrib.auth.models import User, Group
|
2016-01-01 16:13:59 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2015-12-26 13:21:19 +00:00
|
|
|
from django.templatetags.static import static
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-03-03 20:52:42 +00:00
|
|
|
from .models import Correspondent, Tag, Document, Log
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
|
2016-01-28 08:16:29 +00:00
|
|
|
class MonthListFilter(admin.SimpleListFilter):
|
|
|
|
|
|
|
|
|
|
title = "Month"
|
|
|
|
|
|
|
|
|
|
# Parameter for the filter that will be used in the URL query.
|
|
|
|
|
parameter_name = "month"
|
|
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
|
|
|
|
r = []
|
|
|
|
|
for document in Document.objects.all():
|
|
|
|
|
r.append((
|
|
|
|
|
document.created.strftime("%Y-%m"),
|
|
|
|
|
document.created.strftime("%B %Y")
|
|
|
|
|
))
|
|
|
|
|
return sorted(set(r), key=lambda x: x[0], reverse=True)
|
|
|
|
|
|
|
|
|
|
def queryset(self, request, queryset):
|
|
|
|
|
|
|
|
|
|
if not self.value():
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
year, month = self.value().split("-")
|
|
|
|
|
return queryset.filter(created__year=year, created__month=month)
|
|
|
|
|
|
|
|
|
|
|
2017-08-24 20:20:00 +10:00
|
|
|
class FinancialYearFilter(admin.SimpleListFilter):
|
|
|
|
|
|
|
|
|
|
title = "Financial Year"
|
|
|
|
|
parameter_name = "fy"
|
|
|
|
|
|
|
|
|
|
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
|
|
|
assert settings.FY_START
|
|
|
|
|
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
|
|
|
assert settings.FY_END
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
def _determine_fy(self, date):
|
|
|
|
|
"""Return a (query, display) financial year tuple of the given date."""
|
|
|
|
|
fy_start = self._fy_start(date.year)
|
|
|
|
|
|
|
|
|
|
if date.date() >= fy_start:
|
|
|
|
|
query = "{}-{}".format(date.year, date.year + 1)
|
|
|
|
|
else:
|
|
|
|
|
query = "{}-{}".format(date.year - 1, date.year)
|
|
|
|
|
|
|
|
|
|
# To keep it simple we use the same String for both query parameters
|
|
|
|
|
# and the display.
|
|
|
|
|
return (query, query)
|
|
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
|
|
|
|
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):
|
|
|
|
|
if not self.value():
|
|
|
|
|
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
|
|
|
|
|
|
|
|
list_display = ("name", "match", "matching_algorithm")
|
|
|
|
|
list_filter = ("matching_algorithm",)
|
|
|
|
|
list_editable = ("match", "matching_algorithm")
|
|
|
|
|
|
|
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
class TagAdmin(CommonAdmin):
|
2016-01-28 18:37:27 +00:00
|
|
|
|
|
|
|
|
list_display = ("name", "colour", "match", "matching_algorithm")
|
|
|
|
|
list_filter = ("colour", "matching_algorithm")
|
|
|
|
|
list_editable = ("colour", "match", "matching_algorithm")
|
|
|
|
|
|
|
|
|
|
|
2017-01-14 17:09:48 +00:00
|
|
|
class DocumentAdmin(CommonAdmin):
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-02-06 17:27:17 +00:00
|
|
|
class Media:
|
|
|
|
|
css = {
|
|
|
|
|
"all": ("paperless.css",)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 09:14:50 +00:00
|
|
|
search_fields = ("correspondent__name", "title", "content")
|
2017-01-29 19:43:35 +00:00
|
|
|
list_display = ("title", "created", "thumbnail", "correspondent", "tags_")
|
2017-08-24 20:51:09 +10:00
|
|
|
list_filter = ("tags", "correspondent")
|
|
|
|
|
if settings.FY_START and settings.FY_END:
|
|
|
|
|
list_filter += (FinancialYearFilter,)
|
|
|
|
|
list_filter += (MonthListFilter,)
|
|
|
|
|
|
2017-01-07 14:57:25 -08:00
|
|
|
ordering = ["-created", "correspondent"]
|
2015-12-20 19:23:33 +00: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
|
|
|
|
2017-01-07 14:57:25 -08:00
|
|
|
def thumbnail(self, obj):
|
|
|
|
|
png_img = self._html_tag(
|
2017-01-08 19:05:31 +00:00
|
|
|
"img",
|
|
|
|
|
src="/fetch/thumb/{}".format(obj.id),
|
2017-01-08 19:43:17 +00:00
|
|
|
width=180,
|
2017-03-01 00:50:53 +01:00
|
|
|
alt="Thumbnail of {}".format(obj.file_name),
|
2017-01-08 19:05:31 +00:00
|
|
|
title=obj.file_name
|
|
|
|
|
)
|
2017-01-07 14:57:25 -08:00
|
|
|
return self._html_tag("a", png_img, href=obj.download_url)
|
|
|
|
|
thumbnail.allow_tags = True
|
|
|
|
|
|
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(
|
2016-02-21 00:14:50 +00:00
|
|
|
"a",
|
|
|
|
|
tag.slug,
|
|
|
|
|
**{
|
|
|
|
|
"class": "tag",
|
|
|
|
|
"style": "background-color: {};".format(colour),
|
|
|
|
|
"href": "{}?tags__id__exact={}".format(
|
|
|
|
|
reverse("admin:documents_document_changelist"),
|
|
|
|
|
tag.pk
|
|
|
|
|
)
|
|
|
|
|
}
|
2016-01-23 04:40:35 +00:00
|
|
|
)
|
|
|
|
|
return r
|
|
|
|
|
tags_.allow_tags = True
|
|
|
|
|
|
2016-02-06 17:27:17 +00:00
|
|
|
def document(self, obj):
|
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
|
|
|
document.allow_tags = True
|
|
|
|
|
|
2016-02-27 20:18:50 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def _html_tag(kind, inside=None, **kwargs):
|
|
|
|
|
|
|
|
|
|
attributes = []
|
|
|
|
|
for lft, rgt in kwargs.items():
|
|
|
|
|
attributes.append('{}="{}"'.format(lft, rgt))
|
|
|
|
|
|
|
|
|
|
if inside is not None:
|
|
|
|
|
return "<{kind} {attributes}>{inside}</{kind}>".format(
|
|
|
|
|
kind=kind, attributes=" ".join(attributes), inside=inside)
|
|
|
|
|
|
|
|
|
|
return "<{} {}/>".format(kind, " ".join(attributes))
|
|
|
|
|
|
|
|
|
|
|
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)
|
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.
|
|
|
|
|
admin.site.unregister(Group)
|
|
|
|
|
admin.site.unregister(User)
|