Tony Mack | 29c287f | 2013-04-11 21:07:16 -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.site_privileges import add_site_privilege, delete_site_privilege, get_site_privileges, update_site_privilege |
| 7 | from core.serializers import SitePrivilegeSerializer |
| 8 | from util.request import parse_request |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class SitePrivilegeListCreate(APIView): |
| 12 | """ |
| 13 | List all site_privileges or create a new site_privilege. |
| 14 | """ |
| 15 | |
| 16 | def post(self, request, format = None): |
| 17 | data = parse_request(request.DATA) |
| 18 | if 'auth' not in data: |
| 19 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 20 | elif 'site_privilege' in data: |
| 21 | site_privilege = add_site_privilege(data['auth'], data['site_privilege']) |
| 22 | serializer = SitePrivilegeSerializer(site_privilege) |
| 23 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 24 | else: |
| 25 | site_privileges = get_site_privileges(data['auth']) |
| 26 | serializer = SitePrivilegeSerializer(site_privileges, many=True) |
| 27 | return Response(serializer.data) |
| 28 | |
| 29 | |
| 30 | class SitePrivilegeRetrieveUpdateDestroy(APIView): |
| 31 | """ |
| 32 | Retrieve, update or delete a site_privilege |
| 33 | """ |
| 34 | |
| 35 | def post(self, request, pk, format=None): |
| 36 | """Retrieve a site_privilege""" |
| 37 | data = parse_request(request.DATA) |
| 38 | if 'auth' not in data: |
| 39 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 40 | site_privileges = get_site_privileges(data['auth'], pk) |
| 41 | if not site_privileges: |
| 42 | return Response(status=status.HTTP_404_NOT_FOUND) |
| 43 | serializer = SitePrivilegeSerializer(site_privileges[0]) |
| 44 | return Response(serializer.data) |
| 45 | |
| 46 | def put(self, request, pk, format=None): |
| 47 | """update a site_privilege""" |
| 48 | data = parse_request(request.DATA) |
| 49 | if 'auth' not in data: |
| 50 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 51 | elif 'site_privilege' not in data: |
| 52 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 53 | |
| 54 | site_privilege = update_site_privilege(pk, data['site_privilege']) |
| 55 | serializer = SitePrivilegeSerializer(site_privilege) |
| 56 | return Response(serializer.data) |
| 57 | |
| 58 | def delete(self, request, pk, format=None): |
| 59 | data = parse_request(request.DATA) |
| 60 | if 'auth' not in data: |
| 61 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 62 | delete_site_privilege(data['auth'], pk) |
| 63 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 64 | |
| 65 | |
| 66 | |