Add initial (completely untested) server API

master
Bradlee Speice 2014-03-28 18:31:06 -04:00
parent e26bf2419e
commit 09512c03d4
3 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,26 @@
from scavenger.models import Location
from django import forms
from django.core.exceptions import ObjectDoesNotExist
class LocationValidator(forms.ModelForm):
'''
Validate that the user actually found a location
'''
def is_valid(self):
valid = super(LocationValidator, self).is_valid()
if not valid:
return valid
# Make sure the key and ID submitted match
try:
self._instance = Location.objects.get(id=self.cleaned_data['id'],
key=self.cleaned_data['key'])
return True
except ObjectDoesNotExist, e:
return False
class Meta:
model = Location
fields = ('id', 'key')

View File

@ -0,0 +1,13 @@
from rest_framework.serializers import ModelSerializer
from models import Location
class LocationListSerializer(ModelSerializer):
class Meta:
model = Location
fields = ('id', 'name', 'riddle', 'location')
class LocationResultSerializer(ModelSerializer):
class Meta:
model = Location
fields = ('id', 'name', 'result')

View File

@ -1 +1,27 @@
# Create your views here.
from scavenger.models import Location
from scavenger.serializers import LocationListSerializer
from scavenger.forms import LocationValidator
from rest_framework.views import APIView
from rest_framework.response import Response
class LocationList(APIView):
'''
List all locations available for the scavenger hunt.
'''
def get(self, request, format=None):
locations = Location.objects.all()
locations_serializer = LocationListSerializer(locations, many=True)
return Response(serializer.data)
class LocationResult(APIView):
'''
Show the result for a specific location
'''
def get(self, request, format=None):
l_form = LocationValidator(data=request.data)
if l_form.is_valid():
location = l_form._instance
return Response(location.result)
else:
return Response("You found the wrong code!", status=400)