Add code for the REST API to work with Parking lot ratings

This commit is contained in:
Bradlee Speice 2013-10-14 22:54:52 -04:00
parent 7be5cd6f97
commit 4b338c220e
4 changed files with 25 additions and 6 deletions

View File

@ -7,4 +7,9 @@ class ParkingLotSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ParkingLot model = ParkingLot
fields = ('location', 'filled_pct') fields = ('location', 'filled_pct')
class ParkingRatingSerializer(serializers.ModelSerializer):
class Meta:
model = ParkingRating
fields = ('rating', 'parking_lot',)

View File

@ -2,5 +2,6 @@ from django.conf.urls import url, patterns
import views import views
urlpatterns = patterns('', urlpatterns = patterns('',
url('^lots/$', views.ParkingLotList.as_view()) url('^lots/$', views.ParkingLotList.as_view()),
url('^rate/$', views.RateLot.as_view())
) )

View File

@ -1,8 +1,9 @@
from models import ParkingLot from models import ParkingLot, ParkingRating
from serializers import ParkingLotSerializer from serializers import *
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status
class ParkingLotList(APIView): class ParkingLotList(APIView):
""" """
@ -12,4 +13,16 @@ class ParkingLotList(APIView):
def get(self, request): def get(self, request):
parking_lots = ParkingLot.objects.all() parking_lots = ParkingLot.objects.all()
serializer = ParkingLotSerializer(parking_lots, many=True) serializer = ParkingLotSerializer(parking_lots, many=True)
return Response(serializer.data) return Response(serializer.data)
class RateLot(APIView):
"""
Rate a parking lot
"""
def post(self, request):
rating = ParkingRatingSerializer(data=request.DATA)
if rating.is_valid():
rating.save()
return Response(rating.data)
return Response(rating.errors, status=status.HTTP_400_BAD_REQUEST)

View File

@ -4,4 +4,4 @@
from gameday.models import ParkingLot from gameday.models import ParkingLot
if ParkingLot.objects.all().count() == 0: if ParkingLot.objects.all().count() == 0:
for location in ParkingLot.LOT_CHOICES: for location in ParkingLot.LOT_CHOICES:
ParkingLot.objects.create(location=location) ParkingLot.objects.create(location=location[0])