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 | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class RoleListCreate(APIView): |
| 11 | """ |
| 12 | List all roles or create a new role. |
| 13 | """ |
| 14 | |
| 15 | def post(self, request, format = None): |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 16 | |
Tony Mack | dca8dab | 2013-04-08 00:24:43 -0400 | [diff] [blame] | 17 | if 'auth' not in request.DATA: |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 18 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | ed069ac | 2013-04-08 00:26:13 -0400 | [diff] [blame^] | 19 | elif 'name' in request.DATA: |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 20 | role = add_role(request.DATA['auth'], request.DATA['name']) |
| 21 | serializer = RoleSerializer(data=role) |
| 22 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 23 | else: |
| 24 | roles = get_roles(request.DATA['auth']) |
| 25 | serializer = RoleSerializer(roles, many=True) |
| 26 | return Response(Serializer.data) |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
Tony Mack | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 32 | |