stash
This commit is contained in:
parent
7b0524a219
commit
c471aeb6b2
1 changed files with 13 additions and 7 deletions
|
@ -20,18 +20,24 @@ class ContainerViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Container.objects.all()
|
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'])
|
@api_view(['GET'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([])
|
||||||
|
#@permission_classes([IsAuthenticated])
|
||||||
#@permission_required('view_item', raise_exception=True)
|
#@permission_required('view_item', raise_exception=True)
|
||||||
def search_items(request, event_slug, query):
|
def search_items(request, event_slug, query):
|
||||||
try:
|
try:
|
||||||
event = Event.objects.get(slug=event_slug)
|
event = Event.objects.get(slug=event_slug)
|
||||||
query_tokens = query.split(' ')
|
items = filter_items(Item.objects.filter(event=event), query)
|
||||||
q = Item.objects.filter(event=event)
|
return Response(ItemSerializer(items, many=True).data)
|
||||||
for token in query_tokens:
|
|
||||||
if token:
|
|
||||||
q = q.filter(description__icontains=token)
|
|
||||||
return Response(ItemSerializer(q, many=True).data)
|
|
||||||
except Event.DoesNotExist:
|
except Event.DoesNotExist:
|
||||||
return Response(status=404)
|
return Response(status=404)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue