Quellcode durchsuchen

Auto-generate version UID if not provided; change term from "label" to "ver_uid"; fix incorrect header on version POST.

Stefano Cossu vor 7 Jahren
Ursprung
Commit
e191fce745

+ 13 - 0
doc/notes/fcrepo4_deltas.md

@@ -83,6 +83,19 @@ in LAKEsuperior in a 404.
 In both above cases, PUTting into `rest/a` yields a 409, POSTing to it results
 in a 201.
 
+## Non-mandatory slug in version POST
+
+FCREPO requires a `Slug` header to POST to `fcr:versions` to create a new
+version.
+
+LAKEsuperior adheres to the more general FCREPO POST rule and if no slug is
+provided, an automatic ID is generated instead. The ID is a UUID4.
+
+Also, note that internally this ID is not called "label" but "uid" since it is
+treated as a fully qualified identifier. The `fcrepo:hasVersionLabel` predicate
+has been kept as not worth changing until the adoption of Memento, which will
+change the retrieval mechanisms.
+
 
 ## Deprecation track
 

+ 19 - 7
lakesuperior/endpoints/ldp.py

@@ -190,6 +190,9 @@ def post_resource(parent):
 
 @ldp.route('/<path:uuid>/fcr:versions', methods=['GET'])
 def get_version_info(uuid):
+    '''
+    Get version info (`fcr:versions`).
+    '''
     try:
         rsp = Ldpr(uuid).version_info
     except ResourceNotExistsError as e:
@@ -202,10 +205,16 @@ def get_version_info(uuid):
         return rsp.serialize(format='turtle'), 200
 
 
-@ldp.route('/<path:uuid>/fcr:versions/<label>', methods=['GET'])
-def get_version(uuid, label):
+@ldp.route('/<path:uuid>/fcr:versions/<ver_uid>', methods=['GET'])
+def get_version(uuid, ver_uid):
+    '''
+    Get an individual resource version.
+
+    @param uuid (string) Resource UUID.
+    @param ver_uid (string) Version UID.
+    '''
     try:
-        rsp = Ldpr(uuid).get_version(label)
+        rsp = Ldpr(uuid).get_version(ver_uid)
     except ResourceNotExistsError as e:
         return str(e), 404
     except InvalidResourceError as e:
@@ -218,12 +227,15 @@ def get_version(uuid, label):
 
 @ldp.route('/<path:uuid>/fcr:versions', methods=['POST'])
 def post_version(uuid):
-    label = request.headers.get('slug', None)
-    if not label:
-        return 'Specify label for version.', 400
+    '''
+    Create a new resource version.
+    '''
+    ver_uid = request.headers.get('slug', None)
+    if not ver_uid:
+        ver_uid = str(uuid4())
 
     try:
-        ver_uri = Ldpr.outbound_inst(uuid).create_version(label)
+        ver_uri = Ldpr.outbound_inst(uuid).create_version(ver_uid)
     except ResourceNotExistsError as e:
         return str(e), 404
     except InvalidResourceError as e:

+ 0 - 1
lakesuperior/model/ldp_rs.py

@@ -64,7 +64,6 @@ class Ldpc(LdpRs):
 
 
 
-
 class LdpBc(Ldpc):
     '''LDP-BC (LDP Basic Container).'''
     def __init__(self, uuid, *args, **kwargs):

+ 5 - 5
lakesuperior/model/ldpr.py

@@ -556,7 +556,7 @@ class Ldpr(metaclass=ABCMeta):
 
 
     @atomic
-    def create_version(self, label):
+    def create_version(self, ver_uid):
         '''
         Create a new version of the resource.
 
@@ -564,13 +564,13 @@ class Ldpr(metaclass=ABCMeta):
         to the added `hasVersion` triple and possibly to the `hasVersions` one)
         but not for the version being created.
 
-        @param label Version label. If already existing, an exception is
+        @param ver_uid Version ver_uid. If already existing, an exception is
         raised.
         '''
         # Create version resource from copying the current state.
         ver_add_gr = Graph()
         vers_uuid = '{}/{}'.format(self.uuid, self.RES_VER_CONT_LABEL)
-        ver_uuid = '{}/{}'.format(vers_uuid, label)
+        ver_uuid = '{}/{}'.format(vers_uuid, ver_uid)
         ver_urn = nsc['fcres'][ver_uuid]
         ver_add_gr.add((ver_urn, RDF.type, nsc['fcrepo'].Version))
         for t in self.imr.graph:
@@ -603,7 +603,7 @@ class Ldpr(metaclass=ABCMeta):
         meta_add_gr.add(
                 (ver_urn, nsc['fcrepo'].created, g.timestamp_term))
         meta_add_gr.add(
-                (ver_urn, nsc['fcrepo'].hasVersionLabel, Literal(label)))
+                (ver_urn, nsc['fcrepo'].hasVersionLabel, Literal(ver_uid)))
 
         self.rdfly.modify_dataset(
                 add_trp=meta_add_gr, types={nsc['fcrepo'].Metadata})
@@ -615,7 +615,7 @@ class Ldpr(metaclass=ABCMeta):
 
         self._modify_rsrc(self.RES_UPDATED, add_trp=rsrc_add_gr, notify=False)
 
-        return g.tbox.uuid_to_uri(vers_uuid)
+        return g.tbox.uuid_to_uri(ver_uuid)
 
 
     ## PROTECTED METHODS ##

+ 6 - 5
lakesuperior/store_layouts/ldp_rs/default_layout.py

@@ -116,7 +116,7 @@ class DefaultLayout(BaseRdfLayout):
 
     def get_version_info(self, urn):
         '''
-        See base_rdf_layout.get_vesion_info.
+        See base_rdf_layout.get_version_info.
         '''
         q = '''
         CONSTRUCT {
@@ -137,9 +137,9 @@ class DefaultLayout(BaseRdfLayout):
             return rsp.graph
 
 
-    def get_version(self, urn, label):
+    def get_version(self, urn, ver_uid):
         '''
-        See base_rdf_layout.get_vesion.
+        See base_rdf_layout.get_version.
         '''
         q = '''
         CONSTRUCT {
@@ -147,14 +147,15 @@ class DefaultLayout(BaseRdfLayout):
         } WHERE {
           GRAPH fcg:metadata {
             ?s fcrepo:hasVersion ?v .
-            ?v fcrepo:hasVersionLabel ?l .
+            ?v fcrepo:hasVersionLabel ?uid .
           }
           GRAPH fcg:historic {
             ?v ?p ?o .
           }
         }
         '''
-        rsp = self.ds.query(q, initBindings={'s': urn, 'l': Literal(label)})
+        rsp = self.ds.query(q, initBindings={
+            's': urn, 'uid': Literal(ver_uid)})
         if not len(rsp):
             raise ResourceNotExistsError(
                     urn,