blob: 37bb14933636b572132b76a0dce8b31b4dae6031 [file] [log] [blame]
Tony Mack78174092013-04-07 20:17:38 -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.roles import add_role, delete_role, get_roles
7from core.serializers import RoleSerializer
8from util.request import parse_request
Tony Mack78174092013-04-07 20:17:38 -04009
10
11class RoleListCreate(APIView):
12 """
13 List all roles or create a new role.
14 """
15
16 def post(self, request, format = None):
Tony Macke9d64ca2013-04-08 01:14:32 -040017 data = parse_request(request.DATA)
18 if 'auth' not in data:
Tony Mack9f237972013-04-07 23:43:11 -040019 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack9648f022013-04-08 16:17:11 -040020 elif 'role' in data:
Tony Mack771f4b92013-04-12 01:49:57 -040021 role = add_role(data['auth'], data['role']['role_type'])
Tony Mack9f237972013-04-07 23:43:11 -040022 serializer = RoleSerializer(data=role)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
Tony Macke9d64ca2013-04-08 01:14:32 -040025 roles = get_roles(data['auth'])
Tony Mack9f237972013-04-07 23:43:11 -040026 serializer = RoleSerializer(roles, many=True)
Tony Mackbd4ec342013-04-08 15:57:04 -040027 return Response(serializer.data)
Tony Mack9f237972013-04-07 23:43:11 -040028
29
Tony Mackce61bd72013-04-08 20:37:12 -040030class RoleRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a role
33 """
34
Tony Mack959e7062013-04-08 20:55:36 -040035 def post(self, request, pk, format=None):
Tony Mackce61bd72013-04-08 20:37:12 -040036 """Retrieve a role"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack29c287f2013-04-11 21:07:16 -040040 roles = get_roles(data['auth'], pk)
Tony Mackce61bd72013-04-08 20:37:12 -040041 if not roles:
42 return Response(status=status.HTTP_404_NOT_FOUND)
Tony Mack72870972013-04-08 21:22:36 -040043 serializer = RoleSerializer(roles[0])
Tony Mackce61bd72013-04-08 20:37:12 -040044 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """role update not implemnted"""
48 return Response(status=status.HTTP_404_NOT_FOUND)
49
50 def delete(self, request, pk, format=None):
Tony Mack47ed6482013-04-08 21:36:13 -040051 data = parse_request(request.DATA)
Tony Mackce61bd72013-04-08 20:37:12 -040052 if 'auth' not in data:
53 return Response(status=status.HTTP_400_BAD_REQUEST)
Tony Mack29c287f2013-04-11 21:07:16 -040054 delete_role(data['auth'], pk)
Tony Mackce61bd72013-04-08 20:37:12 -040055 return Response(status=status.HTTP_204_NO_CONTENT)
Tony Mack9f237972013-04-07 23:43:11 -040056
57
Tony Mack78174092013-04-07 20:17:38 -040058