paperless-ngx/src/documents/admin.py

109 lines
2.7 KiB
Python
Raw Normal View History

from django.contrib import admin
2018-09-23 12:41:28 +01:00
from django.contrib.auth.models import Group, User
2018-09-06 10:15:15 +02:00
from django.utils.html import format_html, format_html_join
2018-07-04 17:03:59 +02:00
from django.utils.safestring import mark_safe
2015-12-20 19:23:33 +00:00
from .models import Correspondent, Document, DocumentType, Log, Tag
2015-12-20 19:23:33 +00:00
class CorrespondentAdmin(admin.ModelAdmin):
2018-09-23 12:41:28 +01:00
list_display = (
"name",
2020-10-21 12:53:14 +02:00
"automatic_classification"
2018-09-23 12:41:28 +01:00
)
2018-09-25 16:09:33 +02:00
list_editable = ("automatic_classification",)
readonly_fields = ("slug",)
class TagAdmin(admin.ModelAdmin):
2016-01-28 18:37:27 +00:00
2018-09-25 16:09:33 +02:00
list_display = (
"name",
"colour",
2020-10-21 12:53:14 +02:00
"automatic_classification"
)
list_filter = ("colour",)
list_editable = ("colour", "automatic_classification")
2016-01-28 18:37:27 +00:00
readonly_fields = ("slug",)
class DocumentTypeAdmin(admin.ModelAdmin):
2018-08-24 13:45:15 +02:00
2020-10-21 12:53:14 +02:00
list_display = (
"name",
"automatic_classification"
)
list_editable = ("automatic_classification",)
2018-08-24 13:45:15 +02:00
readonly_fields = ("slug",)
2018-09-13 14:15:16 +02:00
class DocumentAdmin(admin.ModelAdmin):
2016-02-06 17:27:17 +00:00
search_fields = ("correspondent__name", "title", "content", "tags__name")
readonly_fields = ("added", "file_type", "storage_type",)
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 = (
"document_type",
2018-09-23 12:41:28 +01:00
"tags",
"correspondent"
2018-09-23 12:41:28 +01:00
)
filter_horizontal = ("tags",)
ordering = ["-created", "correspondent"]
2015-12-20 19:23:33 +00:00
2018-09-23 12:41:28 +01:00
date_hierarchy = "created"
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
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(
"span",
tag.slug + ", "
2016-01-23 04:40:35 +00:00
)
return r
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:
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
return format_html("<{} {}/>", kind, attributes)
2016-02-27 20:18:50 +00:00
class LogAdmin(admin.ModelAdmin):
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
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.
admin.site.unregister(Group)
admin.site.unregister(User)