From fc90e1d91d5d3875359d4aa7bb3a748bfd11db6a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 5 Nov 2024 13:19:09 +0000 Subject: [PATCH] [PUI] Updates for AdminButton - Hide if django admin interface is disabled - Use correct admin URL --- src/backend/InvenTree/InvenTree/api.py | 21 +++++++++++++------ .../src/components/buttons/AdminButton.tsx | 13 ++++++++---- src/frontend/src/defaults/defaults.tsx | 3 ++- src/frontend/src/states/states.tsx | 1 + 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/api.py b/src/backend/InvenTree/InvenTree/api.py index 022c9d6628af..973b2c620897 100644 --- a/src/backend/InvenTree/InvenTree/api.py +++ b/src/backend/InvenTree/InvenTree/api.py @@ -230,14 +230,23 @@ def get(self, request, *args, **kwargs): 'debug_mode': settings.DEBUG, 'docker_mode': settings.DOCKER, 'default_locale': settings.LANGUAGE_CODE, - # Following fields are only available to staff users - 'system_health': check_system_health() if is_staff else None, - 'database': InvenTree.version.inventreeDatabase() if is_staff else None, - 'platform': InvenTree.version.inventreePlatform() if is_staff else None, - 'installer': InvenTree.version.inventreeInstaller() if is_staff else None, - 'target': InvenTree.version.inventreeTarget() if is_staff else None, } + if is_staff: + # Following fields are only available to staff users + data.update({ + 'system_health': check_system_health() if is_staff else None, + 'database': InvenTree.version.inventreeDatabase() if is_staff else None, + 'platform': InvenTree.version.inventreePlatform() if is_staff else None, + 'installer': InvenTree.version.inventreeInstaller() + if is_staff + else None, + 'target': InvenTree.version.inventreeTarget() if is_staff else None, + 'django_admin': settings.INVENTREE_ADMIN_URL + if settings.INVENTREE_ADMIN_ENABLED + else None, + }) + return JsonResponse(data) def check_auth_header(self, request): diff --git a/src/frontend/src/components/buttons/AdminButton.tsx b/src/frontend/src/components/buttons/AdminButton.tsx index cccf7c6c4827..60a7107ecdb1 100644 --- a/src/frontend/src/components/buttons/AdminButton.tsx +++ b/src/frontend/src/components/buttons/AdminButton.tsx @@ -3,6 +3,7 @@ import { IconUserStar } from '@tabler/icons-react'; import { useCallback, useMemo } from 'react'; import { ModelType } from '../../enums/ModelType'; +import { useServerApiState } from '../../states/ApiState'; import { useLocalState } from '../../states/LocalState'; import { useUserState } from '../../states/UserState'; import { ModelInformationDict } from '../render/ModelType'; @@ -24,6 +25,7 @@ export type AdminButtonProps = { */ export default function AdminButton(props: Readonly) { const user = useUserState(); + const server = useServerApiState(); const enabled: boolean = useMemo(() => { // Only users with superuser permission will see this button @@ -31,10 +33,13 @@ export default function AdminButton(props: Readonly) { return false; } - // TODO: Check if the server has the admin interface enabled - const modelDef = ModelInformationDict[props.model]; + // Check if the server has the admin interface enabled + if (!server.server.django_admin) { + return false; + } + // No admin URL associated with the model if (!modelDef.admin_url) { return false; @@ -57,8 +62,8 @@ export default function AdminButton(props: Readonly) { return; } - // TODO: Check the actual "admin" URL (it may be custom) - const url = `${host}/admin${modelDef.admin_url}${props.pk}/`; + // Generate the URL for the admin interface + const url = `${host}/${server.server.django_admin}${modelDef.admin_url}${props.pk}/`; if (event?.ctrlKey || event?.shiftKey) { // Open the link in a new tab diff --git a/src/frontend/src/defaults/defaults.tsx b/src/frontend/src/defaults/defaults.tsx index 60cecc7bf50b..00f2286eb8a5 100644 --- a/src/frontend/src/defaults/defaults.tsx +++ b/src/frontend/src/defaults/defaults.tsx @@ -18,7 +18,8 @@ export const emptyServerAPI = { platform: null, installer: null, target: null, - default_locale: null + default_locale: null, + django_admin: null }; export interface SiteMarkProps { diff --git a/src/frontend/src/states/states.tsx b/src/frontend/src/states/states.tsx index d3bf6e14f397..a88c9134bd6c 100644 --- a/src/frontend/src/states/states.tsx +++ b/src/frontend/src/states/states.tsx @@ -47,6 +47,7 @@ export interface ServerAPIProps { installer: null | string; target: null | string; default_locale: null | string; + django_admin: null | string; } export interface AuthProps {