blob: a4f684865eb7bb5a83f11258b42e85cad97fba44 [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
6from plstackapi.core.api.slice_memberships import add_slice_membership, delete_slice_membership, get_slice_memberships, update_slice_membership
7from plstackapi.core.serializers import SliceMembershipSerializer
8from plstackapi.util.request import parse_request
9
10
11class SliceMembershipListCreate(APIView):
12 """
13 List all slice_memberships or create a new slice_membership.
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_membership' in data:
21 slice_membership = add_slice_membership(data['auth'], data['slice_membership'])
22 serializer = SliceMembershipSerializer(slice_membership)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
25 slice_memberships = get_slice_memberships(data['auth'])
26 serializer = SliceMembershipSerializer(slice_memberships, many=True)
27 return Response(serializer.data)
28
29
30class SliceMembershipRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a slice_membership
33 """
34
35 def post(self, request, pk, format=None):
36 """Retrieve a slice_membership"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
40 slice_memberships = get_slice_memberships(data['auth'], pk)
41 if not slice_memberships:
42 return Response(status=status.HTTP_404_NOT_FOUND)
43 serializer = SliceMembershipSerializer(slice_memberships[0])
44 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """update a slice_membership"""
48 data = parse_request(request.DATA)
49 if 'auth' not in data:
50 return Response(status=status.HTTP_400_BAD_REQUEST)
51 elif 'slice_membership' not in data:
52 return Response(status=status.HTTP_400_BAD_REQUEST)
53
54 slice_membership = update_slice_membership(pk, data['slice_membership'])
55 serializer = SliceMembershipSerializer(slice_membership)
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)
62 delete_slice_membership(data['auth'], pk)
63 return Response(status=status.HTTP_204_NO_CONTENT)
64
65
66