def polygon_list(request): """ List all polygons, or create a polygon. """ if request.method == 'GET': snippets = Polygon.objects.all() serializer = PolygonSerializer(snippets, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = PolygonSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def polygon_detail(request, pk): """ Retrieve, update or delete a polygon. """ try: snippet = Polygon.objects.get(pk=pk) except Polygon.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = PolygonSerializer(snippet) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = PolygonSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204)