From 245803b0d448db668064e180b77f86afde94303d Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 6 Nov 2024 09:27:43 +1100 Subject: [PATCH] [PUI] Updates for AdminButton (#8434) * [PUI] Updates for AdminButton - Hide if django admin interface is disabled - Use correct admin URL * Adjust InfoView * Add playwright tests --- src/backend/InvenTree/InvenTree/api.py | 3 +++ src/frontend/playwright.config.ts | 3 ++- .../src/components/buttons/AdminButton.tsx | 13 +++++++++---- src/frontend/src/defaults/defaults.tsx | 3 ++- src/frontend/src/states/states.tsx | 1 + src/frontend/tests/pui_general.spec.ts | 15 +++++++++++++++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/api.py b/src/backend/InvenTree/InvenTree/api.py index 022c9d6628af..3771c411b5e1 100644 --- a/src/backend/InvenTree/InvenTree/api.py +++ b/src/backend/InvenTree/InvenTree/api.py @@ -236,6 +236,9 @@ def get(self, request, *args, **kwargs): '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 (is_staff and settings.INVENTREE_ADMIN_ENABLED) + else None, } return JsonResponse(data) diff --git a/src/frontend/playwright.config.ts b/src/frontend/playwright.config.ts index f42af97995c2..3bdc40fcd41b 100644 --- a/src/frontend/playwright.config.ts +++ b/src/frontend/playwright.config.ts @@ -39,7 +39,8 @@ export default defineConfig({ command: 'invoke dev.server -a 127.0.0.1:8000', env: { INVENTREE_DEBUG: 'True', - INVENTREE_PLUGINS_ENABLED: 'True' + INVENTREE_PLUGINS_ENABLED: 'True', + INVENTREE_ADMIN_URL: 'test-admin' }, url: 'http://127.0.0.1:8000/api/', reuseExistingServer: !process.env.CI, 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 { diff --git a/src/frontend/tests/pui_general.spec.ts b/src/frontend/tests/pui_general.spec.ts index bd5e6a7e48ff..370947010201 100644 --- a/src/frontend/tests/pui_general.spec.ts +++ b/src/frontend/tests/pui_general.spec.ts @@ -110,3 +110,18 @@ test('Company', async ({ page }) => { await page.getByText('Enter a valid URL.').waitFor(); await page.getByRole('button', { name: 'Cancel' }).click(); }); + +/** + * Test for integration of django admin button + */ +test('Admin Button', async ({ page }) => { + await doQuickLogin(page, 'admin', 'inventree'); + await page.goto(`${baseUrl}/company/1/details`); + + // Click on the admin button + await page.getByLabel(/action-button-open-in-admin/).click(); + + await page.waitForURL('**/test-admin/company/company/1/change/**'); + await page.getByRole('heading', { name: 'Change Company' }).waitFor(); + await page.getByRole('link', { name: 'View on site' }).waitFor(); +});