Pārlūkot izejas kodu

Use strict handling with PATCH by default.

Stefano Cossu 5 gadi atpakaļ
vecāks
revīzija
12f074f265

+ 6 - 3
lakesuperior/api/resource.py

@@ -237,19 +237,22 @@ def create_or_replace(uid, **kwargs):
 
 
 @transaction(True)
-def update(uid, update_str, is_metadata=False):
+def update(uid, update_str, is_metadata=False, handling='strict'):
     """
     Update a resource with a SPARQL-Update string.
 
     :param string uid: Resource UID.
     :param string update_str: SPARQL-Update statements.
     :param bool is_metadata: Whether the resource metadata are being updated.
+    :param str handling: How to handle servre-managed triples. ``strict``
+        (the default) rejects the update with an exception if server-managed
+        triples are being changed. ``lenient`` modifies the update graph so
+        offending triples are removed and the update can be applied.
 
     :raise InvalidResourceError: If ``is_metadata`` is False and the resource
         being updated is a LDP-NR.
     """
-    # FCREPO is lenient here and Hyrax requires it.
-    rsrc = LdpFactory.from_stored(uid, handling='lenient')
+    rsrc = LdpFactory.from_stored(uid, handling=handling)
     if LDP_NR_TYPE in rsrc.ldp_types and not is_metadata:
         raise InvalidResourceError(
                 'Cannot use this method to update an LDP-NR content.')

+ 3 - 1
lakesuperior/endpoints/ldp.py

@@ -410,6 +410,8 @@ def patch_resource(uid, is_metadata=False):
     if cond_ret:
         return cond_ret
 
+    handling, _ = set_post_put_params()
+
     rsp_headers = {'Content-Type' : 'text/plain; charset=utf-8'}
     if request.mimetype != 'application/sparql-update':
         return 'Provided content type is not a valid parsable format: {}'\
@@ -418,7 +420,7 @@ def patch_resource(uid, is_metadata=False):
     update_str = request.get_data().decode('utf-8')
     local_update_str = g.tbox.localize_ext_str(update_str, nsc['fcres'][uid])
     try:
-        rsrc = rsrc_api.update(uid, local_update_str, is_metadata)
+        rsrc = rsrc_api.update(uid, local_update_str, is_metadata, handling)
     except (ServerManagedTermError, SingleSubjectError) as e:
         return str(e), 412
     except InvalidResourceError as e:

+ 32 - 0
tests/3_endpoints/test_3_0_ldp.py

@@ -544,11 +544,27 @@ class TestLdp:
             headers={'content-type': 'application/sparql-update'}
         ).status_code == 412
 
+        assert self.client.patch(
+            path, data=ins_qry1,
+            headers={
+                'content-type': 'application/sparql-update',
+                'prefer': 'handling=lenient',
+            }
+        ).status_code == 204
+
         assert self.client.patch(
             path, data=ins_qry2,
             headers={'content-type': 'application/sparql-update'}
         ).status_code == 412
 
+        assert self.client.patch(
+            path, data=ins_qry2,
+            headers={
+                'content-type': 'application/sparql-update',
+                'prefer': 'handling=lenient',
+            }
+        ).status_code == 204
+
         assert self.client.patch(
             path, data=ins_qry3,
             headers={'content-type': 'application/sparql-update'}
@@ -559,11 +575,27 @@ class TestLdp:
             headers={'content-type': 'application/sparql-update'}
         ).status_code == 412
 
+        assert self.client.patch(
+            path, data=del_qry1,
+            headers={
+                'content-type': 'application/sparql-update',
+                'prefer': 'handling=lenient',
+            }
+        ).status_code == 204
+
         assert self.client.patch(
             path, data=del_qry2,
             headers={'content-type': 'application/sparql-update'}
         ).status_code == 412
 
+        assert self.client.patch(
+            path, data=ins_qry2,
+            headers={
+                'content-type': 'application/sparql-update',
+                'prefer': 'handling=lenient',
+            }
+        ).status_code == 204
+
         assert self.client.patch(
             path, data=del_qry3,
             headers={'content-type': 'application/sparql-update'}