From 6898bb22a572d75346a97280f5e4ce5eee5376dc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 28 Aug 2020 21:49:15 +0200 Subject: [PATCH] Display files urls and copy button --- attachments/admin.py | 17 ++++++++++++++++- attachments/static/attachments/js/copy_url.js | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 attachments/static/attachments/js/copy_url.js diff --git a/attachments/admin.py b/attachments/admin.py index 859f088..afa789a 100644 --- a/attachments/admin.py +++ b/attachments/admin.py @@ -6,5 +6,20 @@ from attachments.models import Attachment @register(Attachment) class AttachmentAdmin(admin.ModelAdmin): - list_display = ["description", "original_file", "processed_file"] + list_display = [ + "description", + "original_file", + "original_file_url", + "processed_file", + "processed_file_url", + ] list_display_links = ["description"] + + class Media: + js = ["attachments/js/copy_url.js"] + + def processed_file_url(self, instance): + return instance.processed_file.url + + def original_file_url(self, instance): + return instance.original_file.url diff --git a/attachments/static/attachments/js/copy_url.js b/attachments/static/attachments/js/copy_url.js new file mode 100644 index 0000000..a067f9e --- /dev/null +++ b/attachments/static/attachments/js/copy_url.js @@ -0,0 +1,17 @@ +function copy(data) { + navigator.clipboard.writeText(data).then(() => { + console.log("Copied"); + }) +} + +$(document).ready(function() { + const $ = django.jQuery; + const fileUrls = $('td.field-processed_file_url, td.field-original_file_url'); + for (let fileUrl of fileUrls) { + fileUrl = $(fileUrl); + const existingText = fileUrl.text(); + const copyButton = `📋`; + let innerHTML = `${existingText} ${copyButton}`; + fileUrl.html(innerHTML); + } +});