Skip to content

Commit

Permalink
Merge pull request #9969 from archesproject/9968_jsonld_PUT_bug
Browse files Browse the repository at this point in the history
fixes bug where not supplying a graphid or slug to the json-ld api would delete the resource but not save the new one, re #9968
  • Loading branch information
chiatt authored Aug 30, 2023
2 parents 857a693 + 491ba7e commit c46a919
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions arches/app/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,27 +545,22 @@ def get(self, request, resourceid=None, slug=None, graphid=None):
return JSONResponse(out, indent=indent)

def put(self, request, resourceid, slug=None, graphid=None):
indent = request.GET.get("indent", None)

allowed_formats = ["arches-json", "json-ld"]
indent = request.GET.get("indent", None)
format = request.GET.get("format", "json-ld")

if format not in allowed_formats:
return JSONResponse(status=406, reason="incorrect format specified, only %s formats allowed" % allowed_formats)

if format == "json-ld" and slug is None and graphid is None:
return JSONResponse({"error": "Need to supply either a graph id or slug in the request url. See the API reference in the developer documentation at https://arches.readthedocs.io for more details"}, status=400)

if not user_can_edit_resource(user=request.user, resourceid=resourceid):
return JSONResponse(status=403)
else:
with transaction.atomic():
try:
if format == "json-ld":
try:
# DELETE
resource_instance = Resource.objects.get(pk=resourceid)
resource_instance.delete()
except models.ResourceInstance.DoesNotExist:
pass

# POST
data = JSONDeserializer().deserialize(request.body)
reader = JsonLdReader()
if slug is not None:
Expand All @@ -580,6 +575,12 @@ def put(self, request, resourceid, slug=None, graphid=None):
response = []
for resource in reader.resources:
with transaction.atomic():
try:
# DELETE
resource_instance = Resource.objects.get(pk=resource.pk)
resource_instance.delete()
except models.ResourceInstance.DoesNotExist:
pass
resource.save(request=request)
response.append(JSONDeserializer().deserialize(self.get(request, resource.resourceinstanceid).content))
return JSONResponse(response, indent=indent, status=201)
Expand Down Expand Up @@ -624,12 +625,16 @@ def put(self, request, resourceid, slug=None, graphid=None):
return JSONResponse({"error": "resource data could not be saved"}, status=500, reason=e)

def post(self, request, resourceid=None, slug=None, graphid=None):
indent = request.POST.get("indent", None)
allowed_formats = ["arches-json", "json-ld"]
indent = request.POST.get("indent", None)
format = request.GET.get("format", "json-ld")

if format not in allowed_formats:
return JSONResponse(status=406, reason="incorrect format specified, only %s formats allowed" % allowed_formats)

if format == "json-ld" and slug is None and graphid is None:
return JSONResponse({"error": "Need to supply either a graph id or slug in the request url. See the API reference in the developer documentation at https://arches.readthedocs.io for more details"}, status=400)

try:
if user_can_edit_resource(user=request.user, resourceid=resourceid):
if format == "json-ld":
Expand Down

0 comments on commit c46a919

Please sign in to comment.