From aa08de0d10e13e36fa61088143471e972d18b842 Mon Sep 17 00:00:00 2001 From: stefan Date: Sun, 17 Nov 2024 23:28:15 +0100 Subject: [PATCH] extended api with read only fields to improve label writing --- shimatta_kenkyusho/api/serializers.py | 51 +++++++++++++++++++++++---- shimatta_kenkyusho/api/urls.py | 2 ++ shimatta_kenkyusho/api/views.py | 22 +++++++++++- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/shimatta_kenkyusho/api/serializers.py b/shimatta_kenkyusho/api/serializers.py index 4c404cb..183e134 100644 --- a/shimatta_kenkyusho/api/serializers.py +++ b/shimatta_kenkyusho/api/serializers.py @@ -24,22 +24,43 @@ class PackageSerializer(serializers.HyperlinkedModelSerializer): model = parts_models.Package fields = '__all__' -class StorageSerializer(serializers.HyperlinkedModelSerializer): - full_path = serializers.ReadOnlyField(source='get_full_path') - +class ComponentParameterSerializer(serializers.HyperlinkedModelSerializer): + id = serializers.ReadOnlyField() + ro_parameter_type = serializers.ReadOnlyField(source='parameter_type.parameter_name') class Meta: - model = parts_models.Storage - fields = ['url', 'id', 'name', 'verbose_name', 'parent_storage', 'responsible', 'template', 'full_path'] + model = parts_models.ComponentParameter + fields = '__all__' + +class ComponentDistributorNumSerializer(serializers.HyperlinkedModelSerializer): + id = serializers.ReadOnlyField() + class Meta: + model = parts_models.DistributorNum + fields = '__all__' class ComponentSerializer(serializers.HyperlinkedModelSerializer): package_data = PackageSerializerNoLink(source='package', read_only=True) ro_manufacturer_name = serializers.ReadOnlyField(source='manufacturer.name') ro_image = serializers.ReadOnlyField(source='get_resolved_image') + ro_component_type = serializers.ReadOnlyField(source='component_type.class_name') + ro_parameters = ComponentParameterSerializer(many=True, source='componentparameter_set', read_only=True) + ro_distributor_numbers = ComponentDistributorNumSerializer(many=True, source='distributornum_set', read_only=True) class Meta: model = parts_models.Component - fields = ['url', 'id', 'name', 'package_data', 'package', 'pref_distri', 'image', 'manufacturer', 'ro_manufacturer_name', 'ro_image'] - + fields = ['url', + 'id', + 'name', + 'package_data', + 'package', + 'pref_distri', + 'image', + 'manufacturer', + 'component_type', + 'ro_manufacturer_name', + 'ro_image', + 'ro_component_type', + 'ro_parameters', + 'ro_distributor_numbers'] class StockSerializer(serializers.HyperlinkedModelSerializer): id = serializers.ReadOnlyField() @@ -51,6 +72,22 @@ class StockSerializer(serializers.HyperlinkedModelSerializer): model = parts_models.Stock fields = '__all__' +class StockSerializerExpandComponent(StockSerializer): + ro_component = ComponentSerializer(read_only=True, source='component') + +class StorageSerializer(serializers.HyperlinkedModelSerializer): + full_path = serializers.ReadOnlyField(source='get_full_path') + + class Meta: + model = parts_models.Storage + fields = ['url', 'id', 'name', 'verbose_name', 'parent_storage', 'responsible', 'template', 'full_path'] + +class StorageSerializerStocksExpanded(StorageSerializer): + ro_stocks = StockSerializerExpandComponent(many=True, read_only=True, source='stock_set') + + class Meta(StorageSerializer.Meta): + fields = StorageSerializer.Meta.fields + ['ro_stocks'] + class DistributorSerializer(serializers.HyperlinkedModelSerializer): id = serializers.ReadOnlyField() class Meta: diff --git a/shimatta_kenkyusho/api/urls.py b/shimatta_kenkyusho/api/urls.py index 1aa3de9..688b109 100644 --- a/shimatta_kenkyusho/api/urls.py +++ b/shimatta_kenkyusho/api/urls.py @@ -9,6 +9,8 @@ router.register(r'groups', GroupViewSet) router.register(r'parts/storages', PartsStorageViewSet) router.register(r'parts/storage_templates', PartsStorageTemplatesViewSet, basename='storage-template') router.register(r'parts/components', PartsComponentViewSet) +router.register(r'parts/component-parameters', PartsComponentParameterViewSet) +router.register(r'parts/component-distributor-numbers', PartsComponentDistributorNumViewSet) router.register(r'parts/stocks', PartsStockViewSet) router.register(r'parts/packages', PartsPackageViewSet) router.register(r'parts/distributors', PartsDistributorviewSet) diff --git a/shimatta_kenkyusho/api/views.py b/shimatta_kenkyusho/api/views.py index 137e344..c6d8ef7 100644 --- a/shimatta_kenkyusho/api/views.py +++ b/shimatta_kenkyusho/api/views.py @@ -42,12 +42,16 @@ class GroupViewSet(viewsets.ReadOnlyModelViewSet): class PartsStorageViewSet(viewsets.ModelViewSet): queryset = parts_models.Storage.objects.all() - serializer_class = StorageSerializer permission_classes = [permissions.DjangoModelPermissions] filter_backends = [django_filters.rest_framework.DjangoFilterBackend] search_fields = ['id', 'name', 'parent_storage'] filterset_fields = ['id', 'name', 'parent_storage'] + def get_serializer_class(self): + if self.request.GET.get('expand_stocks'): + return StorageSerializerStocksExpanded + return StorageSerializer + class PartsStorageTemplatesViewSet(viewsets.ReadOnlyModelViewSet): queryset = parts_models.Storage.objects.filter(is_template=True) serializer_class = StorageSerializer @@ -64,6 +68,22 @@ class PartsComponentViewSet(viewsets.ModelViewSet): search_fields = ['id', 'name', 'package__name', 'manufacturer__name'] filterset_fields = ['id', 'name'] +class PartsComponentParameterViewSet(viewsets.ModelViewSet): + queryset = parts_models.ComponentParameter.objects.all() + serializer_class = ComponentParameterSerializer + permission_classes = [permissions.DjangoModelPermissions] + filter_backends = [filters.SearchFilter, django_filters.rest_framework.DjangoFilterBackend] + search_fields = ['id', 'parameter_type__parameter_name'] + filterset_fields = ['id'] + +class PartsComponentDistributorNumViewSet(viewsets.ModelViewSet): + queryset = parts_models.DistributorNum.objects.all() + serializer_class = ComponentDistributorNumSerializer + permission_classes = [permissions.DjangoModelPermissions] + filter_backends = [filters.SearchFilter, django_filters.rest_framework.DjangoFilterBackend] + search_fields = ['id', 'distributor', 'distributor_part_number'] + filterset_fields = ['id'] + class PartsComponentTypeViewSet(viewsets.ModelViewSet): queryset = parts_models.ComponentType.objects.all() serializer_class = ComponentTypeSerializer -- 2.47.0