From 83b471b4f7ab8a92c5f341c32bf0482bc50c51ca Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 7 Aug 2022 22:44:51 +1000 Subject: [PATCH] Improved loading for custom logo (#3489) - First check the 'static' directory - Second check the 'media' directory (backwards compatibility) - Third use the default logo --- InvenTree/InvenTree/helpers.py | 29 ++++++++++++++++++++--------- InvenTree/InvenTree/settings.py | 21 +++++++++++++++++---- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 046f3f2d5c..51dc08bd47 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -12,6 +12,7 @@ from wsgiref.util import FileWrapper from django.conf import settings from django.contrib.auth.models import Permission +from django.contrib.staticfiles.storage import StaticFilesStorage from django.core.exceptions import FieldError, ValidationError from django.core.files.storage import default_storage from django.core.validators import URLValidator @@ -241,17 +242,27 @@ def getLogoImage(as_file=False, custom=True): """Return the path to the logo-file.""" if custom and settings.CUSTOM_LOGO: - if as_file: - return f"file://{default_storage.path(settings.CUSTOM_LOGO)}" - else: - return default_storage.url(settings.CUSTOM_LOGO) + static_storage = StaticFilesStorage() - else: - if as_file: - path = settings.STATIC_ROOT.joinpath('img/inventree.png') - return f"file://{path}" + if static_storage.exists(settings.CUSTOM_LOGO): + storage = static_storage + elif default_storage.exists(settings.CUSTOM_LOGO): + storage = default_storage else: - return getStaticUrl('img/inventree.png') + storage = None + + if storage is not None: + if as_file: + return f"file://{storage.path(settings.CUSTOM_LOGO)}" + else: + return storage.url(settings.CUSTOM_LOGO) + + # If we have got to this point, return the default logo + if as_file: + path = settings.STATIC_ROOT.joinpath('img/inventree.png') + return f"file://{path}" + else: + return getStaticUrl('img/inventree.png') def TestIfImageURL(url): diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 44633456e7..c37159c387 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -16,6 +16,7 @@ import sys from pathlib import Path import django.conf.locale +from django.contrib.staticfiles.storage import StaticFilesStorage from django.core.files.storage import default_storage from django.http import Http404 from django.utils.translation import gettext_lazy as _ @@ -820,10 +821,22 @@ CUSTOMIZE = get_setting('INVENTREE_CUSTOMIZE', 'customize', {}) CUSTOM_LOGO = get_setting('INVENTREE_CUSTOM_LOGO', 'customize.logo', None) -# check that the logo-file exsists in media -if CUSTOM_LOGO and not default_storage.exists(CUSTOM_LOGO): # pragma: no cover - logger.warning(f"The custom logo file '{CUSTOM_LOGO}' could not be found in the default media storage") - CUSTOM_LOGO = False +""" +Check for the existence of a 'custom logo' file: +- Check the 'static' directory +- Check the 'media' directory (legacy) +""" + +if CUSTOM_LOGO: + static_storage = StaticFilesStorage() + + if static_storage.exists(CUSTOM_LOGO): + logger.info(f"Loading custom logo from static directory: {CUSTOM_LOGO}") + elif default_storage.exists(CUSTOM_LOGO): + logger.info(f"Loading custom logo from media directory: {CUSTOM_LOGO}") + else: + logger.warning(f"The custom logo file '{CUSTOM_LOGO}' could not be found in the static or media directories") + CUSTOM_LOGO = False if DEBUG: logger.info("InvenTree running with DEBUG enabled")