paperless-ngx/src/documents/admin.py

139 lines
3.5 KiB
Python
Raw Normal View History

from django.contrib import admin
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
from whoosh.writing import AsyncWriter
2015-12-20 19:23:33 +00:00
from . import index
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",
"match",
"matching_algorithm"
2018-09-23 12:41:28 +01:00
)
list_filter = ("matching_algorithm",)
list_editable = ("match", "matching_algorithm")
class TagAdmin(admin.ModelAdmin):
2016-01-28 18:37:27 +00:00
2018-09-25 16:09:33 +02:00
list_display = (
"name",
"colour",
"match",
"matching_algorithm"
2020-10-21 12:53:14 +02:00
)
list_filter = ("colour", "matching_algorithm")
list_editable = ("colour", "match", "matching_algorithm")
2016-01-28 18:37:27 +00:00
class DocumentTypeAdmin(admin.ModelAdmin):
2018-08-24 13:45:15 +02:00
2020-10-21 12:53:14 +02:00
list_display = (
"name",
"match",
"matching_algorithm"
2020-10-21 12:53:14 +02:00
)
list_filter = ("matching_algorithm",)
list_editable = ("match", "matching_algorithm")
2018-08-24 13:45:15 +02:00
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")
2020-12-07 21:51:00 +01:00
readonly_fields = (
"added",
"modified",
"mime_type",
"storage_type",
"filename")
list_display_links = ("title",)
2020-11-12 21:09:45 +01:00
list_display = (
"correspondent",
"title",
2020-11-12 21:09:45 +01:00
"tags_",
"created",
2020-11-12 21:09:45 +01:00
)
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
def delete_queryset(self, request, queryset):
ix = index.open_index()
with AsyncWriter(ix) as writer:
for o in queryset:
index.remove_document(writer, o)
super(DocumentAdmin, self).delete_queryset(request, queryset)
def delete_model(self, request, obj):
index.remove_document_from_index(obj)
super(DocumentAdmin, self).delete_model(request, obj)
def save_model(self, request, obj, form, change):
index.add_or_update_document(obj)
super(DocumentAdmin, self).save_model(request, obj, form, change)
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-27 20:18:50 +00:00
r += self._html_tag(
"span",
tag.name + ", "
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
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False
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
ordering = ('-created',)
list_display_links = ("created", "message")
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)