This commit is contained in:
j3d1 2024-07-17 18:59:54 +02:00
parent c40467f325
commit 5f28406d95

View file

@ -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)