Expose test templates to the API

This commit is contained in:
Oliver Walters 2020-05-17 13:56:49 +10:00
parent f791ac9f57
commit 4d992ea528
2 changed files with 63 additions and 2 deletions

View File

@ -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'),
])),

View File

@ -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.