blob: 10bc42c0a3ac12009ce70c77bfb653bda8fa95b6 [file] [log] [blame]
Tony Mack3a1af412013-04-09 22:36:27 -04001from django.http import Http404
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5
6from plstackapi.core.api.slices import add_slice, delete_slice, get_slices, update_slice
7from plstackapi.core.serializers import SliceSerializer
8from plstackapi.util.request import parse_request
9
10
11class SliceListCreate(APIView):
12 """
13 List all slices or create a new slice.
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 'slice' in data:
21 slice = add_slice(data['auth'], data['slice'])
22 serializer = SliceSerializer(slice)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
25 slices = get_slices(data['auth'])
26 serializer = SliceSerializer(slices, many=True)
27 return Response(serializer.data)
28
29
30class SliceRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a slice
33 """
34
35 def post(self, request, pk, format=None):
36 """Retrieve a slice"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack875e58a2013-05-02 15:46:27 -040040 slices = get_slices(data['auth'], pk)
Tony Mack3a1af412013-04-09 22:36:27 -040041 if not slices:
42 return Response(status=status.HTTP_404_NOT_FOUND)
Tony Mack29c287f2013-04-11 21:07:16 -040043 serializer = SliceSerializer(slices[0])
Tony Mack3a1af412013-04-09 22:36:27 -040044 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """update a slice"""
48 data = parse_request(request.DATA)
49 if 'auth' not in data:
50 return Response(status=status.HTTP_400_BAD_REQUEST)
51 elif 'slice' not in data:
52 return Response(status=status.HTTP_400_BAD_REQUEST)
53
54 slice = update_slice(pk, data['slice'])
Tony Mack29c287f2013-04-11 21:07:16 -040055 serializer = SliceSerializer(slice)
Tony Mack3a1af412013-04-09 22:36:27 -040056 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 Mack875e58a2013-05-02 15:46:27 -040062 delete_slice(data['auth'], pk)
Tony Mack3a1af412013-04-09 22:36:27 -040063 return Response(status=status.HTTP_204_NO_CONTENT)
64
65
66