Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PUI] Updates for AdminButton #8434

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/backend/InvenTree/InvenTree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions src/frontend/src/components/buttons/AdminButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,17 +25,21 @@ export type AdminButtonProps = {
*/
export default function AdminButton(props: Readonly<AdminButtonProps>) {
const user = useUserState();
const server = useServerApiState();

const enabled: boolean = useMemo(() => {
// Only users with superuser permission will see this button
if (!user || !user.isLoggedIn() || !user.isSuperuser()) {
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;
Expand All @@ -57,8 +62,8 @@ export default function AdminButton(props: Readonly<AdminButtonProps>) {
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
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/defaults/defaults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/states/states.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ServerAPIProps {
installer: null | string;
target: null | string;
default_locale: null | string;
django_admin: null | string;
}

export interface AuthProps {
Expand Down
15 changes: 15 additions & 0 deletions src/frontend/tests/pui_general.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Loading