2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-10 01:08:49 +00:00

Setting filter (#9076)

* Expose "model_filters" to settings API

* Enable settings filters in the front-end

* Fix return types

* Bump API version
This commit is contained in:
Oliver 2025-02-14 19:15:42 +11:00 committed by GitHub
parent cca1912220
commit 944a4f28be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 4 deletions

View File

@ -1,13 +1,16 @@
"""InvenTree API version information."""
# InvenTree API version
INVENTREE_API_VERSION = 310
INVENTREE_API_VERSION = 311
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
INVENTREE_API_TEXT = """
v311 - 2025-02-14 : https://github.com/inventree/InvenTree/pull/9076
- Adds "model_filters" attribute to settings API
v310 - 2025-02-14 : https://github.com/inventree/InvenTree/pull/9077
- Adds 'is_variant' filter to the Part list API

View File

@ -893,7 +893,7 @@ class BaseInvenTreeSetting(models.Model):
"""Check if this setting references a model instance in the database."""
return self.model_name() is not None
def model_name(self):
def model_name(self) -> str:
"""Return the model name associated with this setting."""
setting = self.get_setting_definition(
self.key, **self.get_filters_for_instance()
@ -901,6 +901,19 @@ class BaseInvenTreeSetting(models.Model):
return setting.get('model', None)
def model_filters(self) -> dict:
"""Return the model filters associated with this setting."""
setting = self.get_setting_definition(
self.key, **self.get_filters_for_instance()
)
filters = setting.get('model_filters', None)
if filters is not None and type(filters) is not dict:
filters = None
return filters
def model_class(self):
"""Return the model class associated with this setting.

View File

@ -71,6 +71,8 @@ class SettingsSerializer(InvenTreeModelSerializer):
model_name = serializers.CharField(read_only=True)
model_filters = serializers.DictField(read_only=True)
api_url = serializers.CharField(read_only=True)
value = SettingsValueField(allow_null=True)
@ -185,6 +187,7 @@ class GenericReferencedSettingSerializer(SettingsSerializer):
'type',
'choices',
'model_name',
'model_filters',
'api_url',
'typ',
'required',

View File

@ -62,9 +62,11 @@ export function SettingList({
return 'string';
}, [setting]);
const key: string = useMemo(() => setting?.key ?? '', [setting]);
const editSettingModal = useEditApiFormModal({
url: settingsState.endpoint,
pk: setting?.key,
pk: key,
pathParams: settingsState.pathParams,
title: t`Edit Setting`,
fields: {
@ -75,10 +77,11 @@ export function SettingList({
description: setting?.description,
api_url: setting?.api_url ?? '',
model: (setting?.model_name?.split('.')[1] as ModelType) ?? null,
filters: setting?.model_filters || undefined,
choices: setting?.choices ?? undefined
}
},
successMessage: t`Setting ${setting?.key} updated successfully`,
successMessage: t`Setting ${key} updated successfully`,
onFormSuccess: () => {
settingsState.fetchSettings();
onChange?.();

View File

@ -85,6 +85,7 @@ export interface Setting {
units: string;
choices: SettingChoice[];
model_name: string | null;
model_filters: Record<string, any> | null;
api_url: string | null;
typ: SettingTyp;
plugin?: string;