refactor custom file lookup (#3536)

* refactor custom file lookup

* Rename function
This commit is contained in:
Matthias Mair 2022-08-15 14:28:34 +02:00 committed by GitHub
parent b0e91e7068
commit e9b8c15c56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 32 deletions

View File

@ -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

View File

@ -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")