From 7be5cd6f979a704a1caffac3dd915a22595c5972 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 14 Oct 2013 22:31:36 -0400 Subject: [PATCH] Add new serialization code --- .gitignore | 3 +++ uncc_gameday/gameday/models.py | 9 ++++----- uncc_gameday/gameday/serializers.py | 10 ++++++++++ uncc_gameday/gameday/urls.py | 6 ++++++ uncc_gameday/gameday/views.py | 16 +++++++++++++++- uncc_gameday/uncc_gameday/one_shot.py | 7 +++++++ uncc_gameday/uncc_gameday/settings.py | 4 +++- uncc_gameday/uncc_gameday/urls.py | 3 +++ 8 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 uncc_gameday/gameday/serializers.py create mode 100644 uncc_gameday/gameday/urls.py create mode 100644 uncc_gameday/uncc_gameday/one_shot.py diff --git a/.gitignore b/.gitignore index ee0df3b..266ba65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ *.pyc +*.sqlite +*.sublime* bin/ include/ lib/ local/ +.codeintel/ \ No newline at end of file diff --git a/uncc_gameday/gameday/models.py b/uncc_gameday/gameday/models.py index 3357d19..1f5351f 100755 --- a/uncc_gameday/gameday/models.py +++ b/uncc_gameday/gameday/models.py @@ -5,7 +5,7 @@ from datetime import datetime # Create your models here. class RegisteredUser(models.Model): - date_registered = models.DateTimeField() + date_registered = models.DateTimeField(default=datetime.now) first_name = models.CharField(max_length=64) last_name = models.CharField(max_length=64) section = models.CharField(max_length=8) @@ -46,7 +46,7 @@ class ParkingLot(models.Model): # Only one ParkingLot per location location = models.CharField(choices=LOT_CHOICES, unique=True, - primary_key=True) + primary_key=True, max_length=8) # parkingrating_set @property @@ -59,8 +59,7 @@ class ParkingLot(models.Model): # Get the current rating of this parking lot NUM_LOTS = 10 if self.current_ratings.count() < NUM_LOTS: - # Probably raise an exception here, not enough responses yet - pass + return None else: total = sum([ ParkingRating.RATING_WEIGHTS[r.rating] for r in self.parkingrating_set.all().order_by('created')[0:10] ]) @@ -86,5 +85,5 @@ class ParkingRating(models.Model): } rating = models.CharField(max_length=10, choices=RATING_CHOICES) - created = models.DateTimeField(default=datetime.datetime.now) + created = models.DateTimeField(default=datetime.now) parking_lot = models.ForeignKey(ParkingLot) diff --git a/uncc_gameday/gameday/serializers.py b/uncc_gameday/gameday/serializers.py new file mode 100644 index 0000000..53593cc --- /dev/null +++ b/uncc_gameday/gameday/serializers.py @@ -0,0 +1,10 @@ +from django.forms import widgets +from rest_framework import serializers +from models import * + +class ParkingLotSerializer(serializers.ModelSerializer): + filled_pct = serializers.IntegerField(read_only=True, source='get_rating') + + class Meta: + model = ParkingLot + fields = ('location', 'filled_pct') \ No newline at end of file diff --git a/uncc_gameday/gameday/urls.py b/uncc_gameday/gameday/urls.py new file mode 100644 index 0000000..c021d93 --- /dev/null +++ b/uncc_gameday/gameday/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls import url, patterns +import views + +urlpatterns = patterns('', + url('^lots/$', views.ParkingLotList.as_view()) +) \ No newline at end of file diff --git a/uncc_gameday/gameday/views.py b/uncc_gameday/gameday/views.py index 60f00ef..4364652 100755 --- a/uncc_gameday/gameday/views.py +++ b/uncc_gameday/gameday/views.py @@ -1 +1,15 @@ -# Create your views here. +from models import ParkingLot +from serializers import ParkingLotSerializer + +from rest_framework.views import APIView +from rest_framework.response import Response + +class ParkingLotList(APIView): + """ + List all parking lots + """ + + 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 diff --git a/uncc_gameday/uncc_gameday/one_shot.py b/uncc_gameday/uncc_gameday/one_shot.py new file mode 100644 index 0000000..d6e03e8 --- /dev/null +++ b/uncc_gameday/uncc_gameday/one_shot.py @@ -0,0 +1,7 @@ +# Any code below will be run exactly once on import. +# Basically, we run whenever the root URLconf is loaded. + +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 diff --git a/uncc_gameday/uncc_gameday/settings.py b/uncc_gameday/uncc_gameday/settings.py index 2e352ef..d79662a 100755 --- a/uncc_gameday/uncc_gameday/settings.py +++ b/uncc_gameday/uncc_gameday/settings.py @@ -119,12 +119,14 @@ INSTALLED_APPS = ( #'django.contrib.sessions', #'django.contrib.sites', #'django.contrib.messages', - #'django.contrib.staticfiles', + 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'rest_framework', + + 'gameday', ) SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer' diff --git a/uncc_gameday/uncc_gameday/urls.py b/uncc_gameday/uncc_gameday/urls.py index 7e47edb..8ef461e 100755 --- a/uncc_gameday/uncc_gameday/urls.py +++ b/uncc_gameday/uncc_gameday/urls.py @@ -1,5 +1,8 @@ from django.conf.urls import patterns, include, url +# Bit of a hack, but run any one-time code +import one_shot + # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover()