From 4d992ea52851fca1853d5bbf5c2159825caa43fd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 17 May 2020 13:56:49 +1000 Subject: [PATCH] Expose test templates to the API --- InvenTree/part/api.py | 48 +++++++++++++++++++++++++++++++++-- InvenTree/part/serializers.py | 17 +++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 84ff83af0a..26210badd6 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -19,7 +19,7 @@ from django.urls import reverse from .models import Part, PartCategory, BomItem, PartStar from .models import PartParameter, PartParameterTemplate -from .models import PartAttachment +from .models import PartAttachment, PartTestTemplate from . import serializers as part_serializers @@ -120,6 +120,45 @@ class PartAttachmentList(generics.ListCreateAPIView, AttachmentMixin): ] +class PartTestTemplateList(generics.ListCreateAPIView): + """ + API endpoint for listing (and creating) a PartTestTemplate. + """ + + queryset = PartTestTemplate.objects.all() + serializer_class = part_serializers.PartTestTemplateSerializer + + def filter_queryset(self, queryset): + """ + Filter the test list queryset. + + If filtering by 'part', we include results for any parts "above" the specified part. + """ + + queryset = super().filter_queryset(queryset) + + params = self.request.query_params + + part = params.get('part', None) + + # Filter by part + if part: + try: + part = Part.objects.get(pk=part) + queryset = queryset.filter(part__in=part.get_ancestors(include_self=True)) + except (ValueError, Part.DoesNotExist): + pass + + return queryset + + permission_classes = [permissions.IsAuthenticated] + + filter_backends = [ + DjangoFilterBackend, + filters.OrderingFilter, + filters.SearchFilter, + ] + class PartThumbs(generics.ListAPIView): """ API endpoint for retrieving information on available Part thumbnails """ @@ -635,8 +674,13 @@ part_api_urls = [ url(r'^$', CategoryList.as_view(), name='api-part-category-list'), ])), + # Base URL for PartTestTemplate API endpoints + url(r'^test-template/', include([ + url(r'^$', PartTestTemplateList.as_view(), name='api-part-test-template-list'), + ])), + # Base URL for PartAttachment API endpoints - url(r'attachment/', include([ + url(r'^attachment/', include([ url(r'^$', PartAttachmentList.as_view(), name='api-part-attachment-list'), ])), diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 74005dd5af..396f19ea58 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -10,6 +10,7 @@ from .models import PartCategory from .models import BomItem from .models import PartParameter, PartParameterTemplate from .models import PartAttachment +from .models import PartTestTemplate from decimal import Decimal @@ -56,6 +57,22 @@ class PartAttachmentSerializer(InvenTreeModelSerializer): ] +class PartTestTemplateSerializer(InvenTreeModelSerializer): + """ + Serializer for the PartTestTemplate class + """ + + class Meta: + model = PartTestTemplate + + fields = [ + 'pk', + 'part', + 'test_name', + 'required' + ] + + class PartThumbSerializer(serializers.Serializer): """ Serializer for the 'image' field of the Part model.