Add detail API endpoint for BuildItem model

This commit is contained in:
Oliver 2021-10-04 23:49:31 +11:00
parent 5ded23fd99
commit 8f298f71ef
2 changed files with 16 additions and 19 deletions

View File

@ -98,7 +98,7 @@ class BuildList(generics.ListCreateAPIView):
as some of the fields don't natively play nicely with DRF as some of the fields don't natively play nicely with DRF
""" """
queryset = super().get_queryset().prefetch_related('part') queryset = super().get_queryset().select_related('part')
queryset = BuildSerializer.annotate_queryset(queryset) queryset = BuildSerializer.annotate_queryset(queryset)
@ -229,6 +229,15 @@ class BuildAllocate(generics.CreateAPIView):
return context return context
class BuildItemDetail(generics.RetrieveUpdateDestroyAPIView):
"""
API endpoint for detail view of a BuildItem object
"""
queryset = BuildItem.objects.all()
serializer_class = BuildItemSerializer
class BuildItemList(generics.ListCreateAPIView): class BuildItemList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of BuildItem objects """ API endpoint for accessing a list of BuildItem objects
@ -258,9 +267,9 @@ class BuildItemList(generics.ListCreateAPIView):
query = BuildItem.objects.all() query = BuildItem.objects.all()
query = query.select_related('stock_item') query = query.select_related('stock_item__location')
query = query.prefetch_related('stock_item__part') query = query.select_related('stock_item__part')
query = query.prefetch_related('stock_item__part__category') query = query.select_related('stock_item__part__category')
return query return query
@ -330,12 +339,13 @@ build_api_urls = [
# Attachments # Attachments
url(r'^attachment/', include([ url(r'^attachment/', include([
url(r'^(?P<pk>\d+)/', BuildAttachmentDetail.as_view(), name='api-build-attachment-detail'), url(r'^(?P<pk>\d+)/', BuildAttachmentDetail.as_view(), name='api-build-attachment-detail'),
url('^.*$', BuildAttachmentList.as_view(), name='api-build-attachment-list'), url(r'^.*$', BuildAttachmentList.as_view(), name='api-build-attachment-list'),
])), ])),
# Build Items # Build Items
url(r'^item/', include([ url(r'^item/', include([
url('^.*$', BuildItemList.as_view(), name='api-build-item-list') url(r'^(?P<pk>\d+)/', BuildItemDetail.as_view(), name='api-build-item-detail'),
url(r'^.*$', BuildItemList.as_view(), name='api-build-item-list'),
])), ])),
# Build Detail # Build Detail

View File

@ -256,22 +256,13 @@ class BuildAllocationSerializer(serializers.Serializer):
return data return data
def save(self): def save(self):
print("creating new allocation items!")
data = self.validated_data data = self.validated_data
print("data:")
print(data)
items = data.get('items', []) items = data.get('items', [])
print("items:")
print(items)
build = self.context['build'] build = self.context['build']
created_items = []
with transaction.atomic(): with transaction.atomic():
for item in items: for item in items:
bom_item = item['bom_item'] bom_item = item['bom_item']
@ -288,10 +279,6 @@ class BuildAllocationSerializer(serializers.Serializer):
install_into=output install_into=output
) )
created_items.append(build_item)
return created_items
class BuildItemSerializer(InvenTreeModelSerializer): class BuildItemSerializer(InvenTreeModelSerializer):
""" Serializes a BuildItem object """ """ Serializes a BuildItem object """