mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
refactor: remove preference-view (#8894)
* remove preference-view * bump api --------- Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
parent
fbe2131fa5
commit
89dfa0f6f9
@ -1,13 +1,16 @@
|
|||||||
"""InvenTree API version information."""
|
"""InvenTree API version information."""
|
||||||
|
|
||||||
# InvenTree API version
|
# InvenTree API version
|
||||||
INVENTREE_API_VERSION = 300
|
INVENTREE_API_VERSION = 301
|
||||||
|
|
||||||
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
|
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
|
||||||
|
|
||||||
|
|
||||||
INVENTREE_API_TEXT = """
|
INVENTREE_API_TEXT = """
|
||||||
|
|
||||||
|
v301 - 2025-01-14 - https://github.com/inventree/InvenTree/pull/8894
|
||||||
|
- Remove ui preferences from the API
|
||||||
|
|
||||||
v300 - 2025-01-13 - https://github.com/inventree/InvenTree/pull/8886
|
v300 - 2025-01-13 - https://github.com/inventree/InvenTree/pull/8886
|
||||||
- Allow null value for 'expiry_date' field introduced in #8867
|
- Allow null value for 'expiry_date' field introduced in #8867
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ import stock.api
|
|||||||
import users.api
|
import users.api
|
||||||
from InvenTree.auth_override_views import CustomRegisterView
|
from InvenTree.auth_override_views import CustomRegisterView
|
||||||
from plugin.urls import get_plugin_urls
|
from plugin.urls import get_plugin_urls
|
||||||
from web.urls import api_urls as web_api_urls
|
|
||||||
from web.urls import urlpatterns as platform_urls
|
from web.urls import urlpatterns as platform_urls
|
||||||
|
|
||||||
from .api import (
|
from .api import (
|
||||||
@ -87,7 +86,6 @@ apipatterns = [
|
|||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
path('user/', include(users.api.user_urls)),
|
path('user/', include(users.api.user_urls)),
|
||||||
path('web/', include(web_api_urls)),
|
|
||||||
# Plugin endpoints
|
# Plugin endpoints
|
||||||
path('', include(plugin.api.plugin_api_urls)),
|
path('', include(plugin.api.plugin_api_urls)),
|
||||||
# Common endpoints endpoint
|
# Common endpoints endpoint
|
||||||
|
@ -391,10 +391,7 @@ class LoginRedirect(RedirectView):
|
|||||||
|
|
||||||
def get_redirect_url(self, *args, **kwargs):
|
def get_redirect_url(self, *args, **kwargs):
|
||||||
"""Return the URL to redirect to."""
|
"""Return the URL to redirect to."""
|
||||||
session = self.request.session
|
|
||||||
if session.get('preferred_method', 'cui') == 'pui':
|
|
||||||
return f'/{FRONTEND_URL_BASE}/logged-in/'
|
return f'/{FRONTEND_URL_BASE}/logged-in/'
|
||||||
return '/index/'
|
|
||||||
|
|
||||||
|
|
||||||
user_urls = [
|
user_urls = [
|
||||||
|
@ -95,11 +95,6 @@ class UserAPITests(InvenTreeAPITestCase):
|
|||||||
def test_login_redirect(self):
|
def test_login_redirect(self):
|
||||||
"""Test login redirect endpoint."""
|
"""Test login redirect endpoint."""
|
||||||
response = self.get(reverse('api-login-redirect'), expected_code=302)
|
response = self.get(reverse('api-login-redirect'), expected_code=302)
|
||||||
self.assertEqual(response.url, '/index/')
|
|
||||||
|
|
||||||
# PUI
|
|
||||||
self.put(reverse('api-ui-preference'), {'preferred_method': 'pui'})
|
|
||||||
response = self.get(reverse('api-login-redirect'), expected_code=302)
|
|
||||||
self.assertEqual(response.url, '/platform/logged-in/')
|
self.assertEqual(response.url, '/platform/logged-in/')
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,10 +5,8 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from django.urls import reverse
|
|
||||||
|
|
||||||
from InvenTree.config import get_frontend_settings
|
from InvenTree.config import get_frontend_settings
|
||||||
from InvenTree.unit_test import InvenTreeAPITestCase, InvenTreeTestCase
|
from InvenTree.unit_test import InvenTreeTestCase
|
||||||
|
|
||||||
from .templatetags import spa_helper
|
from .templatetags import spa_helper
|
||||||
|
|
||||||
@ -92,26 +90,3 @@ class TemplateTagTest(InvenTreeTestCase):
|
|||||||
"""Test the redirect helper."""
|
"""Test the redirect helper."""
|
||||||
response = self.client.get('/assets/testpath')
|
response = self.client.get('/assets/testpath')
|
||||||
self.assertEqual(response.url, '/static/web/assets/testpath')
|
self.assertEqual(response.url, '/static/web/assets/testpath')
|
||||||
|
|
||||||
|
|
||||||
class TestWebHelpers(InvenTreeAPITestCase):
|
|
||||||
"""Tests for the web helpers."""
|
|
||||||
|
|
||||||
def test_ui_preference(self):
|
|
||||||
"""Test the UI preference API."""
|
|
||||||
url = reverse('api-ui-preference')
|
|
||||||
|
|
||||||
# Test default
|
|
||||||
resp = self.get(url)
|
|
||||||
data = json.loads(resp.content)
|
|
||||||
self.assertTrue(data['cui'])
|
|
||||||
self.assertFalse(data['pui'])
|
|
||||||
self.assertEqual(data['preferred_method'], 'cui')
|
|
||||||
|
|
||||||
# Set to PUI
|
|
||||||
resp = self.put(url, {'preferred_method': 'pui'})
|
|
||||||
data = json.loads(resp.content)
|
|
||||||
self.assertEqual(resp.status_code, 200)
|
|
||||||
self.assertFalse(data['cui'])
|
|
||||||
self.assertTrue(data['pui'])
|
|
||||||
self.assertEqual(data['preferred_method'], 'pui')
|
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
"""URLs for web app."""
|
"""URLs for web app."""
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import JsonResponse
|
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.urls import include, path, re_path
|
from django.urls import include, path, re_path
|
||||||
from django.views.decorators.csrf import ensure_csrf_cookie
|
from django.views.decorators.csrf import ensure_csrf_cookie
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from rest_framework import permissions, serializers
|
|
||||||
|
|
||||||
from InvenTree.mixins import RetrieveUpdateAPI
|
|
||||||
|
|
||||||
|
|
||||||
class RedirectAssetView(TemplateView):
|
class RedirectAssetView(TemplateView):
|
||||||
"""View to redirect to static asset."""
|
"""View to redirect to static asset."""
|
||||||
@ -22,55 +17,6 @@ class RedirectAssetView(TemplateView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class PreferredSerializer(serializers.Serializer):
|
|
||||||
"""Serializer for the preferred serializer session setting."""
|
|
||||||
|
|
||||||
preferred_method = serializers.ChoiceField(choices=['cui', 'pui'])
|
|
||||||
pui = serializers.SerializerMethodField(read_only=True)
|
|
||||||
cui = serializers.SerializerMethodField(read_only=True)
|
|
||||||
|
|
||||||
def get_pui(self, obj) -> bool:
|
|
||||||
"""Return true if preferred method is PUI."""
|
|
||||||
return obj['preferred_method'] == 'pui'
|
|
||||||
|
|
||||||
def get_cui(self, obj) -> bool:
|
|
||||||
"""Return true if preferred method is CUI."""
|
|
||||||
return obj['preferred_method'] == 'cui'
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
"""Meta class for PreferedSerializer."""
|
|
||||||
|
|
||||||
fields = '__all__'
|
|
||||||
|
|
||||||
|
|
||||||
class PreferredUiView(RetrieveUpdateAPI):
|
|
||||||
"""Set preferred UI (CIU/PUI)."""
|
|
||||||
|
|
||||||
permission_classes = [permissions.AllowAny]
|
|
||||||
serializer_class = PreferredSerializer
|
|
||||||
http_method_names = ['get', 'post', 'put', 'head', 'options']
|
|
||||||
|
|
||||||
def retrieve(self, request, *args, **kwargs):
|
|
||||||
"""Retrieve the preferred UI method."""
|
|
||||||
session = self.request.session
|
|
||||||
session['preferred_method'] = session.get('preferred_method', 'cui')
|
|
||||||
serializer = self.get_serializer(data=dict(session))
|
|
||||||
serializer.is_valid(raise_exception=True)
|
|
||||||
return JsonResponse(serializer.data)
|
|
||||||
|
|
||||||
def update(self, request, *args, **kwargs):
|
|
||||||
"""Update the preferred UI method."""
|
|
||||||
serializer = self.get_serializer(data=self.clean_data(request.data))
|
|
||||||
serializer.is_valid(raise_exception=True)
|
|
||||||
|
|
||||||
# Run update
|
|
||||||
session = self.request.session
|
|
||||||
session['preferred_method'] = serializer.validated_data['preferred_method']
|
|
||||||
session.modified = True
|
|
||||||
|
|
||||||
return JsonResponse(serializer.data)
|
|
||||||
|
|
||||||
|
|
||||||
spa_view = ensure_csrf_cookie(TemplateView.as_view(template_name='web/index.html'))
|
spa_view = ensure_csrf_cookie(TemplateView.as_view(template_name='web/index.html'))
|
||||||
assets_path = path('assets/<path:path>', RedirectAssetView.as_view())
|
assets_path = path('assets/<path:path>', RedirectAssetView.as_view())
|
||||||
|
|
||||||
@ -91,8 +37,3 @@ urlpatterns = [
|
|||||||
assets_path,
|
assets_path,
|
||||||
path(settings.FRONTEND_URL_BASE, spa_view, name='platform'),
|
path(settings.FRONTEND_URL_BASE, spa_view, name='platform'),
|
||||||
]
|
]
|
||||||
|
|
||||||
api_urls = [
|
|
||||||
# UI Preference
|
|
||||||
path('ui_preference/', PreferredUiView.as_view(), name='api-ui-preference')
|
|
||||||
]
|
|
||||||
|
@ -15,10 +15,6 @@ import {
|
|||||||
} from '@tabler/icons-react';
|
} from '@tabler/icons-react';
|
||||||
|
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { showNotification } from '@mantine/notifications';
|
|
||||||
import { api } from '../../App';
|
|
||||||
import { ApiEndpoints } from '../../enums/ApiEndpoints';
|
|
||||||
import { apiUrl } from '../../states/ApiState';
|
|
||||||
import type { Provider } from '../../states/states';
|
import type { Provider } from '../../states/states';
|
||||||
|
|
||||||
const brandIcons: { [key: string]: JSX.Element } = {
|
const brandIcons: { [key: string]: JSX.Element } = {
|
||||||
@ -37,24 +33,7 @@ const brandIcons: { [key: string]: JSX.Element } = {
|
|||||||
|
|
||||||
export function SsoButton({ provider }: Readonly<{ provider: Provider }>) {
|
export function SsoButton({ provider }: Readonly<{ provider: Provider }>) {
|
||||||
function login() {
|
function login() {
|
||||||
// set preferred provider
|
|
||||||
api
|
|
||||||
.put(
|
|
||||||
apiUrl(ApiEndpoints.ui_preference),
|
|
||||||
{ preferred_method: 'pui' },
|
|
||||||
{ headers: { Authorization: '' } }
|
|
||||||
)
|
|
||||||
.then(() => {
|
|
||||||
// redirect to login
|
|
||||||
window.location.href = provider.login;
|
window.location.href = provider.login;
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
showNotification({
|
|
||||||
title: t`Error`,
|
|
||||||
message: t`Sign in redirect failed.`,
|
|
||||||
color: 'red'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -221,6 +221,5 @@ export enum ApiEndpoints {
|
|||||||
error_report_list = 'error-report/',
|
error_report_list = 'error-report/',
|
||||||
project_code_list = 'project-code/',
|
project_code_list = 'project-code/',
|
||||||
custom_unit_list = 'units/',
|
custom_unit_list = 'units/',
|
||||||
ui_preference = 'web/ui_preference/',
|
|
||||||
notes_image_upload = 'notes-image-upload/'
|
notes_image_upload = 'notes-image-upload/'
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user