blob: c9b9664a14011dad69cf3206d015318e221ebb3f [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
Tony Mack9f237972013-04-07 23:43:11 -04006from plstackapi.planetstack.api.roles import add_role, delete_role, get_roles
7from plstackapi.planetstack.serializers import RoleSerializer
Tony Mackae1f67c2013-04-08 14:53:49 -04008from plstackapi.util.jsonhelper 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 Macke9d64ca2013-04-08 01:14:32 -040020 elif 'name' in data:
21 role = add_role(data['auth'], data['name'])
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)
27 return Response(Serializer.data)
28
29
30
31
32
Tony Mack78174092013-04-07 20:17:38 -040033