mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-11-04 02:10:32 -05:00 
			
		
		
		
	Add initial (completely untested) server API
This commit is contained in:
		
							
								
								
									
										26
									
								
								UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								UNCCScavenger-Server/UNCCScavenger_Server/scavenger/forms.py
									
									
									
									
									
										Normal 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')
 | 
				
			||||||
@ -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')
 | 
				
			||||||
@ -1 +1,27 @@
 | 
				
			|||||||
# Create your views here.
 | 
					# 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)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user