From 1327c1d3b1714ca60641b78d29a8be8dee6200a4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 10 Feb 2020 22:03:06 +1100 Subject: [PATCH] Add API endpoint for querying part images --- InvenTree/part/api.py | 57 +++++++++++++++++++++++++---------- InvenTree/part/serializers.py | 10 ++++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 75c8f3a60f..424f03fd0b 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -8,7 +8,7 @@ from __future__ import unicode_literals from django_filters.rest_framework import DjangoFilterBackend from django.conf import settings -from django.db.models import Sum +from django.db.models import Sum, Count from rest_framework import status from rest_framework.response import Response @@ -23,10 +23,7 @@ import os from .models import Part, PartCategory, BomItem, PartStar from .models import PartParameter, PartParameterTemplate -from .serializers import PartSerializer, BomItemSerializer -from .serializers import CategorySerializer -from .serializers import PartStarSerializer -from .serializers import PartParameterSerializer, PartParameterTemplateSerializer +from . import serializers as part_serializers from InvenTree.views import TreeSerializer from InvenTree.helpers import str2bool @@ -53,7 +50,7 @@ class CategoryList(generics.ListCreateAPIView): """ queryset = PartCategory.objects.all() - serializer_class = CategorySerializer + serializer_class = part_serializers.CategorySerializer permission_classes = [ permissions.IsAuthenticated, @@ -83,14 +80,40 @@ class CategoryList(generics.ListCreateAPIView): class CategoryDetail(generics.RetrieveUpdateDestroyAPIView): """ API endpoint for detail view of a single PartCategory object """ - serializer_class = CategorySerializer + serializer_class = part_serializers.CategorySerializer queryset = PartCategory.objects.all() +class PartThumbs(generics.ListAPIView): + """ API endpoint for retrieving information on available Part thumbnails """ + + serializer_class = part_serializers.PartThumbSerializer + + def list(self, reguest, *args, **kwargs): + """ + Serialize the available Part images. + - Images may be used for multiple parts! + """ + + # Get all Parts which have an associated image + queryset = Part.objects.all().exclude(image='') + + data = queryset.values( + 'image', + ).annotate(count=Count('image')).order_by('-count') + + print("Parts with img:", queryset.count()) + + print(data) + + return Response(data) + + class PartDetail(generics.RetrieveUpdateAPIView): """ API endpoint for detail view of a single Part object """ + queryset = Part.objects.all() - serializer_class = PartSerializer + serializer_class = part_serializers.PartSerializer permission_classes = [ permissions.IsAuthenticated, @@ -104,12 +127,12 @@ class PartList(generics.ListCreateAPIView): - POST: Create a new Part object """ - serializer_class = PartSerializer + serializer_class = part_serializers.PartSerializer def list(self, request, *args, **kwargs): """ Instead of using the DRF serialiser to LIST, - we serialize the objects manuually. + we serialize the objects manually. This turns out to be significantly faster. """ @@ -218,7 +241,7 @@ class PartStarDetail(generics.RetrieveDestroyAPIView): """ API endpoint for viewing or removing a PartStar object """ queryset = PartStar.objects.all() - serializer_class = PartStarSerializer + serializer_class = part_serializers.PartStarSerializer class PartStarList(generics.ListCreateAPIView): @@ -229,7 +252,7 @@ class PartStarList(generics.ListCreateAPIView): """ queryset = PartStar.objects.all() - serializer_class = PartStarSerializer + serializer_class = part_serializers.PartStarSerializer def create(self, request, *args, **kwargs): @@ -271,7 +294,7 @@ class PartParameterTemplateList(generics.ListCreateAPIView): """ queryset = PartParameterTemplate.objects.all() - serializer_class = PartParameterTemplateSerializer + serializer_class = part_serializers.PartParameterTemplateSerializer permission_classes = [ permissions.IsAuthenticated, @@ -294,7 +317,7 @@ class PartParameterList(generics.ListCreateAPIView): """ queryset = PartParameter.objects.all() - serializer_class = PartParameterSerializer + serializer_class = part_serializers.PartParameterSerializer permission_classes = [ permissions.IsAuthenticated, @@ -317,7 +340,7 @@ class BomList(generics.ListCreateAPIView): - POST: Create a new BomItem object """ - serializer_class = BomItemSerializer + serializer_class = part_serializers.BomItemSerializer def get_serializer(self, *args, **kwargs): @@ -360,7 +383,7 @@ class BomDetail(generics.RetrieveUpdateDestroyAPIView): """ API endpoint for detail view of a single BomItem object """ queryset = BomItem.objects.all() - serializer_class = BomItemSerializer + serializer_class = part_serializers.BomItemSerializer permission_classes = [ permissions.IsAuthenticated, @@ -424,6 +447,8 @@ part_api_urls = [ url(r'^star/', include(part_star_api_urls)), url(r'^parameter/', include(part_param_api_urls)), + url(r'^thumbs/', PartThumbs.as_view(), name='api-part-thumbs'), + url(r'^(?P\d+)/?', PartDetail.as_view(), name='api-part-detail'), url(r'^.*$', PartList.as_view(), name='api-part-list'), diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index bdeead670d..3e1ed6949a 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -30,6 +30,16 @@ class CategorySerializer(InvenTreeModelSerializer): ] +class PartThumbSerializer(serializers.Serializer): + """ + Serializer for the 'image' field of the Part model. + Used to serve and display existing Part images. + """ + + image = serializers.URLField(read_only=True) + count = serializers.IntegerField(read_only=True) + + class PartBriefSerializer(InvenTreeModelSerializer): """ Serializer for Part (brief detail) """