From 4b338c220e1dac34a4457041b98cba7a00b65022 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 14 Oct 2013 22:54:52 -0400 Subject: [PATCH] Add code for the REST API to work with Parking lot ratings --- uncc_gameday/gameday/serializers.py | 7 ++++++- uncc_gameday/gameday/urls.py | 3 ++- uncc_gameday/gameday/views.py | 19 ++++++++++++++++--- uncc_gameday/uncc_gameday/one_shot.py | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/uncc_gameday/gameday/serializers.py b/uncc_gameday/gameday/serializers.py index 53593cc..058aaf8 100644 --- a/uncc_gameday/gameday/serializers.py +++ b/uncc_gameday/gameday/serializers.py @@ -7,4 +7,9 @@ class ParkingLotSerializer(serializers.ModelSerializer): class Meta: model = ParkingLot - fields = ('location', 'filled_pct') \ No newline at end of file + fields = ('location', 'filled_pct') + +class ParkingRatingSerializer(serializers.ModelSerializer): + class Meta: + model = ParkingRating + fields = ('rating', 'parking_lot',) diff --git a/uncc_gameday/gameday/urls.py b/uncc_gameday/gameday/urls.py index c021d93..e48a2b4 100644 --- a/uncc_gameday/gameday/urls.py +++ b/uncc_gameday/gameday/urls.py @@ -2,5 +2,6 @@ from django.conf.urls import url, patterns import views urlpatterns = patterns('', - url('^lots/$', views.ParkingLotList.as_view()) + url('^lots/$', views.ParkingLotList.as_view()), + url('^rate/$', views.RateLot.as_view()) ) \ No newline at end of file diff --git a/uncc_gameday/gameday/views.py b/uncc_gameday/gameday/views.py index 4364652..da6d710 100755 --- a/uncc_gameday/gameday/views.py +++ b/uncc_gameday/gameday/views.py @@ -1,8 +1,9 @@ -from models import ParkingLot -from serializers import ParkingLotSerializer +from models import ParkingLot, ParkingRating +from serializers import * from rest_framework.views import APIView from rest_framework.response import Response +from rest_framework import status class ParkingLotList(APIView): """ @@ -12,4 +13,16 @@ class ParkingLotList(APIView): def get(self, request): parking_lots = ParkingLot.objects.all() serializer = ParkingLotSerializer(parking_lots, many=True) - return Response(serializer.data) \ No newline at end of file + 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) \ No newline at end of file diff --git a/uncc_gameday/uncc_gameday/one_shot.py b/uncc_gameday/uncc_gameday/one_shot.py index d6e03e8..37b0713 100644 --- a/uncc_gameday/uncc_gameday/one_shot.py +++ b/uncc_gameday/uncc_gameday/one_shot.py @@ -4,4 +4,4 @@ from gameday.models import ParkingLot if ParkingLot.objects.all().count() == 0: for location in ParkingLot.LOT_CHOICES: - ParkingLot.objects.create(location=location) \ No newline at end of file + ParkingLot.objects.create(location=location[0]) \ No newline at end of file