From 8432297d3c5d577836b65a89a0a53d93f7d6ac24 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Wed, 20 Sep 2023 19:39:05 -0400 Subject: [PATCH] [P UI] Make base url configurable (#5577) * made url_base configurable for P UI * fixed test for configurable base url --- InvenTree/InvenTree/middleware.py | 2 +- InvenTree/InvenTree/settings.py | 11 ++++++----- InvenTree/InvenTree/tests.py | 2 +- InvenTree/config_template.yaml | 2 ++ InvenTree/web/templatetags/spa_helper.py | 6 +++++- InvenTree/web/urls.py | 4 ++-- src/frontend/src/main.tsx | 7 +++++-- src/frontend/src/views/DesktopAppView.tsx | 3 ++- 8 files changed, 24 insertions(+), 13 deletions(-) diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py index 9566bec577..a5b7c8474b 100644 --- a/InvenTree/InvenTree/middleware.py +++ b/InvenTree/InvenTree/middleware.py @@ -64,7 +64,7 @@ class AuthRequiredMiddleware(object): elif request.path_info.startswith('/accounts/'): authorized = True - elif request.path_info.startswith('/platform/') or request.path_info.startswith('/assets/') or request.path_info == '/platform': + elif request.path_info.startswith(f'/{settings.PUI_URL_BASE}/') or request.path_info.startswith('/assets/') or request.path_info == f'/{settings.PUI_URL_BASE}': authorized = True elif 'Authorization' in request.headers.keys() or 'authorization' in request.headers.keys(): diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index c387dc19a7..75f4eab1eb 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -606,11 +606,6 @@ DATABASES = { REMOTE_LOGIN = get_boolean_setting('INVENTREE_REMOTE_LOGIN', 'remote_login_enabled', False) REMOTE_LOGIN_HEADER = get_setting('INVENTREE_REMOTE_LOGIN_HEADER', 'remote_login_header', 'REMOTE_USER') -# Magic login django-sesame -SESAME_MAX_AGE = 300 -# LOGIN_REDIRECT_URL = "/platform/logged-in/" -LOGIN_REDIRECT_URL = "/index/" - # sentry.io integration for error reporting SENTRY_ENABLED = get_boolean_setting('INVENTREE_SENTRY_ENABLED', 'sentry_enabled', False) @@ -981,6 +976,7 @@ CUSTOM_SPLASH = get_custom_file('INVENTREE_CUSTOM_SPLASH', 'customize.splash', ' CUSTOMIZE = get_setting('INVENTREE_CUSTOMIZE', 'customize', {}) # Frontend settings +PUI_URL_BASE = get_setting('INVENTREE_PUI_URL_BASE', 'pui_url_base', 'platform') PUI_SETTINGS = get_setting("INVENTREE_PUI_SETTINGS", "pui_settings", {}) if DEBUG: @@ -1008,3 +1004,8 @@ if CUSTOM_FLAGS: else: logger.info(f"Custom flags: {CUSTOM_FLAGS}") FLAGS.update(CUSTOM_FLAGS) + +# Magic login django-sesame +SESAME_MAX_AGE = 300 +# LOGIN_REDIRECT_URL = f"/{PUI_URL_BASE}/logged-in/" +LOGIN_REDIRECT_URL = "/index/" diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 138117a644..53f00ebb44 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1211,6 +1211,6 @@ class MagicLoginTest(InvenTreeTestCase): self.assertEqual(resp.url, '/index/') # Note: 2023-08-08 - This test has been changed because "platform UI" is not generally available yet # TODO: In the future, the URL comparison will need to be reverted - # self.assertEqual(resp.url, '/platform/logged-in/') + # self.assertEqual(resp.url, f'/{settings.PUI_URL_BASE}/logged-in/') # And we should be logged in again self.assertEqual(resp.wsgi_request.user, self.user) diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 2e25d5b832..00c0f4dad8 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -250,6 +250,8 @@ remote_login_header: HTTP_REMOTE_USER # show_server_selector: false # sentry_dsn: https://84f0c3ea90c64e5092e2bf5dfe325725@o1047628.ingest.sentry.io/4504160008273920 # environment: development +# Base URL for serving Platform UI +# pui_url_base: 'platform' # Custom flags # InvenTree uses django-flags; read more in their docs at https://cfpb.github.io/django-flags/conditions/ diff --git a/InvenTree/web/templatetags/spa_helper.py b/InvenTree/web/templatetags/spa_helper.py index 3087e7c34e..4d8ebd9ae7 100644 --- a/InvenTree/web/templatetags/spa_helper.py +++ b/InvenTree/web/templatetags/spa_helper.py @@ -10,7 +10,11 @@ from django.utils.safestring import mark_safe logger = getLogger("InvenTree") register = template.Library() -PUI_SETTINGS = json.dumps(settings.PUI_SETTINGS) +PUI_DEFAULTS = { + 'url_base': settings.PUI_URL_BASE, +} +PUI_DEFAULTS.update(getattr(settings, 'PUI_SETTINGS', {})) +PUI_SETTINGS = json.dumps(PUI_DEFAULTS) @register.simple_tag diff --git a/InvenTree/web/urls.py b/InvenTree/web/urls.py index 1f34880b49..d1e409eeab 100644 --- a/InvenTree/web/urls.py +++ b/InvenTree/web/urls.py @@ -20,12 +20,12 @@ spa_view = ensure_csrf_cookie(TemplateView.as_view(template_name="web/index.html urlpatterns = [ - path('platform/', include([ + path(f'{settings.PUI_URL_BASE}/', include([ path("assets/", RedirectAssetView.as_view()), re_path(r"^(?P.*)/$", spa_view), path("set-password?uid=&token=", spa_view, name="password_reset_confirm"), path("", spa_view),] )), - re_path(r'^platform', spa_view, name='platform'), + path(settings.PUI_URL_BASE, spa_view, name='platform'), path("assets/", RedirectAssetView.as_view()), ] diff --git a/src/frontend/src/main.tsx b/src/frontend/src/main.tsx index 205de488cb..fc3fe6e53a 100644 --- a/src/frontend/src/main.tsx +++ b/src/frontend/src/main.tsx @@ -14,6 +14,7 @@ declare global { server_list: HostList; default_server: string; show_server_selector: boolean; + url_base: string; sentry_dsn?: string; environment?: string; }; @@ -55,13 +56,15 @@ if (window.INVENTREE_SETTINGS.sentry_dsn) { }); } +export const url_base = window.INVENTREE_SETTINGS.url_base || 'platform'; + ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( ); -// Redirect to /platform if on / +// Redirect to base url if on / if (window.location.pathname === '/') { - window.location.replace('/platform'); + window.location.replace(`/${url_base}`); } diff --git a/src/frontend/src/views/DesktopAppView.tsx b/src/frontend/src/views/DesktopAppView.tsx index eb3027c3e8..a6d2c09b18 100644 --- a/src/frontend/src/views/DesktopAppView.tsx +++ b/src/frontend/src/views/DesktopAppView.tsx @@ -5,6 +5,7 @@ import { BrowserRouter } from 'react-router-dom'; import { queryClient, setApiDefaults } from '../App'; import { BaseContext } from '../contexts/BaseContext'; import { defaultHostList } from '../defaults/defaultHostList'; +import { url_base } from '../main'; import { routes } from '../router'; import { useApiState } from '../states/ApiState'; import { useLocalState } from '../states/LocalState'; @@ -35,7 +36,7 @@ export default function DesktopAppView() { return ( - {routes} + {routes} );