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 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.api.roles import add_role, delete_role, get_roles |
| 7 | from core.serializers import RoleSerializer |
| 8 | from util.request 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 | 9648f02 | 2013-04-08 16:17:11 -0400 | [diff] [blame] | 20 | elif 'role' in data: |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 21 | role = add_role(data['auth'], data['role']['role_type']) |
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) |
Tony Mack | bd4ec34 | 2013-04-08 15:57:04 -0400 | [diff] [blame] | 27 | return Response(serializer.data) |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 28 | |
| 29 | |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 30 | class RoleRetrieveUpdateDestroy(APIView): |
| 31 | """ |
| 32 | Retrieve, update or delete a role |
| 33 | """ |
| 34 | |
Tony Mack | 959e706 | 2013-04-08 20:55:36 -0400 | [diff] [blame] | 35 | def post(self, request, pk, format=None): |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 36 | """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 Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 40 | roles = get_roles(data['auth'], pk) |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 41 | if not roles: |
| 42 | return Response(status=status.HTTP_404_NOT_FOUND) |
Tony Mack | 7287097 | 2013-04-08 21:22:36 -0400 | [diff] [blame] | 43 | serializer = RoleSerializer(roles[0]) |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 44 | 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 Mack | 47ed648 | 2013-04-08 21:36:13 -0400 | [diff] [blame] | 51 | data = parse_request(request.DATA) |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 52 | if 'auth' not in data: |
| 53 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 54 | delete_role(data['auth'], pk) |
Tony Mack | ce61bd7 | 2013-04-08 20:37:12 -0400 | [diff] [blame] | 55 | return Response(status=status.HTTP_204_NO_CONTENT) |
Tony Mack | 9f23797 | 2013-04-07 23:43:11 -0400 | [diff] [blame] | 56 | |
| 57 | |
Tony Mack | 7817409 | 2013-04-07 20:17:38 -0400 | [diff] [blame] | 58 | |