2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

Improved loading for custom logo (#3489)

- First check the 'static' directory
- Second check the 'media' directory (backwards compatibility)
- Third use the default logo
This commit is contained in:
Oliver 2022-08-07 22:44:51 +10:00 committed by GitHub
parent e1d3392f37
commit 83b471b4f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 13 deletions

View File

@ -12,6 +12,7 @@ from wsgiref.util import FileWrapper
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.core.exceptions import FieldError, ValidationError from django.core.exceptions import FieldError, ValidationError
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.core.validators import URLValidator from django.core.validators import URLValidator
@ -241,17 +242,27 @@ def getLogoImage(as_file=False, custom=True):
"""Return the path to the logo-file.""" """Return the path to the logo-file."""
if custom and settings.CUSTOM_LOGO: if custom and settings.CUSTOM_LOGO:
if as_file: static_storage = StaticFilesStorage()
return f"file://{default_storage.path(settings.CUSTOM_LOGO)}"
else:
return default_storage.url(settings.CUSTOM_LOGO)
else: if static_storage.exists(settings.CUSTOM_LOGO):
if as_file: storage = static_storage
path = settings.STATIC_ROOT.joinpath('img/inventree.png') elif default_storage.exists(settings.CUSTOM_LOGO):
return f"file://{path}" storage = default_storage
else: 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): def TestIfImageURL(url):

View File

@ -16,6 +16,7 @@ import sys
from pathlib import Path from pathlib import Path
import django.conf.locale import django.conf.locale
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.http import Http404 from django.http import Http404
from django.utils.translation import gettext_lazy as _ 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) 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 Check for the existence of a 'custom logo' file:
logger.warning(f"The custom logo file '{CUSTOM_LOGO}' could not be found in the default media storage") - Check the 'static' directory
CUSTOM_LOGO = False - 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: if DEBUG:
logger.info("InvenTree running with DEBUG enabled") logger.info("InvenTree running with DEBUG enabled")