paperless-ngx/src/documents/admin.py

119 lines
3.3 KiB
Python
Raw Normal View History

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
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)
2016-01-28 18:37:27 +00:00
class TagAdmin(admin.ModelAdmin):
list_display = ("name", "colour", "match", "matching_algorithm")
list_filter = ("colour", "matching_algorithm")
list_editable = ("colour", "match", "matching_algorithm")
2015-12-20 19:23:33 +00:00
class DocumentAdmin(admin.ModelAdmin):
2016-02-06 17:27:17 +00:00
class Media:
css = {
"all": ("paperless.css",)
}
2016-02-03 17:20:12 +00:00
search_fields = ("sender__name", "title", "content")
2016-02-16 09:28:34 +00:00
list_display = ("created_", "sender", "title", "tags_", "document")
2016-02-03 17:20:12 +00:00
list_filter = ("tags", "sender", MonthListFilter)
2016-01-23 04:40:35 +00:00
list_per_page = 25
2015-12-20 19:23:33 +00:00
2016-02-16 09:28:34 +00:00
def created_(self, obj):
return obj.created.date().strftime("%Y-%m-%d")
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))
class LogAdmin(admin.ModelAdmin):
list_display = ("message", "level", "component")
list_filter = ("level", "component",)
admin.site.register(Correspondent)
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)