2013-10-14 22:54:52 -04:00
|
|
|
from models import ParkingLot, ParkingRating
|
|
|
|
from serializers import *
|
2013-10-14 22:31:36 -04:00
|
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
2013-10-14 22:54:52 -04:00
|
|
|
from rest_framework import status
|
2013-10-14 23:58:15 -04:00
|
|
|
from rest_framework.decorators import api_view
|
|
|
|
|
|
|
|
from django.core.urlresolvers import reverse
|
2013-10-18 18:40:05 -04:00
|
|
|
from django.shortcuts import get_object_or_404
|
2013-10-18 22:42:08 -04:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2013-10-14 23:58:15 -04:00
|
|
|
|
|
|
|
@api_view(('GET',))
|
|
|
|
def api_root(request):
|
|
|
|
'Give some information about our API'
|
|
|
|
return Response({
|
|
|
|
'parking_lots': request.build_absolute_uri(reverse('parking-lots')),
|
|
|
|
'parking_rating': request.build_absolute_uri(reverse('parking-rating')),
|
|
|
|
})
|
2013-10-14 22:31:36 -04:00
|
|
|
|
|
|
|
class ParkingLotList(APIView):
|
|
|
|
"""
|
|
|
|
List all parking lots
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
parking_lots = ParkingLot.objects.all()
|
|
|
|
serializer = ParkingLotSerializer(parking_lots, many=True)
|
2013-10-14 22:54:52 -04:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
2013-10-18 18:10:28 -04:00
|
|
|
class SingleParkingLotList(APIView):
|
|
|
|
"""
|
|
|
|
List a single parking lot
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, lot):
|
2013-10-18 18:40:05 -04:00
|
|
|
parking_lot = get_object_or_404(ParkingLot, location=lot)
|
|
|
|
return Response(ParkingLotSerializer(parking_lot).data)
|
2013-10-18 18:10:28 -04:00
|
|
|
|
2013-10-14 22:54:52 -04:00
|
|
|
class RateLot(APIView):
|
|
|
|
"""
|
2013-10-14 23:10:09 -04:00
|
|
|
Rate a parking lot
|
|
|
|
**GET**: Get the rating choice options
|
|
|
|
**POST**: Rate a parking lot
|
2013-10-14 22:54:52 -04:00
|
|
|
"""
|
|
|
|
|
2013-10-18 22:42:08 -04:00
|
|
|
@csrf_exempt
|
2013-10-14 22:54:52 -04:00
|
|
|
def post(self, request):
|
2013-10-14 23:10:09 -04:00
|
|
|
'Rate a parking lot'
|
2013-10-14 22:54:52 -04:00
|
|
|
rating = ParkingRatingSerializer(data=request.DATA)
|
|
|
|
if rating.is_valid():
|
|
|
|
rating.save()
|
|
|
|
return Response(rating.data)
|
2013-10-14 23:10:09 -04:00
|
|
|
return Response(rating.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
'Get the rating choice options'
|
2013-10-18 18:10:28 -04:00
|
|
|
return Response(ParkingRating.RATING_CHOICES)
|
2013-10-19 20:31:08 -04:00
|
|
|
|
|
|
|
class RegisterUser(APIView):
|
|
|
|
"""
|
|
|
|
Handle Registration of users
|
|
|
|
**GET**: Get the current information of a registered user by ID
|
|
|
|
**POST**: Register a new user
|
|
|
|
"""
|
|
|
|
def get(self, request):
|
|
|
|
'Get the first and last names of all registered users'
|
|
|
|
users = RegisteredUser.objects.all()
|
|
|
|
users_s = SingleUserSerializer(users, many=True)
|
|
|
|
return Response(users_s.data)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
'Register a new user'
|
|
|
|
user = UserSerializer(data=request.DATA)
|
|
|
|
if user.is_valid():
|
|
|
|
user.save()
|
|
|
|
return Response(user.data)
|
|
|
|
return Response(user.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
class ListRegisteredUsers(APIView):
|
|
|
|
"""
|
|
|
|
List all registered users.
|
|
|
|
We have a separate view set up so we can list all information about one
|
|
|
|
user at a time (this one), and one view to get just id, first_name,
|
|
|
|
and last_name of all users.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, id):
|
|
|
|
'Get the information for a registered user'
|
|
|
|
user = get_object_or_404(RegisteredUser, id=id)
|
|
|
|
return Response(UserSerializer(user).data)
|