blob: 3741cce511881c44a32dac2969fa3cf45b9f0f2e [file] [log] [blame]
Tony Mack29c287f2013-04-11 21:07:16 -04001from django.http import Http404
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.api.slivers import add_sliver, delete_sliver, get_slivers, update_sliver
7from core.serializers import SliverSerializer
8from util.request import parse_request
Tony Mack29c287f2013-04-11 21:07:16 -04009
10
11class SliverListCreate(APIView):
12 """
13 List all slivers or create a new sliver.
14 """
15
16 def post(self, request, format = None):
17 data = parse_request(request.DATA)
18 if 'auth' not in data:
19 return Response(status=status.HTTP_400_BAD_REQUEST)
20 elif 'sliver' in data:
21 sliver = add_sliver(data['auth'], data['sliver'])
22 serializer = SliverSerializer(sliver)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
25 slivers = get_slivers(data['auth'])
26 serializer = SliverSerializer(slivers, many=True)
27 return Response(serializer.data)
28
29
30class SliverRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a sliver
33 """
34
35 def post(self, request, pk, format=None):
36 """Retrieve a sliver"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack79748612013-05-01 14:52:03 -040040 slivers = get_slivers(data['auth'], pk)
Tony Mack29c287f2013-04-11 21:07:16 -040041 if not slivers:
42 return Response(status=status.HTTP_404_NOT_FOUND)
43 serializer = SliverSerializer(slivers[0])
44 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """update a sliver"""
48 data = parse_request(request.DATA)
49 if 'auth' not in data:
50 return Response(status=status.HTTP_400_BAD_REQUEST)
51 elif 'sliver' not in data:
52 return Response(status=status.HTTP_400_BAD_REQUEST)
53
54 sliver = update_sliver(pk, data['sliver'])
55 serializer = SliverSerializer(sliver)
56 return Response(serializer.data)
57
58 def delete(self, request, pk, format=None):
59 data = parse_request(request.DATA)
60 if 'auth' not in data:
61 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack79748612013-05-01 14:52:03 -040062 delete_sliver(data['auth'], pk)
Tony Mack29c287f2013-04-11 21:07:16 -040063 return Response(status=status.HTTP_204_NO_CONTENT)
64
65
66