2020-11-28 20:09:37 +01:00
|
|
|
from django.contrib import admin, messages
|
2020-08-26 19:04:48 +02:00
|
|
|
from django.contrib.admin import register
|
2020-09-02 13:14:36 +02:00
|
|
|
from django.utils.html import format_html
|
2020-08-26 19:04:48 +02:00
|
|
|
|
|
|
|
from attachments.models import Attachment
|
|
|
|
|
|
|
|
|
|
|
|
@register(Attachment)
|
|
|
|
class AttachmentAdmin(admin.ModelAdmin):
|
2020-08-28 21:49:15 +02:00
|
|
|
list_display = [
|
|
|
|
"description",
|
|
|
|
"original_file",
|
|
|
|
"original_file_url",
|
|
|
|
"processed_file",
|
|
|
|
"processed_file_url",
|
2020-11-28 20:09:37 +01:00
|
|
|
"open_graph_image",
|
2020-08-28 21:49:15 +02:00
|
|
|
]
|
2020-08-26 19:04:48 +02:00
|
|
|
list_display_links = ["description"]
|
2020-09-02 13:14:36 +02:00
|
|
|
fields = [
|
|
|
|
"description",
|
|
|
|
"original_file",
|
|
|
|
"original_file_url",
|
|
|
|
"processed_file",
|
|
|
|
"processed_file_url",
|
2020-11-28 20:09:37 +01:00
|
|
|
"open_graph_image",
|
2020-09-02 13:14:36 +02:00
|
|
|
]
|
|
|
|
readonly_fields = [
|
|
|
|
"original_file_url",
|
|
|
|
"processed_file_url",
|
2020-11-28 20:09:37 +01:00
|
|
|
"open_graph_image",
|
2020-09-02 13:14:36 +02:00
|
|
|
]
|
2020-12-27 19:23:13 +01:00
|
|
|
actions = [
|
|
|
|
"set_as_open_graph_image",
|
|
|
|
"reprocess_selected_attachments",
|
|
|
|
]
|
2020-12-30 11:32:26 +01:00
|
|
|
search_fields = ["description", "original_file", "processed_file"]
|
2020-08-28 21:49:15 +02:00
|
|
|
|
|
|
|
class Media:
|
|
|
|
js = ["attachments/js/copy_url.js"]
|
|
|
|
|
|
|
|
def processed_file_url(self, instance):
|
2020-08-28 21:58:18 +02:00
|
|
|
if instance.processed_file:
|
2020-09-02 13:14:36 +02:00
|
|
|
return format_html(
|
|
|
|
'{} <a class="copy-button" data-to-copy="{}" href="#">📋</a>',
|
|
|
|
instance.processed_file.url,
|
|
|
|
instance.processed_file.url,
|
|
|
|
)
|
2020-08-28 21:58:18 +02:00
|
|
|
return ""
|
2020-08-28 21:49:15 +02:00
|
|
|
|
|
|
|
def original_file_url(self, instance):
|
2020-08-28 21:58:18 +02:00
|
|
|
if instance.original_file:
|
2020-09-02 13:14:36 +02:00
|
|
|
return format_html(
|
|
|
|
'{} <a class="copy-button" data-to-copy="{}" href="#">📋</a>',
|
|
|
|
instance.original_file.url,
|
|
|
|
instance.original_file.url,
|
|
|
|
)
|
2020-08-28 21:58:18 +02:00
|
|
|
return ""
|
2020-11-28 20:09:37 +01:00
|
|
|
|
|
|
|
def set_as_open_graph_image(self, request, queryset):
|
|
|
|
if len(queryset) != 1:
|
|
|
|
messages.error(request, "You must select only one attachment")
|
|
|
|
return
|
|
|
|
Attachment.objects.update(open_graph_image=False)
|
|
|
|
queryset.update(open_graph_image=True)
|
|
|
|
messages.success(request, "Done")
|
|
|
|
|
|
|
|
set_as_open_graph_image.short_description = "Set as open graph image"
|
2020-12-27 19:23:13 +01:00
|
|
|
|
|
|
|
def reprocess_selected_attachments(self, request, queryset):
|
|
|
|
if len(queryset) == 0:
|
|
|
|
messages.error(request, "You must select at least one attachment")
|
|
|
|
return
|
|
|
|
for attachment in queryset:
|
|
|
|
attachment.reprocess()
|
|
|
|
messages.success(request, "Attachments were successfully reprocessed.")
|
|
|
|
|
|
|
|
reprocess_selected_attachments.short_description = "Reprocess selected attachments"
|