Tony Mack | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 1 | from django.http import Http404 |
| 2 | from rest_framework.views import APIView |
| 3 | from rest_framework.response import Response |
| 4 | from rest_framework import status |
| 5 | |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 6 | from plstackapi.planetstack.api.roles import add_role, delete_role, get_roles |
| 7 | from plstackapi.planetstack.serializers import RoleSerializer |
Tony Mack | e9d64ca | 2013-04-08 01:14:32 -0400 | [diff] [blame^] | 8 | from plstackapi.util.json import parse_request |
Tony Mack | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class RoleListCreate(APIView): |
| 12 | """ |
| 13 | List all roles or create a new role. |
| 14 | """ |
| 15 | |
| 16 | def post(self, request, format = None): |
Tony Mack | e9d64ca | 2013-04-08 01:14:32 -0400 | [diff] [blame^] | 17 | data = parse_request(request.DATA) |
| 18 | if 'auth' not in data: |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 19 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | e9d64ca | 2013-04-08 01:14:32 -0400 | [diff] [blame^] | 20 | elif 'name' in data: |
| 21 | role = add_role(data['auth'], data['name']) |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 22 | serializer = RoleSerializer(data=role) |
| 23 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 24 | else: |
Tony Mack | e9d64ca | 2013-04-08 01:14:32 -0400 | [diff] [blame^] | 25 | roles = get_roles(data['auth']) |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 26 | serializer = RoleSerializer(roles, many=True) |
| 27 | return Response(Serializer.data) |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
Tony Mack | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 33 | |