blob: 8b279281e5a4822ec421075ee32bd904a607bb0c [file] [log] [blame]
Tony Mack5cc16ba2013-04-09 10:30:45 -04001from django.http import Http404
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.api.users import add_user, delete_user, get_users, update_user
7from core.serializers import UserSerializer
8from util.request import parse_request
Tony Mack5cc16ba2013-04-09 10:30:45 -04009
10
11class UserListCreate(APIView):
12 """
13 List all users or create a new user.
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 'user' in data:
21 user = add_user(data['auth'], data['user'])
22 serializer = UserSerializer(user)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
25 users = get_users(data['auth'])
26 serializer = UserSerializer(users, many=True)
27 return Response(serializer.data)
28
29
30class UserRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a user
33 """
34
35 def post(self, request, pk, format=None):
36 """Retrieve a user"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
40 users = get_users(data['auth'], {'id': pk})
41 if not users:
42 return Response(status=status.HTTP_404_NOT_FOUND)
43 serializer = UserSerializer(users[0])
44 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """update a user"""
48 data = parse_request(request.DATA)
49 if 'auth' not in data:
50 return Response(status=status.HTTP_400_BAD_REQUEST)
51 elif 'user' not in data:
52 return Response(status=status.HTTP_400_BAD_REQUEST)
53
54 user = update_user(pk, data['user'])
55 serializer = UserSerializer(user)
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_user(data['auth'], {'id': pk})
63 return Response(status=status.HTTP_204_NO_CONTENT)
64
65
66