mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Remove code left over from previous commit (#3385)
* Remove code left over from previous commit * Handle invalid image loaded with uploaded_image report template tag * Add option to *not* use a custom logo * Fix unit tests
This commit is contained in:
parent
779646771d
commit
d32054b53b
@ -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)}"
|
||||
|
@ -535,9 +535,6 @@ class ReportAsset(models.Model):
|
||||
and can be loaded in a template using the {% report_asset <filename> %} 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)
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user