diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 4326d3b377..3bdab4c472 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -106,11 +106,11 @@ def getBlankThumbnail(): return getStaticUrl("img/blank_image.thumbnail.png") -def getLogoImage(as_file=False): +def getLogoImage(as_file=False, custom=True): """Return the InvenTree logo image, or a custom logo if available.""" """Return the path to the logo-file.""" - if settings.CUSTOM_LOGO: + if custom and settings.CUSTOM_LOGO: if as_file: return f"file://{default_storage.path(settings.CUSTOM_LOGO)}" diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 50837926d9..39ca0f7cd7 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -535,9 +535,6 @@ class ReportAsset(models.Model): and can be loaded in a template using the {% report_asset %} tag. """ - # String keys used for uniquely indentifying particular assets - ASSET_COMPANY_LOGO = "COMPANY_LOGO" - def __str__(self): """String representation of a ReportAsset instance""" return os.path.basename(self.asset.name) diff --git a/InvenTree/report/templatetags/report.py b/InvenTree/report/templatetags/report.py index 94211fbfed..3b7863e2f4 100644 --- a/InvenTree/report/templatetags/report.py +++ b/InvenTree/report/templatetags/report.py @@ -1,5 +1,6 @@ """Custom template tags for report generation.""" +import logging import os from django import template @@ -14,6 +15,9 @@ from part.models import Part register = template.Library() +logger = logging.getLogger('inventree') + + @register.simple_tag() def asset(filename): """Return fully-qualified path for an upload report asset file. @@ -65,6 +69,10 @@ def uploaded_image(filename, replace_missing=True, replacement_file='blank_image except Exception: exists = False + if exists and not InvenTree.helpers.TestIfImage(full_path): + logger.warning(f"File '{filename}' is not a valid image") + exists = False + if not exists and not replace_missing: raise FileNotFoundError(f"Image file '{filename}' not found") @@ -126,7 +134,7 @@ def company_image(company): @register.simple_tag() -def logo_image(): +def logo_image(**kwargs): """Return a fully-qualified path for the logo image. - If a custom logo has been provided, return a path to that logo @@ -136,7 +144,7 @@ def logo_image(): # If in debug mode, return URL to the image, not a local file debug_mode = InvenTreeSetting.get_setting('REPORT_DEBUG_MODE') - return InvenTree.helpers.getLogoImage(as_file=not debug_mode) + return InvenTree.helpers.getLogoImage(as_file=not debug_mode, **kwargs) @register.simple_tag() diff --git a/InvenTree/report/tests.py b/InvenTree/report/tests.py index bed13b198c..f9e4c070fb 100644 --- a/InvenTree/report/tests.py +++ b/InvenTree/report/tests.py @@ -9,6 +9,8 @@ from django.http.response import StreamingHttpResponse from django.test import TestCase from django.urls import reverse +from PIL import Image + import report.models as report_models from build.models import Build from common.models import InvenTreeSetting, InvenTreeUserSetting @@ -74,9 +76,17 @@ class ReportTagTest(TestCase): with open(img_file, 'w') as f: f.write("dummy data") - # Test in debug mode + # Test in debug mode. Returns blank image as dummy file is not a valid image self.debug_mode(True) img = report_tags.uploaded_image('part/images/test.jpg') + self.assertEqual(img, '/static/img/blank_image.png') + + # Now, let's create a proper image + img = Image.new('RGB', (128, 128), color='RED') + img.save(img_file) + + # Try again + img = report_tags.uploaded_image('part/images/test.jpg') self.assertEqual(img, '/media/part/images/test.jpg') self.debug_mode(False)