2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-10-25 10:27:39 +00:00
This commit is contained in:
Matthias Mair
2025-10-05 21:43:19 +02:00
parent 47892e409e
commit 30dc02605e
2 changed files with 68 additions and 47 deletions

View File

@@ -34,7 +34,6 @@ import users.models
from importer.registry import register_importer from importer.registry import register_importer
from InvenTree.mixins import DataImportExportSerializerMixin from InvenTree.mixins import DataImportExportSerializerMixin
from InvenTree.ready import isGeneratingSchema from InvenTree.ready import isGeneratingSchema
from InvenTree.serializers import can_filter
from users.serializers import UserSerializer from users.serializers import UserSerializer
from .models import ( from .models import (
@@ -92,7 +91,6 @@ class CategorySerializer(
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# TODO INVE-T1 support complex filters
if not path_detail and not isGeneratingSchema(): if not path_detail and not isGeneratingSchema():
self.fields.pop('path', None) self.fields.pop('path', None)
@@ -357,7 +355,6 @@ class PartBriefSerializer(InvenTree.serializers.InvenTreeModelSerializer):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# TODO INVE-T1 support complex filters
if not pricing and not isGeneratingSchema(): if not pricing and not isGeneratingSchema():
self.fields.pop('pricing_min', None) self.fields.pop('pricing_min', None)
self.fields.pop('pricing_max', None) self.fields.pop('pricing_max', None)
@@ -417,6 +414,25 @@ class PartParameterSerializer(
read_only_fields = ['updated', 'updated_by'] read_only_fields = ['updated', 'updated_by']
def __init__(self, *args, **kwargs):
"""Custom initialization method for the serializer.
Allows us to optionally include or exclude particular information
"""
template_detail = kwargs.pop('template_detail', True)
part_detail = kwargs.pop('part_detail', False)
super().__init__(*args, **kwargs)
if isGeneratingSchema():
return
if not part_detail:
self.fields.pop('part_detail', None)
if not template_detail:
self.fields.pop('template_detail', None)
def save(self): def save(self):
"""Save the PartParameter instance.""" """Save the PartParameter instance."""
instance = super().save() instance = super().save()
@@ -428,15 +444,12 @@ class PartParameterSerializer(
return instance return instance
part_detail = can_filter( part_detail = PartBriefSerializer(
PartBriefSerializer(source='part', many=False, read_only=True, allow_null=True) source='part', many=False, read_only=True, allow_null=True
) )
template_detail = can_filter( template_detail = PartParameterTemplateSerializer(
PartParameterTemplateSerializer(
source='template', many=False, read_only=True, allow_null=True source='template', many=False, read_only=True, allow_null=True
),
True,
) )
updated_by_detail = UserSerializer( updated_by_detail = UserSerializer(
@@ -724,7 +737,9 @@ class PartSerializer(
- Allows us to optionally pass extra fields based on the query. - Allows us to optionally pass extra fields based on the query.
""" """
self.starred_parts = kwargs.pop('starred_parts', []) self.starred_parts = kwargs.pop('starred_parts', [])
category_detail = kwargs.pop('category_detail', False)
location_detail = kwargs.pop('location_detail', False) location_detail = kwargs.pop('location_detail', False)
parameters = kwargs.pop('parameters', False)
create = kwargs.pop('create', False) create = kwargs.pop('create', False)
pricing = kwargs.pop('pricing', True) pricing = kwargs.pop('pricing', True)
path_detail = kwargs.pop('path_detail', False) path_detail = kwargs.pop('path_detail', False)
@@ -734,11 +749,15 @@ class PartSerializer(
if isGeneratingSchema(): if isGeneratingSchema():
return return
# TODO INVE-T1 support complex filters if not category_detail:
self.fields.pop('category_detail', None)
if not location_detail: if not location_detail:
self.fields.pop('default_location_detail', None) self.fields.pop('default_location_detail', None)
# TODO INVE-T1 support complex filters if not parameters:
self.fields.pop('parameters', None)
if not path_detail: if not path_detail:
self.fields.pop('category_path', None) self.fields.pop('category_path', None)
@@ -750,7 +769,6 @@ class PartSerializer(
continue continue
self.fields.pop(f, None) self.fields.pop(f, None)
# TODO INVE-T1 support complex filters
if not pricing: if not pricing:
self.fields.pop('pricing_min', None) self.fields.pop('pricing_min', None)
self.fields.pop('pricing_max', None) self.fields.pop('pricing_max', None)
@@ -870,11 +888,9 @@ class PartSerializer(
return part in self.starred_parts return part in self.starred_parts
# Extra detail for the category # Extra detail for the category
category_detail = can_filter( category_detail = CategorySerializer(
CategorySerializer(
source='category', many=False, read_only=True, allow_null=True source='category', many=False, read_only=True, allow_null=True
) )
)
category_path = serializers.ListField( category_path = serializers.ListField(
child=serializers.DictField(), child=serializers.DictField(),
@@ -1002,9 +1018,7 @@ class PartSerializer(
source='pricing_data.updated', allow_null=True, read_only=True source='pricing_data.updated', allow_null=True, read_only=True
) )
parameters = can_filter( parameters = PartParameterSerializer(many=True, read_only=True, allow_null=True)
PartParameterSerializer(many=True, read_only=True, allow_null=True)
)
# Extra fields used only for creation of a new Part instance # Extra fields used only for creation of a new Part instance
duplicate = DuplicatePartSerializer( duplicate = DuplicatePartSerializer(
@@ -1635,14 +1649,29 @@ class BomItemSerializer(
- part_detail and sub_part_detail serializers are only included if requested. - part_detail and sub_part_detail serializers are only included if requested.
- This saves a bunch of database requests - This saves a bunch of database requests
""" """
can_build = kwargs.pop('can_build', True)
part_detail = kwargs.pop('part_detail', False)
sub_part_detail = kwargs.pop('sub_part_detail', True)
pricing = kwargs.pop('pricing', True) pricing = kwargs.pop('pricing', True)
substitutes = kwargs.pop('substitutes', True)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if isGeneratingSchema(): if isGeneratingSchema():
return return
# TODO INVE-T1 support complex filters if not part_detail:
self.fields.pop('part_detail', None)
if not sub_part_detail:
self.fields.pop('sub_part_detail', None)
if not can_build:
self.fields.pop('can_build')
if not substitutes:
self.fields.pop('substitutes', None)
if not pricing: if not pricing:
self.fields.pop('pricing_min', None) self.fields.pop('pricing_min', None)
self.fields.pop('pricing_max', None) self.fields.pop('pricing_max', None)
@@ -1673,18 +1702,12 @@ class BomItemSerializer(
help_text=_('Select the parent assembly'), help_text=_('Select the parent assembly'),
) )
substitutes = can_filter( substitutes = BomItemSubstituteSerializer(
BomItemSubstituteSerializer(many=True, read_only=True, allow_null=True), True many=True, read_only=True, allow_null=True
) )
part_detail = can_filter( part_detail = PartBriefSerializer(
PartBriefSerializer( source='part', label=_('Assembly'), many=False, read_only=True, allow_null=True
source='part',
label=_('Assembly'),
many=False,
read_only=True,
allow_null=True,
)
) )
sub_part = serializers.PrimaryKeyRelatedField( sub_part = serializers.PrimaryKeyRelatedField(
@@ -1693,15 +1716,12 @@ class BomItemSerializer(
help_text=_('Select the component part'), help_text=_('Select the component part'),
) )
sub_part_detail = can_filter( sub_part_detail = PartBriefSerializer(
PartBriefSerializer(
source='sub_part', source='sub_part',
label=_('Component'), label=_('Component'),
many=False, many=False,
read_only=True, read_only=True,
allow_null=True, allow_null=True,
),
True,
) )
on_order = serializers.FloatField( on_order = serializers.FloatField(
@@ -1712,9 +1732,8 @@ class BomItemSerializer(
label=_('In Production'), read_only=True, allow_null=True label=_('In Production'), read_only=True, allow_null=True
) )
can_build = can_filter( can_build = serializers.FloatField(
serializers.FloatField(label=_('Can Build'), read_only=True, allow_null=True), label=_('Can Build'), read_only=True, allow_null=True
True,
) )
# Cached pricing fields # Cached pricing fields

View File

@@ -406,12 +406,16 @@ class StockItemSerializer(
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Add detail fields.""" """Add detail fields."""
path_detail = kwargs.pop('path_detail', False) path_detail = kwargs.pop('path_detail', False)
tests = kwargs.pop('tests', False)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if isGeneratingSchema(): if isGeneratingSchema():
return return
if not tests:
self.fields.pop('tests', None)
# TODO INVE-T1 support complex filters # TODO INVE-T1 support complex filters
if not path_detail: if not path_detail:
self.fields.pop('location_path', None) self.fields.pop('location_path', None)
@@ -620,11 +624,9 @@ class StockItemSerializer(
True, True,
) )
tests = can_filter( tests = StockItemTestResultSerializer(
StockItemTestResultSerializer(
source='test_results', many=True, read_only=True, allow_null=True source='test_results', many=True, read_only=True, allow_null=True
) )
)
quantity = InvenTreeDecimalField() quantity = InvenTreeDecimalField()