diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index f3be374e34..82b32ec4d5 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -145,12 +145,20 @@ def download_image_from_url(remote_url, timeout=2.5): # Calculate maximum allowable image size (in bytes) max_size = int(InvenTreeSetting.get_setting('INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE')) * 1024 * 1024 + # Add user specified user-agent to request (if specified) + user_agent = InvenTreeSetting.get_setting('INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT') + if user_agent: + headers = {"User-Agent": user_agent} + else: + headers = None + try: response = requests.get( remote_url, timeout=timeout, allow_redirects=True, stream=True, + headers=headers, ) # Throw an error if anything goes wrong response.raise_for_status() diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 60b9df5ebf..33800e2387 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -875,6 +875,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): ] }, + 'INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT': { + 'name': _('User-agent used to download from URL'), + 'description': _('Allow to override the user-agent used to download images and files from external URL (leave blank for the default)'), + 'default': '', + }, + 'INVENTREE_REQUIRE_CONFIRM': { 'name': _('Require confirm'), 'description': _('Require explicit user confirmation for certain action.'), diff --git a/InvenTree/company/templates/company/company_base.html b/InvenTree/company/templates/company/company_base.html index 6a28e5ba67..2bf017b506 100644 --- a/InvenTree/company/templates/company/company_base.html +++ b/InvenTree/company/templates/company/company_base.html @@ -58,9 +58,7 @@ {% if allow_download %} {% endif %} - {% if company.image %} - {% endif %} @@ -176,6 +174,7 @@ showModalImage(data.image); }); + $('#company-image-delete').show(); } else { location.reload(); } @@ -197,6 +196,9 @@ $('#company-image').click(function() { showModalImage('{{ company.image.url }}'); }); + {% else %} + $('#company-image-delete').hide(); + {% endif %} $('#company-image-delete').click(function(event) { event.stopPropagation(); @@ -224,8 +226,6 @@ ); }); - {% endif %} - $("#company-image-upload").click(function(event) { event.stopPropagation(); constructForm( @@ -244,8 +244,8 @@ }); if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) { - $('#company-image-url').click(function() { + event.stopPropagation(); constructForm( '{% url "api-company-detail" company.pk %}', { diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index 1e005b1adb..f358e4ec01 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -391,6 +391,8 @@ $('#part-thumb').click(function() { showModalImage('{{ part.image.url }}'); }); + {% else %} + $('#part-image-delete').hide(); {% endif %} function reloadImage(data) { @@ -403,6 +405,7 @@ showModalImage(data.image); }); + $("#part-image-delete").show(); } else { // Otherwise, reload the page location.reload(); @@ -586,8 +589,8 @@ {% if roles.part.change %} if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) { - $("#part-image-url").click(function() { + event.stopPropagation(); constructForm( '{% url "api-part-detail" part.pk %}', { @@ -596,7 +599,9 @@ fields: { remote_image: {}, }, - onSuccess: onSelectImage, + onSuccess: function(data) { + reloadImage(data); + }, } ); }); diff --git a/InvenTree/part/templates/part/part_thumb.html b/InvenTree/part/templates/part/part_thumb.html index f35646dd0f..d676e4ce9a 100644 --- a/InvenTree/part/templates/part/part_thumb.html +++ b/InvenTree/part/templates/part/part_thumb.html @@ -13,9 +13,7 @@ {% if allow_download %} {% endif %} - {% if part.image %} - {% endif %} {% endif %} diff --git a/InvenTree/templates/InvenTree/settings/global.html b/InvenTree/templates/InvenTree/settings/global.html index aab649b0bc..b7516e641d 100644 --- a/InvenTree/templates/InvenTree/settings/global.html +++ b/InvenTree/templates/InvenTree/settings/global.html @@ -21,6 +21,7 @@ {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE" icon="fa-server" %} + {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT" icon="fa-server" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_REQUIRE_CONFIRM" icon="fa-check" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_TREE_DEPTH" icon="fa-sitemap" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_BACKUP_ENABLE" icon="fa-hdd" %}