Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -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 | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 6 | from plstackapi.planetstack.api.sites import add_site, delete_site, get_sites |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 7 | from plstackapi.planetstack.serializers import SiteSerializer |
| 8 | from plstackapi.util.request import parse_request |
| 9 | |
| 10 | |
| 11 | class SiteListCreate(APIView): |
| 12 | """ |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 13 | List all sites or create a new site. |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 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' in data: |
| 21 | site = add_site(data['auth'], data['site']) |
Tony Mack | 7f2a228 | 2013-04-09 00:34:54 -0400 | [diff] [blame] | 22 | serializer = SiteSerializer(site) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 23 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 24 | else: |
| 25 | sites = get_sites(data['auth']) |
| 26 | serializer = SiteSerializer(sites, many=True) |
| 27 | return Response(serializer.data) |
| 28 | |
| 29 | |
| 30 | class SiteRetrieveUpdateDestroy(APIView): |
| 31 | """ |
| 32 | Retrieve, update or delete a site |
| 33 | """ |
| 34 | |
| 35 | def post(self, request, pk, format=None): |
| 36 | """Retrieve a site""" |
| 37 | data = parse_request(request.DATA) |
| 38 | if 'auth' not in data: |
| 39 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 40 | sites = get_sites(data['auth'], {'login_base': pk}) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 41 | if not sites: |
| 42 | return Response(status=status.HTTP_404_NOT_FOUND) |
| 43 | serializer = SiteSerializer(sites[0]) |
| 44 | return Response(serializer.data) |
| 45 | |
| 46 | def put(self, request, pk, format=None): |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 47 | """update a site""" |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 48 | data = parse_request(request.DATA) |
| 49 | if 'auth' not in data: |
| 50 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 51 | elif 'site' not in data: |
| 52 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 53 | |
| 54 | site = update_site(pk, data['site']) |
| 55 | serializer = SiteSerializer(data=site) |
| 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) |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 62 | delete_site(data['auth'], {'login_base': pk}) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 63 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 64 | |
| 65 | |
| 66 | |