views.py 1.1 KB
Newer Older
Ben Waters's avatar
Ben Waters committed
1
from rest_framework import viewsets
Ben Waters's avatar
Ben Waters committed
2

Ben Waters's avatar
Ben Waters committed
3 4
from rest_framework.response import Response
from rest_framework import status
Ben Waters's avatar
Ben Waters committed
5 6 7 8
from rest_framework.decorators import detail_route

from .models import Location, LocationSerializer, UserLocation, UserLocationSerializer

Ben Waters's avatar
Ben Waters committed
9

Ben Waters's avatar
Ben Waters committed
10 11

# Create your views here.
Ben Waters's avatar
Ben Waters committed
12
class LocationViewSet(viewsets.ModelViewSet):
Ben Waters's avatar
Ben Waters committed
13
    queryset = Location.objects.all()
Ben Waters's avatar
Ben Waters committed
14
    serializer_class = LocationSerializer
Ben Waters's avatar
Ben Waters committed
15

Ben Waters's avatar
Ben Waters committed
16 17 18
    @detail_route(methods=['post'])
    def get_location(self,request,pk=None):
        location = self.get_object()
Ben Waters's avatar
Ben Waters committed
19 20
        serializer = LocationSerializer(data=request.DATA)
        if serializer.is_valid():
Ben Waters's avatar
Ben Waters committed
21 22 23 24 25 26 27 28
            lat = serializer.data['lat']
            long = serializer.data['long']
            location = Location.objects.filter()
            out_serializer = LocationSerializer(location)
            return Response(out_serializer.data)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Ben Waters's avatar
Ben Waters committed
29

Ben Waters's avatar
Ben Waters committed
30 31 32
class UserLocationViewSet(viewsets.ModelViewSet):
    queryset = UserLocation.objects.all()
    serializer_class = UserLocationSerializer
Ben Waters's avatar
Ben Waters committed
33