From e9b8c15c569a6ae35ff7c9f3faee835af895201f Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Mon, 15 Aug 2022 14:28:34 +0200 Subject: [PATCH] refactor custom file lookup (#3536) * refactor custom file lookup * Rename function --- InvenTree/InvenTree/config.py | 27 +++++++++++++++++++++++++ InvenTree/InvenTree/settings.py | 36 ++++----------------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/InvenTree/InvenTree/config.py b/InvenTree/InvenTree/config.py index 799788a0da..03e0b8c036 100644 --- a/InvenTree/InvenTree/config.py +++ b/InvenTree/InvenTree/config.py @@ -7,6 +7,9 @@ import shutil import string from pathlib import Path +from django.contrib.staticfiles.storage import StaticFilesStorage +from django.core.files.storage import default_storage + import yaml logger = logging.getLogger('inventree') @@ -203,3 +206,27 @@ def get_secret_key(): key_data = secret_key_file.read_text().strip() return key_data + + +def get_custom_file(env_ref: str, conf_ref: str, log_ref: str, lookup_media: bool = False): + """Returns the checked path to a custom file. + + Set lookup_media to True to also search in the media folder. + """ + value = get_setting(env_ref, conf_ref, None) + + if not value: + return None + + static_storage = StaticFilesStorage() + + if static_storage.exists(value): + logger.info(f"Loading {log_ref} from static directory: {value}") + elif lookup_media and default_storage.exists(value): + logger.info(f"Loading {log_ref} from media directory: {value}") + else: + add_dir_str = ' or media' if lookup_media else '' + logger.warning(f"The {log_ref} file '{value}' could not be found in the static{add_dir_str} directories") + value = False + + return value diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 2d4b898f74..f13a74fe83 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -16,8 +16,6 @@ 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 _ @@ -26,7 +24,7 @@ import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from . import config -from .config import get_boolean_setting, get_setting +from .config import get_boolean_setting, get_custom_file, get_setting # Determine if we are running in "test" mode e.g. "manage.py test" TESTING = 'test' in sys.argv @@ -818,37 +816,11 @@ PLUGIN_RETRY = CONFIG.get('PLUGIN_RETRY', 5) # how often should plugin loading PLUGIN_FILE_CHECKED = False # Was the plugin file checked? # User interface customization values +CUSTOM_LOGO = get_custom_file('INVENTREE_CUSTOM_LOGO', 'customize.logo', 'custom logo', lookup_media=True) +CUSTOM_SPLASH = get_custom_file('INVENTREE_CUSTOM_SPLASH', 'customize.splash', 'custom splash') + CUSTOMIZE = get_setting('INVENTREE_CUSTOMIZE', 'customize', {}) -static_storage = StaticFilesStorage() - -""" -Check for the existence of a 'custom logo' file: -- Check the 'static' directory -- Check the 'media' directory (legacy) -""" - -CUSTOM_LOGO = get_setting('INVENTREE_CUSTOM_LOGO', 'customize.logo', None) - -if CUSTOM_LOGO: - 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 - -# Check for a custom splash screen -CUSTOM_SPLASH = get_setting('INVENTREE_CUSTOM_SPLASH', 'customize.splash', None) - -if CUSTOM_SPLASH: - if static_storage.exists(CUSTOM_SPLASH): - logger.info(f"Loading custom splash screen from static directory: {CUSTOM_SPLASH}") - else: - logger.warning(f"The custom splash screen '{CUSTOM_SPLASH}' could not be found in the static directory") - CUSTOM_SPLASH = False - if DEBUG: logger.info("InvenTree running with DEBUG enabled")