Add new serialization code

This commit is contained in:
Bradlee Speice 2013-10-14 22:31:36 -04:00
parent 269795202d
commit 7be5cd6f97
8 changed files with 51 additions and 7 deletions

3
.gitignore vendored
View File

@ -1,5 +1,8 @@
*.pyc *.pyc
*.sqlite
*.sublime*
bin/ bin/
include/ include/
lib/ lib/
local/ local/
.codeintel/

View File

@ -5,7 +5,7 @@ from datetime import datetime
# Create your models here. # Create your models here.
class RegisteredUser(models.Model): class RegisteredUser(models.Model):
date_registered = models.DateTimeField() date_registered = models.DateTimeField(default=datetime.now)
first_name = models.CharField(max_length=64) first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64) last_name = models.CharField(max_length=64)
section = models.CharField(max_length=8) section = models.CharField(max_length=8)
@ -46,7 +46,7 @@ class ParkingLot(models.Model):
# Only one ParkingLot per location # Only one ParkingLot per location
location = models.CharField(choices=LOT_CHOICES, unique=True, location = models.CharField(choices=LOT_CHOICES, unique=True,
primary_key=True) primary_key=True, max_length=8)
# parkingrating_set # parkingrating_set
@property @property
@ -59,8 +59,7 @@ class ParkingLot(models.Model):
# Get the current rating of this parking lot # Get the current rating of this parking lot
NUM_LOTS = 10 NUM_LOTS = 10
if self.current_ratings.count() < NUM_LOTS: if self.current_ratings.count() < NUM_LOTS:
# Probably raise an exception here, not enough responses yet return None
pass
else: else:
total = sum([ ParkingRating.RATING_WEIGHTS[r.rating] total = sum([ ParkingRating.RATING_WEIGHTS[r.rating]
for r in self.parkingrating_set.all().order_by('created')[0:10] ]) 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) 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) parking_lot = models.ForeignKey(ParkingLot)

View File

@ -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')

View File

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

View File

@ -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)

View File

@ -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)

View File

@ -119,12 +119,14 @@ INSTALLED_APPS = (
#'django.contrib.sessions', #'django.contrib.sessions',
#'django.contrib.sites', #'django.contrib.sites',
#'django.contrib.messages', #'django.contrib.messages',
#'django.contrib.staticfiles', 'django.contrib.staticfiles',
# Uncomment the next line to enable the admin: # Uncomment the next line to enable the admin:
# 'django.contrib.admin', # 'django.contrib.admin',
# Uncomment the next line to enable admin documentation: # Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs', # 'django.contrib.admindocs',
'rest_framework', 'rest_framework',
'gameday',
) )
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer' SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'

View File

@ -1,5 +1,8 @@
from django.conf.urls import patterns, include, url 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: # Uncomment the next two lines to enable the admin:
# from django.contrib import admin # from django.contrib import admin
# admin.autodiscover() # admin.autodiscover()