def byCollection(req, gmId, dmuId): gm = get_object_or_404(GeoMap, pk=gmId) dmu = get_object_or_404(DescriptionOfMapUnits, pk=dmuId) if dmu.owningmap != gm: return HttpResponseBadRequest("Invalid mapunit resource") if req.method == "GET": return HttpGeoJsonResponse(StandardLithology.objects.filter(mapunit=dmu), False) elif req.method == "POST": success, response = geoJsonToKwargs(StandardLithology, req.raw_post_data, ["owningmap", "mapunit"]) if not success: return HttpResponseBadRequest(response) response["owningmap"] = gm response["mapunit"] = dmu try: newSl = StandardLithology(**response) newSl.save() return HttpGeoJsonResponse([newSl]) except Exception as ex: return HttpResponseBadRequest(str(ex)) else: return HttpResponseNotAllowed(["GET", "POST"])
def byResource(req, gmId, dmuId, lithId): gm = get_object_or_404(GeoMap, pk=gmId) dmu = get_object_or_404(DescriptionOfMapUnits, pk=dmuId) if dmu.owningmap != gm: return HttpResponseBadRequest("Invalid mapunit resource") if req.method == "GET": sl = get_object_or_404(StandardLithology, pk=lithId) if sl.mapunit != dmu: return HttpResponseBadRequest("Invalid lithology resource") return HttpGeoJsonResponse([sl]) elif req.method == "PUT": success, response = geoJsonToKwargs(StandardLithology, req.raw_post_data, ["owningmap", "mapunit"]) if not success: return HttpResponseBadRequest(response) response["owningmap"] = gm response["mapunit"] = dmu response["id"] = lithId try: newSl = StandardLithology(**response) newSl.save() return HttpGeoJsonResponse([newSl]) except Exception as ex: return HttpResponseBadRequest(str(ex)) elif req.method == "DELETE": sl = get_object_or_404(StandardLithology, pk=lithId) if sl.mapunit != dmu: return HttpResponseBadRequest("Invalid lithology resource") try: sl.delete() except Exception (ex): return HttpResponseBadRequest(str(ex)) else: return HttpResponse(json.dumps({ "success": True }), content_type="application/json") else: return HttpResponseNotAllowed(["GET", "PUT", "DELETE"])