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

[P_UI] Added django settings for p_ui #5343

Merged
merged 14 commits into from
Aug 10, 2023
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
4 changes: 4 additions & 0 deletions InvenTree/InvenTree/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,10 @@
CUSTOM_SPLASH = get_custom_file('INVENTREE_CUSTOM_SPLASH', 'customize.splash', 'custom splash')

CUSTOMIZE = get_setting('INVENTREE_CUSTOMIZE', 'customize', {})

# Frontend settings
PUI_SETTINGS = get_setting("INVENTREE_PUI_SETTINGS", "pui_settings", {})

if DEBUG:
logger.info("InvenTree running with DEBUG enabled")

Expand Down
9 changes: 9 additions & 0 deletions InvenTree/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ remote_login_header: HTTP_REMOTE_USER
# hide_admin_link: true
# hide_password_reset: true

# Platform UI options
# pui_settings:
# server_list:
# my_server1:
# host: https://demo.inventree.org/api/
# name: InvenTree Demo
matmair marked this conversation as resolved.
Show resolved Hide resolved
# default_server: my_server1
# show_server_selector: false

# Custom flags
# InvenTree uses django-flags; read more in their docs at https://cfpb.github.io/django-flags/conditions/
# Use environment variable INVENTREE_FLAGS or the settings below
Expand Down
1 change: 1 addition & 0 deletions InvenTree/web/templates/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<body>
<div id="root"></div>
{% spa_settings %}
{% spa_bundle %}
</body>

Expand Down
10 changes: 9 additions & 1 deletion InvenTree/web/templatetags/spa_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from django.conf import settings
from django.utils.safestring import mark_safe

logger = getLogger("gwaesser_backend")
logger = getLogger("InvenTree")
register = template.Library()

PUI_SETTINGS = json.dumps(settings.PUI_SETTINGS)


@register.simple_tag
def spa_bundle():
Expand All @@ -34,3 +36,9 @@ def spa_bundle():
return mark_safe(
f"""<script type="module" src="{settings.STATIC_URL}web/{index['file']}"></script>{imports_files}"""
)


@register.simple_tag
def spa_settings():
"""Render settings for spa."""
return mark_safe(f"""<script>window.INVENTREE_SETTINGS={PUI_SETTINGS}</script>""")
5 changes: 4 additions & 1 deletion src/frontend/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
# https://www.netlify.com/docs/netlify-toml-reference/

[build]
command = "yarn run build --outDir dist"
command = "yarn run extract && yarn run compile && yarn run build --outDir dist"
publish = "dist"

[build.environment]
VITE_DEMO = "true"

# Send requests to subpath

[[redirects]]
Expand Down
8 changes: 5 additions & 3 deletions src/frontend/src/components/forms/AuthFormOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export function AuthFormOptions({
<Group>
<ColorToggle />
<LanguageToggle />
<Tooltip label={hostname}>
<IconServer onClick={toggleHostEdit} />
</Tooltip>
{window.INVENTREE_SETTINGS.show_server_selector && (
<Tooltip label={hostname}>
<IconServer onClick={toggleHostEdit} />
</Tooltip>
)}
<Text c={'dimmed'}>
{server.version} | {server.apiVersion}
</Text>
Expand Down
17 changes: 2 additions & 15 deletions src/frontend/src/defaults/defaultHostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,5 @@ import { t } from '@lingui/macro';

import { HostList } from '../states/states';

export const defaultHostList: HostList = {
'mantine-u56l5jt85': {
host: 'https://demo.inventree.org/api/',
name: t`InvenTree Demo`
},
'mantine-g8t1zrj50': {
host: 'https://sample.app.invenhost.com/api/',
name: 'InvenHost: Sample'
},
'mantine-cqj63coxn': {
host: 'http://localhost:8000/api/',
name: t`Local Server`
}
};
export const defaultHostKey = 'mantine-cqj63coxn';
export const defaultHostList: HostList = window.INVENTREE_SETTINGS.server_list;
export const defaultHostKey = window.INVENTREE_SETTINGS.default_server;
38 changes: 38 additions & 0 deletions src/frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@ import React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App';
import { HostList } from './states/states';

// define settings
declare global {
interface Window {
INVENTREE_SETTINGS: {
server_list: HostList;
default_server: string;
show_server_selector: boolean;
};
}
}

export const IS_DEV = import.meta.env.DEV;
export const IS_DEMO = import.meta.env.VITE_DEMO === 'true';
export const IS_DEV_OR_DEMO = IS_DEV || IS_DEMO;

window.INVENTREE_SETTINGS = {
server_list: {
'mantine-cqj63coxn': {
host: `${window.location.origin}/api/`,
name: 'Current Server'
},
...(IS_DEV_OR_DEMO
? {
'mantine-u56l5jt85': {
host: 'https://demo.inventree.org/api/',
name: 'InvenTree Demo'
}
}
: {})
},
default_server: IS_DEMO ? 'mantine-u56l5jt85' : 'mantine-cqj63coxn', // use demo server for demo mode
show_server_selector: IS_DEV_OR_DEMO,

// merge in settings that are already set via django's spa_view or for development
...((window.INVENTREE_SETTINGS || {}) as any)
};

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/pages/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Center, Container } from '@mantine/core';
import { useToggle } from '@mantine/hooks';
import { useEffect } from 'react';

import { setApiDefaults } from '../../App';
import { AuthFormOptions } from '../../components/forms/AuthFormOptions';
import { AuthenticationForm } from '../../components/forms/AuthenticationForm';
import { InstanceOptions } from '../../components/forms/InstanceOptions';
Expand All @@ -27,6 +28,7 @@ export default function Login() {
// Data manipulation functions
function ChangeHost(newHost: string): void {
setHost(hostList[newHost].host, newHost);
setApiDefaults();
fetchServerApiState();
}

Expand Down
8 changes: 8 additions & 0 deletions src/frontend/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_DEMO: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}
14 changes: 13 additions & 1 deletion src/frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import react from '@vitejs/plugin-react';
import { platform } from 'node:os';
import { defineConfig, splitVendorChunkPlugin } from 'vite';

const IS_IN_WSL = platform().includes('WSL');

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
Expand All @@ -16,8 +19,17 @@ export default defineConfig({
outDir: '../../InvenTree/web/static/web'
},
server: {
proxy: {
'/api': {
target: 'http://localhost:8000',
changeOrigin: true,
secure: true
matmair marked this conversation as resolved.
Show resolved Hide resolved
}
},
watch: {
usePolling: true
// use polling only for WSL as the file system doesn't trigger notifications for Linux apps
// ref: https://github.com/vitejs/vite/issues/1153#issuecomment-785467271
usePolling: IS_IN_WSL
}
}
});