diff --git a/src/backend/InvenTree/part/serializers.py b/src/backend/InvenTree/part/serializers.py index 9ceab29c2d..1402267fa3 100644 --- a/src/backend/InvenTree/part/serializers.py +++ b/src/backend/InvenTree/part/serializers.py @@ -34,7 +34,6 @@ import users.models from importer.registry import register_importer from InvenTree.mixins import DataImportExportSerializerMixin from InvenTree.ready import isGeneratingSchema -from InvenTree.serializers import can_filter from users.serializers import UserSerializer from .models import ( @@ -92,7 +91,6 @@ class CategorySerializer( super().__init__(*args, **kwargs) - # TODO INVE-T1 support complex filters if not path_detail and not isGeneratingSchema(): self.fields.pop('path', None) @@ -357,7 +355,6 @@ class PartBriefSerializer(InvenTree.serializers.InvenTreeModelSerializer): super().__init__(*args, **kwargs) - # TODO INVE-T1 support complex filters if not pricing and not isGeneratingSchema(): self.fields.pop('pricing_min', None) self.fields.pop('pricing_max', None) @@ -417,6 +414,25 @@ class PartParameterSerializer( 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): """Save the PartParameter instance.""" instance = super().save() @@ -428,15 +444,12 @@ class PartParameterSerializer( return instance - part_detail = can_filter( - PartBriefSerializer(source='part', many=False, read_only=True, allow_null=True) + part_detail = PartBriefSerializer( + source='part', many=False, read_only=True, allow_null=True ) - template_detail = can_filter( - PartParameterTemplateSerializer( - source='template', many=False, read_only=True, allow_null=True - ), - True, + template_detail = PartParameterTemplateSerializer( + source='template', many=False, read_only=True, allow_null=True ) updated_by_detail = UserSerializer( @@ -724,7 +737,9 @@ class PartSerializer( - Allows us to optionally pass extra fields based on the query. """ self.starred_parts = kwargs.pop('starred_parts', []) + category_detail = kwargs.pop('category_detail', False) location_detail = kwargs.pop('location_detail', False) + parameters = kwargs.pop('parameters', False) create = kwargs.pop('create', False) pricing = kwargs.pop('pricing', True) path_detail = kwargs.pop('path_detail', False) @@ -734,11 +749,15 @@ class PartSerializer( if isGeneratingSchema(): return - # TODO INVE-T1 support complex filters + if not category_detail: + self.fields.pop('category_detail', None) + if not location_detail: 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: self.fields.pop('category_path', None) @@ -750,7 +769,6 @@ class PartSerializer( continue self.fields.pop(f, None) - # TODO INVE-T1 support complex filters if not pricing: self.fields.pop('pricing_min', None) self.fields.pop('pricing_max', None) @@ -870,10 +888,8 @@ class PartSerializer( return part in self.starred_parts # Extra detail for the category - category_detail = can_filter( - CategorySerializer( - source='category', many=False, read_only=True, allow_null=True - ) + category_detail = CategorySerializer( + source='category', many=False, read_only=True, allow_null=True ) category_path = serializers.ListField( @@ -1002,9 +1018,7 @@ class PartSerializer( source='pricing_data.updated', allow_null=True, read_only=True ) - parameters = can_filter( - PartParameterSerializer(many=True, read_only=True, allow_null=True) - ) + parameters = PartParameterSerializer(many=True, read_only=True, allow_null=True) # Extra fields used only for creation of a new Part instance duplicate = DuplicatePartSerializer( @@ -1635,14 +1649,29 @@ class BomItemSerializer( - part_detail and sub_part_detail serializers are only included if requested. - 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) + substitutes = kwargs.pop('substitutes', True) super().__init__(*args, **kwargs) if isGeneratingSchema(): 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: self.fields.pop('pricing_min', None) self.fields.pop('pricing_max', None) @@ -1673,18 +1702,12 @@ class BomItemSerializer( help_text=_('Select the parent assembly'), ) - substitutes = can_filter( - BomItemSubstituteSerializer(many=True, read_only=True, allow_null=True), True + substitutes = BomItemSubstituteSerializer( + many=True, read_only=True, allow_null=True ) - part_detail = can_filter( - PartBriefSerializer( - source='part', - label=_('Assembly'), - many=False, - read_only=True, - allow_null=True, - ) + part_detail = PartBriefSerializer( + source='part', label=_('Assembly'), many=False, read_only=True, allow_null=True ) sub_part = serializers.PrimaryKeyRelatedField( @@ -1693,15 +1716,12 @@ class BomItemSerializer( help_text=_('Select the component part'), ) - sub_part_detail = can_filter( - PartBriefSerializer( - source='sub_part', - label=_('Component'), - many=False, - read_only=True, - allow_null=True, - ), - True, + sub_part_detail = PartBriefSerializer( + source='sub_part', + label=_('Component'), + many=False, + read_only=True, + allow_null=True, ) on_order = serializers.FloatField( @@ -1712,9 +1732,8 @@ class BomItemSerializer( label=_('In Production'), read_only=True, allow_null=True ) - can_build = can_filter( - serializers.FloatField(label=_('Can Build'), read_only=True, allow_null=True), - True, + can_build = serializers.FloatField( + label=_('Can Build'), read_only=True, allow_null=True ) # Cached pricing fields diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index c719bec0e7..eaa5be1972 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -406,12 +406,16 @@ class StockItemSerializer( def __init__(self, *args, **kwargs): """Add detail fields.""" path_detail = kwargs.pop('path_detail', False) + tests = kwargs.pop('tests', False) super().__init__(*args, **kwargs) if isGeneratingSchema(): return + if not tests: + self.fields.pop('tests', None) + # TODO INVE-T1 support complex filters if not path_detail: self.fields.pop('location_path', None) @@ -620,10 +624,8 @@ class StockItemSerializer( True, ) - tests = can_filter( - StockItemTestResultSerializer( - source='test_results', many=True, read_only=True, allow_null=True - ) + tests = StockItemTestResultSerializer( + source='test_results', many=True, read_only=True, allow_null=True ) quantity = InvenTreeDecimalField()