From 2e0c031fab9284b87d8599b0cb4b6d91bc48deec Mon Sep 17 00:00:00 2001 From: radex Date: Wed, 20 May 2020 23:15:57 +0200 Subject: [PATCH] /items/:id now fetches via short_id or label.id (now considered legacy) --- storage/apiviews.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/storage/apiviews.py b/storage/apiviews.py index 54ee944..6de797c 100644 --- a/storage/apiviews.py +++ b/storage/apiviews.py @@ -5,6 +5,7 @@ from rest_framework.permissions import AllowAny from storage.models import Item, Label from storage.serializers import ItemSerializer, LabelSerializer +from django.http import Http404 from django.shortcuts import get_object_or_404 from storage.views import apply_smart_search @@ -55,11 +56,22 @@ class ItemViewSet(viewsets.ModelViewSet): def get_object(self): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - obj = get_object_or_404(Item, pk=self.kwargs[lookup_url_kwarg]) + obj = self.get_item_by_id_or_label(self.kwargs[lookup_url_kwarg]) self.check_object_permissions(self.request, obj) return obj + def get_item_by_id_or_label(self, id): + try: + item = Item.objects.get(uuid__startswith=id) # look up by short id + return item + except Item.DoesNotExist: + try: + label = Label.objects.get(pk=id) + return label.item + except Label.DoesNotExist: + raise Http404() + @detail_route() def children(self, request, pk): item = self.get_object()