예제 #1
0
def map_list(request):
    if request.method == 'GET':
        maps = Map.objects.all()
        serializer = MapSerializerFullResponse(maps, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = MapSerializer(data=data)
        if serializer.is_valid():
            serializer.save()  # POST the data as a serialized JSON object
            map_id = serializer.data.get('id')
            update_or_create_points(map_id, data['points'])
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
def map_detail(request, pk):  # Creating a view for viewing, updating and deleting a map object
    try:
        map = Map.objects.get(pk=pk)
    except Map.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        serializer = MapSerializerFullResponse(map)
        return Response(serializer.data)
    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = MapSerializer(map, data=data)
        if serializer.is_valid():
            serializer.save()  # POST the data as a serialized JSON object
            map_id = serializer.data.get('id')
            update_or_create_points(map_id, data['points'])
            return Response(serializer.data,status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        map.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)