mirror of
https://github.com/inventree/InvenTree.git
synced 2025-12-18 02:08:22 +00:00
API updates
- Fix options for model_type - Add API filters
This commit is contained in:
@@ -772,7 +772,7 @@ class ParameterTemplateFilter(FilterSet):
|
|||||||
"""Metaclass options."""
|
"""Metaclass options."""
|
||||||
|
|
||||||
model = common.models.ParameterTemplate
|
model = common.models.ParameterTemplate
|
||||||
fields = ['name', 'units', 'checkbox']
|
fields = ['model_type', 'name', 'units', 'checkbox']
|
||||||
|
|
||||||
has_choices = rest_filters.BooleanFilter(
|
has_choices = rest_filters.BooleanFilter(
|
||||||
method='filter_has_choices', label='Has Choice'
|
method='filter_has_choices', label='Has Choice'
|
||||||
@@ -794,6 +794,18 @@ class ParameterTemplateFilter(FilterSet):
|
|||||||
|
|
||||||
return queryset.filter(Q(units=None) | Q(units='')).distinct()
|
return queryset.filter(Q(units=None) | Q(units='')).distinct()
|
||||||
|
|
||||||
|
for_model = rest_filters.CharFilter(method='filter_for_model', label='For Model')
|
||||||
|
|
||||||
|
def filter_for_model(self, queryset, name, value):
|
||||||
|
"""Filter queryset to include only ParameterTemplates which apply to the given model.
|
||||||
|
|
||||||
|
Note that this varies from the 'model_type' filter, in that ParameterTemplates
|
||||||
|
with a blank 'model_type' are considered to apply to all models.
|
||||||
|
"""
|
||||||
|
return queryset.filter(
|
||||||
|
Q(model_type__iexact=value) | Q(model_type__isnull=True) | Q(model_type='')
|
||||||
|
).distinct()
|
||||||
|
|
||||||
|
|
||||||
class ParameterTemplateMixin:
|
class ParameterTemplateMixin:
|
||||||
"""Mixin class for ParameterTemplate views."""
|
"""Mixin class for ParameterTemplate views."""
|
||||||
|
|||||||
@@ -72,21 +72,14 @@ class RenderMeta(enums.ChoicesMeta):
|
|||||||
"""Metaclass for rendering choices."""
|
"""Metaclass for rendering choices."""
|
||||||
|
|
||||||
choice_fnc = None
|
choice_fnc = None
|
||||||
allow_blank: bool = False
|
|
||||||
blank_label: str = '------'
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def choices(self):
|
def choices(self):
|
||||||
"""Return a list of choices for the enum class."""
|
"""Return a list of choices for the enum class."""
|
||||||
fnc = getattr(self, 'choice_fnc', None)
|
fnc = getattr(self, 'choice_fnc', None)
|
||||||
if fnc:
|
if fnc:
|
||||||
options = fnc()
|
return fnc()
|
||||||
options = []
|
return []
|
||||||
|
|
||||||
if self.allow_blank:
|
|
||||||
options.insert(0, ('', self.blank_label))
|
|
||||||
|
|
||||||
return options
|
|
||||||
|
|
||||||
|
|
||||||
class RenderChoices(models.TextChoices, metaclass=RenderMeta): # type: ignore
|
class RenderChoices(models.TextChoices, metaclass=RenderMeta): # type: ignore
|
||||||
@@ -2395,8 +2388,7 @@ class ParameterTemplate(
|
|||||||
class ModelChoices(RenderChoices):
|
class ModelChoices(RenderChoices):
|
||||||
"""Model choices for parameter templates."""
|
"""Model choices for parameter templates."""
|
||||||
|
|
||||||
allow_blank = True
|
choice_fnc = common.validators.parameter_template_model_options
|
||||||
choice_fnc = common.validators.parameter_model_options
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_api_url() -> str:
|
def get_api_url() -> str:
|
||||||
|
|||||||
@@ -724,13 +724,13 @@ class ParameterTemplateSerializer(
|
|||||||
if len(self.fields['model_type'].choices) == 0:
|
if len(self.fields['model_type'].choices) == 0:
|
||||||
self.fields[
|
self.fields[
|
||||||
'model_type'
|
'model_type'
|
||||||
].choices = common.validators.attachment_model_options()
|
].choices = common.validators.parameter_template_model_options()
|
||||||
|
|
||||||
# Note: The choices are overridden at run-time on class initialization
|
# Note: The choices are overridden at run-time on class initialization
|
||||||
model_type = serializers.ChoiceField(
|
model_type = serializers.ChoiceField(
|
||||||
label=_('Model Type'),
|
label=_('Model Type'),
|
||||||
default='',
|
default='',
|
||||||
choices=common.validators.attachment_model_options(),
|
choices=common.validators.parameter_template_model_options(),
|
||||||
required=False,
|
required=False,
|
||||||
allow_blank=True,
|
allow_blank=True,
|
||||||
allow_null=True,
|
allow_null=True,
|
||||||
|
|||||||
@@ -29,6 +29,14 @@ def parameter_model_options():
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def parameter_template_model_options():
|
||||||
|
"""Return a list of options for models which support parameter templates.
|
||||||
|
|
||||||
|
Note: This includes a blank option at the start, since ParameterTemplate.model_type may be blank.
|
||||||
|
"""
|
||||||
|
return [('', _('Any model type')), *parameter_model_options()]
|
||||||
|
|
||||||
|
|
||||||
def validate_parameter_model_type(value: str):
|
def validate_parameter_model_type(value: str):
|
||||||
"""Ensure that the provided parameter model is valid."""
|
"""Ensure that the provided parameter model is valid."""
|
||||||
model_names = [el[0] for el in parameter_model_options()]
|
model_names = [el[0] for el in parameter_model_options()]
|
||||||
|
|||||||
@@ -410,6 +410,7 @@ class PartReportContext(report.mixins.BaseReportContext):
|
|||||||
@cleanup.ignore
|
@cleanup.ignore
|
||||||
class Part(
|
class Part(
|
||||||
InvenTree.models.PluginValidationMixin,
|
InvenTree.models.PluginValidationMixin,
|
||||||
|
InvenTree.models.InvenTreeParameterMixin,
|
||||||
InvenTree.models.InvenTreeAttachmentMixin,
|
InvenTree.models.InvenTreeAttachmentMixin,
|
||||||
InvenTree.models.InvenTreeBarcodeMixin,
|
InvenTree.models.InvenTreeBarcodeMixin,
|
||||||
InvenTree.models.InvenTreeNotesMixin,
|
InvenTree.models.InvenTreeNotesMixin,
|
||||||
|
|||||||
Reference in New Issue
Block a user