Use existing serializers to encode information for barcode response

This commit is contained in:
Oliver Walters 2020-04-15 23:41:16 +10:00
parent d19e287cb5
commit 10ee8bc666
3 changed files with 34 additions and 11 deletions

View File

@ -71,7 +71,7 @@ class ActionPluginView(APIView):
# If we got to here, no matching action was found
return Response({
'error': _("No matching action found for"),
'error': _("No matching action found"),
"action": action,
})
@ -136,4 +136,7 @@ class BarcodePluginView(APIView):
# Include the original barcode data
response['barcode_data'] = barcode_data
print("Response:")
print(response)
return Response(response)

View File

@ -96,6 +96,8 @@ class PartSerializer(InvenTreeModelSerializer):
queryset = queryset.prefetch_related('builds')
return queryset
# TODO - Include a 'category_detail' field which serializers the category object
class Meta:
model = Part
partial = True

View File

@ -2,6 +2,11 @@
import hashlib
from rest_framework.renderers import JSONRenderer
from stock.serializers import StockItemSerializer, LocationSerializer
from part.serializers import PartSerializer
import plugins.plugin as plugin
@ -46,18 +51,31 @@ class BarcodePlugin(plugin.InvenTreePlugin):
return None
def render_part(self, part):
return {
'id': part.id,
'name': part.full_name,
}
"""
Render a Part object to JSON
Use the existing serializer to do this.
"""
serializer = PartSerializer(part)
return serializer.data
def render_stock_location(self, loc):
return {
"id": loc.id
}
"""
Render a StockLocation object to JSON
Use the existing serializer to do this.
"""
serializer = LocationSerializer(loc)
return serializer.data
def render_stock_item(self, item):
"""
Render a StockItem object to JSON.
Use the existing serializer to do this
"""
return {
"id": item.id,
}
serializer = StockItemSerializer(item, part_detail=True, location_detail=True, supplier_detail=True)
return serializer.data