2017-04-28 12:14:27 +00:00
|
|
|
from rest_framework import viewsets, generics, filters
|
2017-03-06 15:50:55 +00:00
|
|
|
from rest_framework.response import Response
|
2023-07-10 17:40:15 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
2017-12-13 20:21:13 +00:00
|
|
|
from rest_framework.permissions import AllowAny
|
2017-03-06 15:50:55 +00:00
|
|
|
|
2017-04-28 12:14:27 +00:00
|
|
|
from storage.models import Item, Label
|
|
|
|
from storage.serializers import ItemSerializer, LabelSerializer
|
2020-05-20 21:15:57 +00:00
|
|
|
from django.http import Http404
|
2017-03-06 15:50:55 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2017-10-22 21:51:22 +00:00
|
|
|
from storage.views import apply_smart_search
|
|
|
|
|
|
|
|
|
2023-07-17 18:14:06 +00:00
|
|
|
def api_print(quantity, obj):
|
|
|
|
amount = min(int(quantity), 5)
|
|
|
|
for _ in range(amount):
|
|
|
|
obj.print()
|
|
|
|
return Response({"status": "success"})
|
|
|
|
|
|
|
|
|
2017-10-22 21:51:22 +00:00
|
|
|
class SmartSearchFilterBackend(filters.BaseFilterBackend):
|
|
|
|
"""
|
|
|
|
Filters query using smartsearch filter
|
|
|
|
"""
|
|
|
|
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
2023-07-11 13:34:35 +00:00
|
|
|
search_query = request.query_params.get("smartsearch", None)
|
2017-10-22 21:51:22 +00:00
|
|
|
if search_query:
|
|
|
|
return apply_smart_search(search_query, queryset)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2017-04-28 12:14:27 +00:00
|
|
|
class LabelViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint that allows items to be viewed or edited.
|
|
|
|
"""
|
2023-07-11 13:34:35 +00:00
|
|
|
|
2023-07-17 13:46:00 +00:00
|
|
|
queryset = Label.objects.all()
|
2017-04-28 12:14:27 +00:00
|
|
|
serializer_class = LabelSerializer
|
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
2017-12-13 20:21:13 +00:00
|
|
|
def print(self, request, pk):
|
2023-07-17 18:14:06 +00:00
|
|
|
return api_print(request.query_params.get("quantity", 1), self.get_object())
|
2017-12-13 20:21:13 +00:00
|
|
|
|
2017-03-06 15:50:55 +00:00
|
|
|
|
|
|
|
class ItemViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint that allows items to be viewed or edited.
|
|
|
|
"""
|
2023-07-11 13:34:35 +00:00
|
|
|
|
2023-07-17 13:46:00 +00:00
|
|
|
queryset = Item.objects.all()
|
2017-03-06 15:50:55 +00:00
|
|
|
serializer_class = ItemSerializer
|
2017-10-22 21:51:22 +00:00
|
|
|
filter_backends = (SmartSearchFilterBackend, filters.OrderingFilter)
|
2023-07-11 13:34:35 +00:00
|
|
|
ordering_fields = "__all__"
|
2017-03-06 15:50:55 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2023-07-17 13:46:00 +00:00
|
|
|
return Item.objects.filter(**{"path__level": 1})
|
2017-03-06 15:50:55 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
|
|
|
|
|
2020-05-20 21:15:57 +00:00
|
|
|
obj = self.get_item_by_id_or_label(self.kwargs[lookup_url_kwarg])
|
2017-03-06 15:50:55 +00:00
|
|
|
self.check_object_permissions(self.request, obj)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2020-05-20 21:15:57 +00:00
|
|
|
def get_item_by_id_or_label(self, id):
|
|
|
|
try:
|
2023-07-11 13:34:35 +00:00
|
|
|
item = Item.objects.get(uuid__startswith=id) # look up by short id
|
2020-05-20 21:15:57 +00:00
|
|
|
return item
|
|
|
|
except Item.DoesNotExist:
|
|
|
|
try:
|
|
|
|
label = Label.objects.get(pk=id)
|
|
|
|
return label.item
|
|
|
|
except Label.DoesNotExist:
|
|
|
|
raise Http404()
|
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
2020-05-20 21:23:23 +00:00
|
|
|
def print(self, request, pk):
|
2023-07-17 18:14:06 +00:00
|
|
|
return api_print(request.query_params.get("quantity", 1), self.get_object())
|
2020-05-20 21:23:23 +00:00
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(
|
|
|
|
detail=True,
|
|
|
|
)
|
2017-03-06 15:50:55 +00:00
|
|
|
def children(self, request, pk):
|
|
|
|
item = self.get_object()
|
2023-07-11 13:34:35 +00:00
|
|
|
return Response(
|
|
|
|
self.serializer_class(item.get_children().all(), many=True).data
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(
|
|
|
|
detail=True,
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
def ancestors(self, request, pk):
|
|
|
|
item = self.get_object()
|
2023-07-11 13:34:35 +00:00
|
|
|
return Response(
|
|
|
|
self.serializer_class(item.get_ancestors().all(), many=True).data
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(
|
|
|
|
detail=True,
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
def descendants(self, request, pk):
|
|
|
|
item = self.get_object()
|
2023-07-11 13:34:35 +00:00
|
|
|
return Response(
|
|
|
|
self.serializer_class(item.get_descendants().all(), many=True).data
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
|
2023-07-10 17:40:15 +00:00
|
|
|
@action(
|
|
|
|
detail=True,
|
|
|
|
)
|
2017-04-24 19:57:22 +00:00
|
|
|
def siblings(self, request, pk):
|
|
|
|
item = self.get_object()
|
2023-07-11 13:34:35 +00:00
|
|
|
return Response(
|
|
|
|
self.serializer_class(item.get_siblings().all(), many=True).data
|
|
|
|
)
|