paperless-ngx/src/documents/admin.py

85 lines
2.5 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
2016-01-23 04:40:35 +00:00
from .models import Sender, Tag, Document
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-06 17:14:44 +00:00
list_display = ("created", "sender", "title", "tags_", "document")
2016-02-03 17:20:12 +00:00
list_filter = ("tags", "sender", MonthListFilter)
list_editable = ("sender", "title")
2016-01-23 04:40:35 +00:00
list_per_page = 25
2015-12-20 19:23:33 +00:00
2016-01-23 04:40:35 +00:00
def tags_(self, obj):
r = ""
for tag in obj.tags.all():
2016-02-06 17:27:17 +00:00
r += '<a class="tag" style="background-color: {};" href="{}">{}</a>'.format(
2016-01-23 04:40:35 +00:00
tag.get_colour_display(),
2016-02-06 17:14:44 +00:00
"{}?tags__id__exact={}".format(
reverse("admin:documents_document_changelist"),
tag.pk
),
2016-01-23 04:40:35 +00:00
tag.slug
)
return r
tags_.allow_tags = True
2016-02-06 17:27:17 +00:00
def document(self, obj):
return '<a href="{}">' \
'<img src="{}" width="22" height="22" alt="{} icon">' \
'</a>'.format(
reverse("fetch", kwargs={"pk": obj.pk}),
static("documents/img/{}.png".format(obj.file_type)),
obj.file_type
)
document.allow_tags = True
2016-01-11 12:52:19 +00:00
admin.site.register(Sender)
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-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)