From 9b4db43232542f26e7d46766aad50d9fa49d5bf4 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 30 Jun 2021 17:40:44 +1000 Subject: [PATCH] Refactoring "attachment" tables to use the API - Part attachments - StockItem attachments - PurchaseOrder attachments - SalesOrder attachments - BuildOrder attachments --- InvenTree/InvenTree/urls.py | 1 + .../build/templates/build/attachments.html | 51 ++++++------ .../order/templates/order/po_attachments.html | 62 +++++++------- .../order/templates/order/so_attachments.html | 65 +++++++-------- .../part/templates/part/attachments.html | 67 ++++++++------- .../templates/stock/item_attachments.html | 67 +++++++-------- InvenTree/templates/attachment_table.html | 31 +------ InvenTree/templates/base.html | 1 + InvenTree/templates/js/attachment.js | 82 +++++++++++++++++++ 9 files changed, 234 insertions(+), 193 deletions(-) create mode 100644 InvenTree/templates/js/attachment.js diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 8c0cf494be..6a0d02263d 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -103,6 +103,7 @@ settings_urls = [ # Some javascript files are served 'dynamically', allowing them to pass through the Django translation layer dynamic_javascript_urls = [ url(r'^api.js', DynamicJsView.as_view(template_name='js/api.js'), name='api.js'), + url(r'^attachment.js', DynamicJsView.as_view(template_name='js/attachment.js'), name='attachment.js'), url(r'^forms.js', DynamicJsView.as_view(template_name='js/forms.js'), name='forms.js'), url(r'^model_renderers.js', DynamicJsView.as_view(template_name='js/model_renderers.js'), name='model_renderers.js'), url(r'^modals.js', DynamicJsView.as_view(template_name='js/modals.js'), name='modals.js'), diff --git a/InvenTree/build/templates/build/attachments.html b/InvenTree/build/templates/build/attachments.html index 728c02fca7..e969756b81 100644 --- a/InvenTree/build/templates/build/attachments.html +++ b/InvenTree/build/templates/build/attachments.html @@ -47,37 +47,38 @@ $('#new-attachment').click(function() { } }, method: 'POST', - reload: true, + onSuccess: reloadAttachmentTable, title: '{% trans "Add Attachment" %}', }); }); -// Callback for editing an attachment -$("#attachment-table").on('click', '.attachment-edit-button', function() { - var pk = $(this).attr('pk'); - - constructForm(`/api/build/attachment/${pk}/`, { - fields: { - attachment: {}, - comment: {}, +loadAttachmentTable( + '{% url "api-build-attachment-list" %}', + { + filters: { + build: {{ build.pk }}, }, - reload: true, - title: '{% trans "Edit Attachment" %}', - }); -}); + onEdit: function(pk) { + var url = `/api/build/attachment/${pk}/`; -// Callback for deleting an attachment -$("#attachment-table").on('click', '.attachment-delete-button', function() { - var pk = $(this).attr('pk'); + constructForm(url, { + fields: { + comment: {}, + }, + onSuccess: reloadAttachmentTable, + title: '{% trans "Edit Attachment" %}', + }); + }, + onDelete: function(pk) { - constructForm(`/api/build/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - reload: true, - }); -}); - -$("#attachment-table").inventreeTable({}); + constructForm(`/api/build/attachment/${pk}/`, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete Operation" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + } + } +); {% endblock %} diff --git a/InvenTree/order/templates/order/po_attachments.html b/InvenTree/order/templates/order/po_attachments.html index a6ea769aee..07e425016b 100644 --- a/InvenTree/order/templates/order/po_attachments.html +++ b/InvenTree/order/templates/order/po_attachments.html @@ -34,6 +34,35 @@ enableDragAndDrop( } ); +loadAttachmentTable( + '{% url "api-po-attachment-list" %}', + { + filters: { + order: {{ order.pk }}, + }, + onEdit: function(pk) { + var url = `/api/order/po/attachment/${pk}/`; + + constructForm(url, { + fields: { + comment: {}, + }, + onSuccess: reloadAttachmentTable, + title: '{% trans "Edit Attachment" %}', + }); + }, + onDelete: function(pk) { + + constructForm(`/api/order/po/attachment/${pk}/`, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete Operation" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + } + } +); + $("#new-attachment").click(function() { constructForm('{% url "api-po-attachment-list" %}', { @@ -51,37 +80,4 @@ $("#new-attachment").click(function() { }); }); -$("#attachment-table").on('click', '.attachment-edit-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - var url = `/api/order/po/attachment/${pk}/`; - - constructForm(url, { - fields: { - attachment: {}, - comment: {}, - }, - reload: true, - title: '{% trans "Edit Attachment" %}', - }); -}); - -$("#attachment-table").on('click', '.attachment-delete-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - constructForm(`/api/order/po/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - reload: true, - }); -}); - -$("#attachment-table").inventreeTable({ -}); - {% endblock %} \ No newline at end of file diff --git a/InvenTree/order/templates/order/so_attachments.html b/InvenTree/order/templates/order/so_attachments.html index 6df93bbe45..89f09541d1 100644 --- a/InvenTree/order/templates/order/so_attachments.html +++ b/InvenTree/order/templates/order/so_attachments.html @@ -35,6 +35,36 @@ enableDragAndDrop( } ); +loadAttachmentTable( + '{% url "api-so-attachment-list" %}', + { + filters: { + order: {{ order.pk }}, + }, + onEdit: function(pk) { + var url = `/api/order/so/attachment/${pk}/`; + + constructForm(url, { + fields: { + comment: {}, + }, + onSuccess: reloadAttachmentTable, + title: '{% trans "Edit Attachment" %}', + }); + }, + onDelete: function(pk) { + var pk = button.attr('pk'); + + constructForm(`/api/order/so/attachment/${pk}/`, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete Operation" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + } + } +); + $("#new-attachment").click(function() { constructForm('{% url "api-so-attachment-list" %}', { @@ -47,42 +77,9 @@ $("#new-attachment").click(function() { hidden: true } }, - reload: true, + onSuccess: reloadAttachmentTable, title: '{% trans "Add Attachment" %}' }); }); -$("#attachment-table").on('click', '.attachment-edit-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - var url = `/api/order/so/attachment/${pk}/`; - - constructForm(url, { - fields: { - attachment: {}, - comment: {}, - }, - reload: true, - title: '{% trans "Edit Attachment" %}', - }); -}); - -$("#attachment-table").on('click', '.attachment-delete-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - constructForm(`/api/order/so/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - reload: true, - }); -}); - -$("#attachment-table").inventreeTable({ -}); - {% endblock %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/attachments.html b/InvenTree/part/templates/part/attachments.html index 5fca535098..7128980472 100644 --- a/InvenTree/part/templates/part/attachments.html +++ b/InvenTree/part/templates/part/attachments.html @@ -19,6 +19,36 @@ {% block js_ready %} {{ block.super }} + loadAttachmentTable( + '{% url "api-part-attachment-list" %}', + { + filters: { + part: {{ part.pk }}, + }, + onEdit: function(pk) { + var url = `/api/part/attachment/${pk}/`; + + constructForm(url, { + fields: { + comment: {}, + }, + title: '{% trans "Edit Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + }, + onDelete: function(pk) { + var url = `/api/part/attachment/${pk}/`; + + constructForm(url, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete Operation" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + } + } + ); + enableDragAndDrop( '#attachment-dropzone', '{% url "api-part-attachment-list" %}', @@ -28,7 +58,7 @@ }, label: 'attachment', success: function(data, status, xhr) { - location.reload(); + reloadAttachmentTable(); } } ); @@ -47,43 +77,10 @@ hidden: true, } }, - reload: true, + onSuccess: reloadAttachmentTable, title: '{% trans "Add Attachment" %}', } ) }); - $("#attachment-table").on('click', '.attachment-edit-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - var url = `/api/part/attachment/${pk}/`; - - constructForm(url, { - fields: { - attachment: {}, - comment: {}, - }, - title: '{% trans "Edit Attachment" %}', - reload: true, - }); - }); - - $("#attachment-table").on('click', '.attachment-delete-button', function() { - var button = $(this); - var pk = button.attr('pk'); - var url = `/api/part/attachment/${pk}/`; - - constructForm(url, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - reload: true, - }); - }); - - $("#attachment-table").inventreeTable({ - }); - {% endblock %} \ No newline at end of file diff --git a/InvenTree/stock/templates/stock/item_attachments.html b/InvenTree/stock/templates/stock/item_attachments.html index f58d5db063..34ceecc550 100644 --- a/InvenTree/stock/templates/stock/item_attachments.html +++ b/InvenTree/stock/templates/stock/item_attachments.html @@ -28,11 +28,41 @@ enableDragAndDrop( }, label: 'attachment', success: function(data, status, xhr) { - location.reload(); + reloadAttachmentTable(); } } ); +loadAttachmentTable( + '{% url "api-stock-attachment-list" %}', + { + filters: { + item: {{ item.pk }}, + }, + onEdit: function(pk) { + var url = `/api/stock/attachment/${pk}/`; + + constructForm(url, { + fields: { + comment: {}, + }, + title: '{% trans "Edit Attachment" %}', + onSuccess: reloadAttachmentTable + }); + }, + onDelete: function(pk) { + var url = `/api/stock/attachment/${pk}/`; + + constructForm(url, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete Operation" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); + } + } +); + $("#new-attachment").click(function() { constructForm( @@ -53,39 +83,4 @@ $("#new-attachment").click(function() { ); }); -$("#attachment-table").on('click', '.attachment-edit-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - var url = `/api/stock/attachment/${pk}/`; - - constructForm(url, { - fields: { - attachment: {}, - comment: {}, - }, - title: '{% trans "Edit Attachment" %}', - reload: true - }); -}); - -$("#attachment-table").on('click', '.attachment-delete-button', function() { - var button = $(this); - - var pk = button.attr('pk'); - - var url = `/api/stock/attachment/${pk}/`; - - constructForm(url, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - reload: true, - }); -}); - -$("#attachment-table").inventreeTable({ -}); - {% endblock %} \ No newline at end of file diff --git a/InvenTree/templates/attachment_table.html b/InvenTree/templates/attachment_table.html index 35b114cc05..18a4da9acc 100644 --- a/InvenTree/templates/attachment_table.html +++ b/InvenTree/templates/attachment_table.html @@ -10,35 +10,6 @@
- - - - - - - - - - {% for attachment in attachments %} - - - - - - - {% endfor %} - +
{% trans "File" %}{% trans "Comment" %}{% trans "Uploaded" %}
{{ attachment.basename }}{{ attachment.comment }} - {% if attachment.upload_date %}{{ attachment.upload_date }}{% endif %} - {% if attachment.user %}{{ attachment.user.username }}{% endif %} - -
- - -
-
\ No newline at end of file diff --git a/InvenTree/templates/base.html b/InvenTree/templates/base.html index ec7e31a7f0..76104d8fe2 100644 --- a/InvenTree/templates/base.html +++ b/InvenTree/templates/base.html @@ -149,6 +149,7 @@ + diff --git a/InvenTree/templates/js/attachment.js b/InvenTree/templates/js/attachment.js new file mode 100644 index 0000000000..4fce7e06fe --- /dev/null +++ b/InvenTree/templates/js/attachment.js @@ -0,0 +1,82 @@ +{% load i18n %} + +function reloadAttachmentTable() { + + $('#attachment-table').bootstrapTable("refresh"); +} + + +function loadAttachmentTable(url, options) { + + var table = options.table || '#attachment-table'; + + $(table).inventreeTable({ + url: url, + name: options.name || 'attachments', + formatNoMatches: function() { return '{% trans "No attachments found" %}'}, + sortable: true, + search: false, + queryParams: options.filters || {}, + onPostBody: function() { + // Add callback for 'edit' button + $(table).find('.button-attachment-edit').click(function() { + var pk = $(this).attr('pk'); + + if (options.onEdit) { + options.onEdit(pk); + } + }); + + // Add callback for 'delete' button + $(table).find('.button-attachment-delete').click(function() { + var pk = $(this).attr('pk'); + + if (options.onDelete) { + options.onDelete(pk); + } + }); + }, + columns: [ + { + field: 'attachment', + title: '{% trans "File" %}', + formatter: function(value, row) { + + var split = value.split('/'); + + return renderLink(split[split.length - 1], value); + } + }, + { + field: 'comment', + title: '{% trans "Comment" %}', + }, + { + field: 'actions', + formatter: function(value, row) { + var html = ''; + + html = `
`; + + html += makeIconButton( + 'fa-edit icon-blue', + 'button-attachment-edit', + row.pk, + '{% trans "Edit attachment" %}', + ); + + html += makeIconButton( + 'fa-trash-alt icon-red', + 'button-attachment-delete', + row.pk, + '{% trans "Delete attachment" %}', + ); + + html += `
`; + + return html; + } + } + ] + }); +} \ No newline at end of file