From e58779b98fd3e5066881fe86181d36d154bd97dd Mon Sep 17 00:00:00 2001 From: jedi Date: Wed, 17 Jul 2024 18:59:54 +0200 Subject: [PATCH] stash --- core/inventory/api_v2.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/inventory/api_v2.py b/core/inventory/api_v2.py index ef3ae90..d69b6f7 100644 --- a/core/inventory/api_v2.py +++ b/core/inventory/api_v2.py @@ -20,18 +20,24 @@ class ContainerViewSet(viewsets.ModelViewSet): queryset = Container.objects.all() +def filter_items(items, query): + query_tokens = query.split(' ') + for item in items: + item_tokens = item.name.split(' ') + if all(token in item_tokens for token in query_tokens): + yield item + elif all(token in item.description for token in query_tokens): + yield item + @api_view(['GET']) -@permission_classes([IsAuthenticated]) +@permission_classes([]) +#@permission_classes([IsAuthenticated]) #@permission_required('view_item', raise_exception=True) def search_items(request, event_slug, query): try: event = Event.objects.get(slug=event_slug) - query_tokens = query.split(' ') - q = Item.objects.filter(event=event) - for token in query_tokens: - if token: - q = q.filter(description__icontains=token) - return Response(ItemSerializer(q, many=True).data) + items = filter_items(Item.objects.filter(event=event), query) + return Response(ItemSerializer(items, many=True).data) except Event.DoesNotExist: return Response(status=404)