Bladeren bron

Retrieve version and version info.

Stefano Cossu 7 jaren geleden
bovenliggende
commit
25b42241b6

+ 28 - 0
lakesuperior/endpoints/ldp.py

@@ -188,6 +188,34 @@ def post_resource(parent):
     return rsrc.uri, 201, out_headers
 
 
+@ldp.route('/<path:uuid>/fcr:versions', methods=['GET'])
+def get_version_info(uuid):
+    try:
+        rsp = Ldpr(uuid).version_info
+    except ResourceNotExistsError as e:
+        return str(e), 404
+    except InvalidResourceError as e:
+        return str(e), 409
+    except TombstoneError as e:
+        return _tombstone_response(e, uuid)
+    else:
+        return rsp.serialize(format='turtle'), 200
+
+
+@ldp.route('/<path:uuid>/fcr:versions/<label>', methods=['GET'])
+def get_version(uuid, label):
+    try:
+        rsp = Ldpr(uuid).get_version(label)
+    except ResourceNotExistsError as e:
+        return str(e), 404
+    except InvalidResourceError as e:
+        return str(e), 409
+    except TombstoneError as e:
+        return _tombstone_response(e, uuid)
+    else:
+        return rsp.serialize(format='turtle'), 200
+
+
 @ldp.route('/<path:uuid>/fcr:versions', methods=['POST'])
 def post_version(uuid):
     label = request.headers.get('slug', None)

+ 32 - 5
lakesuperior/model/ldpr.py

@@ -389,6 +389,16 @@ class Ldpr(metaclass=ABCMeta):
         return out_gr
 
 
+    @property
+    def version_info(self):
+        '''
+        Return version metadata (`fcr:versions`).
+        '''
+        rsp = self.rdfly.get_version_info(self.urn)
+
+        return g.tbox.globalize_graph(rsp)
+
+
     @property
     def is_stored(self):
         if hasattr(self, '_imr'):
@@ -536,6 +546,15 @@ class Ldpr(metaclass=ABCMeta):
         self.rdfly.modify_dataset(remove_trp)
 
 
+    def get_version(self, label):
+        '''
+        Get a version by label.
+        '''
+        ver_graph = self.rdfly.get_version(self.urn, label)
+
+        return g.tbox.globalize_graph(ver_graph)
+
+
     @atomic
     def create_version(self, label):
         '''
@@ -555,11 +574,19 @@ class Ldpr(metaclass=ABCMeta):
         ver_urn = nsc['fcres'][ver_uuid]
         ver_add_gr.add((ver_urn, RDF.type, nsc['fcrepo'].Version))
         for t in self.imr.graph:
-            if t[1] == RDF.type and t[2] in {
-                nsc['fcrepo'].Resource,
-                nsc['fcrepo'].Container,
-                nsc['fcrepo'].Binary,
-            }:
+            if (
+                t[1] == RDF.type and t[2] in {
+                    nsc['fcrepo'].Binary,
+                    nsc['fcrepo'].Container,
+                    nsc['fcrepo'].Resource,
+                }
+            ) or (
+                t[1] in {
+                    nsc['fcrepo'].hasParent,
+                    nsc['fcrepo'].hasVersions,
+                    nsc['premis'].hasMessageDigest,
+                }
+            ):
                 pass
             else:
                 ver_add_gr.add((

+ 16 - 0
lakesuperior/store_layouts/ldp_rs/base_rdf_layout.py

@@ -106,6 +106,22 @@ class BaseRdfLayout(metaclass=ABCMeta):
         pass
 
 
+    @abstractmethod
+    def get_version_info(self, urn):
+        '''
+        Get version information about a resource (`fcr:versions`)
+        '''
+        pass
+
+
+    @abstractmethod
+    def get_version(self, urn):
+        '''
+        Get a historic snapshot (version) of a resource.
+        '''
+        pass
+
+
     @abstractmethod
     def ask_rsrc_exists(self, urn):
         '''

+ 49 - 0
lakesuperior/store_layouts/ldp_rs/default_layout.py

@@ -114,6 +114,55 @@ class DefaultLayout(BaseRdfLayout):
                     's': urn, 'g': self.MAIN_GRAPH_URI}))
 
 
+    def get_version_info(self, urn):
+        '''
+        See base_rdf_layout.get_vesion_info.
+        '''
+        q = '''
+        CONSTRUCT {
+          ?s fcrepo:hasVersion ?v .
+          ?v ?p ?o .
+        } WHERE {
+          GRAPH fcg:metadata {
+            ?s fcrepo:hasVersion ?v .
+            ?v ?p ?o .
+          }
+        }
+        '''
+        rsp = self.ds.query(q, initBindings={'s': urn})
+        if not len(rsp):
+            raise ResourceNotExistsError(
+                    urn, 'No version found for this resource.')
+        else:
+            return rsp.graph
+
+
+    def get_version(self, urn, label):
+        '''
+        See base_rdf_layout.get_vesion.
+        '''
+        q = '''
+        CONSTRUCT {
+          ?v ?p ?o .
+        } WHERE {
+          GRAPH fcg:metadata {
+            ?s fcrepo:hasVersion ?v .
+            ?v fcrepo:hasVersionLabel ?l .
+          }
+          GRAPH fcg:historic {
+            ?v ?p ?o .
+          }
+        }
+        '''
+        rsp = self.ds.query(q, initBindings={'s': urn, 'l': Literal(label)})
+        if not len(rsp):
+            raise ResourceNotExistsError(
+                    urn,
+                    'No version found for this resource with the given label.')
+        else:
+            return rsp.graph
+
+
     def modify_dataset(self, remove_trp=Graph(), add_trp=Graph(),
             types={nsc['fcrepo'].Resource}):
         '''