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,6 +96,7 @@ 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):
|
||||||
|
try:
|
||||||
event = Event.objects.get(slug=event_slug)
|
event = Event.objects.get(slug=event_slug)
|
||||||
query_tokens = query.split(' ')
|
query_tokens = query.split(' ')
|
||||||
q = Item.objects.filter(event=event)
|
q = Item.objects.filter(event=event)
|
||||||
|
@ -103,12 +104,15 @@ def search_items(request, event_slug, query):
|
||||||
if token:
|
if token:
|
||||||
q = q.filter(description__icontains=token)
|
q = q.filter(description__icontains=token)
|
||||||
return Response(ItemSerializer(q, many=True).data)
|
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):
|
||||||
|
try:
|
||||||
event = Event.objects.get(slug=event_slug)
|
event = Event.objects.get(slug=event_slug)
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
||||||
|
@ -117,6 +121,8 @@ def item(request, event_slug):
|
||||||
if validated_data.is_valid():
|
if validated_data.is_valid():
|
||||||
validated_data.save(event=event)
|
validated_data.save(event=event)
|
||||||
return Response(validated_data.data, status=201)
|
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