return 404 when events doesn't exist
This commit is contained in:
parent
dedf98a12e
commit
04581b66b6
2 changed files with 28 additions and 15 deletions
|
@ -96,27 +96,33 @@ class ItemSerializer(serializers.ModelSerializer):
|
||||||
@permission_classes([])
|
@permission_classes([])
|
||||||
@authentication_classes([])
|
@authentication_classes([])
|
||||||
def search_items(request, event_slug, query):
|
def search_items(request, event_slug, query):
|
||||||
event = Event.objects.get(slug=event_slug)
|
try:
|
||||||
query_tokens = query.split(' ')
|
event = Event.objects.get(slug=event_slug)
|
||||||
q = Item.objects.filter(event=event)
|
query_tokens = query.split(' ')
|
||||||
for token in query_tokens:
|
q = Item.objects.filter(event=event)
|
||||||
if token:
|
for token in query_tokens:
|
||||||
q = q.filter(description__icontains=token)
|
if token:
|
||||||
return Response(ItemSerializer(q, many=True).data)
|
q = q.filter(description__icontains=token)
|
||||||
|
return Response(ItemSerializer(q, many=True).data)
|
||||||
|
except Event.DoesNotExist:
|
||||||
|
return Response(status=404)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'POST'])
|
@api_view(['GET', 'POST'])
|
||||||
@permission_classes([])
|
@permission_classes([])
|
||||||
@authentication_classes([])
|
@authentication_classes([])
|
||||||
def item(request, event_slug):
|
def item(request, event_slug):
|
||||||
event = Event.objects.get(slug=event_slug)
|
try:
|
||||||
if request.method == 'GET':
|
event = Event.objects.get(slug=event_slug)
|
||||||
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
if request.method == 'GET':
|
||||||
elif request.method == 'POST':
|
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
||||||
validated_data = ItemSerializer(data=request.data)
|
elif request.method == 'POST':
|
||||||
if validated_data.is_valid():
|
validated_data = ItemSerializer(data=request.data)
|
||||||
validated_data.save(event=event)
|
if validated_data.is_valid():
|
||||||
return Response(validated_data.data, status=201)
|
validated_data.save(event=event)
|
||||||
|
return Response(validated_data.data, status=201)
|
||||||
|
except Event.DoesNotExist:
|
||||||
|
return Response(status=404)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'PUT', 'DELETE'])
|
@api_view(['GET', 'PUT', 'DELETE'])
|
||||||
|
@ -138,6 +144,8 @@ def item_by_id(request, event_slug, id):
|
||||||
return Response(status=204)
|
return Response(status=204)
|
||||||
except Item.DoesNotExist:
|
except Item.DoesNotExist:
|
||||||
return Response(status=404)
|
return Response(status=404)
|
||||||
|
except Event.DoesNotExist:
|
||||||
|
return Response(status=404)
|
||||||
|
|
||||||
|
|
||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
|
|
|
@ -91,3 +91,8 @@ class ItemTestCase(TestCase):
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
self.assertEqual(len(response.json()), 1)
|
||||||
self.assertEqual(response.json()[0]['itemCount'], 2)
|
self.assertEqual(response.json()[0]['itemCount'], 2)
|
||||||
|
|
||||||
|
def test_item_nonexistent(self):
|
||||||
|
response = client.get(f'/api/1/NOEVENT/item/')
|
||||||
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue