From 09512c03d47416f5b0aa5aaa704f3a132f7cfb0a Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 28 Mar 2014 18:31:06 -0400 Subject: [PATCH] Add initial (completely untested) server API --- .../UNCCScavenger_Server/scavenger/forms.py | 26 +++++++++++++++++++ .../scavenger/serializers.py | 13 ++++++++++ .../UNCCScavenger_Server/scavenger/views.py | 26 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py create mode 100644 UNCCScavenger-Server/UNCCScavenger_Server/scavenger/serializers.py diff --git a/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py new file mode 100644 index 0000000..5099352 --- /dev/null +++ b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py @@ -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') diff --git a/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/serializers.py b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/serializers.py new file mode 100644 index 0000000..6f509af --- /dev/null +++ b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/serializers.py @@ -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') diff --git a/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/views.py b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/views.py index 60f00ef..d035430 100644 --- a/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/views.py +++ b/UNCCScavenger-Server/UNCCScavenger_Server/scavenger/views.py @@ -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)