[PATCH 2 of 2 oaipmh] Use registered entity types to compute earliestDatestamp in verb=Identify

Denis Laxalde denis.laxalde at logilab.fr
Mon Mar 20 15:15:42 CET 2017


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1490018498 -3600
#      Mon Mar 20 15:01:38 2017 +0100
# Node ID 38b2e630a067172613c1e13fe7e4f15064657314
# Parent  0c1aa21839078168694d70d3b220e88c385db9f3
Use registered entity types to compute earliestDatestamp in verb=Identify

And if no entity of registered types exists when this is called, fall back to
an old entity.

diff --git a/entities.py b/entities.py
--- a/entities.py
+++ b/entities.py
@@ -91,6 +91,29 @@ class OAIComponent(Component):
             for fmt in formats:
                 yield prefix, fmt
 
+    def earliest_datestamp(self):
+        """Return a datetime object guaranteed to be the lower limit of all
+        datestamps recording changes, modifications, or deletions in the
+        repository.
+
+        This is used in the Identify verb (see
+        http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#Identify).
+        """
+        registered_etypes = [
+            specifier.etype for specifier in self.__setspecs__.values()
+            if isinstance(specifier, ETypeOAISetSpec)
+        ]
+        rset = self._cw.execute(
+            'Any D LIMIT 1 WHERE X creation_date D, X is IN (%s)'
+            % ', '.join(registered_etypes))
+        if rset:
+            return rset[0][0]
+        # No entities of registered entity types exists in repository. Look
+        # for an old object, 'system.version.cubicweb' CWProperty inserted at
+        # instance creation.
+        entity = self._cw.find('CWProperty', pkey=u'system.version.cubicweb').one()
+        return entity.creation_date
+
     def match(self, setspec=None, from_date=None, until_date=None,
               metadata_prefix=None, from_eid=None):
         """Return a ResultSet or None and the eid of the next entity to yield.
diff --git a/test/test_oaipmh.py b/test/test_oaipmh.py
--- a/test/test_oaipmh.py
+++ b/test/test_oaipmh.py
@@ -344,12 +344,27 @@ class OAIPMHViewsTC(PyramidCWTest, OAITe
         self.config.global_set_option('admin-email', 'oai-admin at example.org')
         with self.admin_access.web_request() as req:
             result = self.oai_request(req, verb='Identify')
+            cwversion = req.find(
+                'CWProperty', pkey='system.version.cubicweb').one()
+            earliest_datestamp = datetime_isoformat(cwversion.creation_date)
         self.assertIn('<repositoryName>unset title</repositoryName>', result)
         self.assertIn('<baseURL>https://localhost:80/oai</baseURL>', result)
         self.assertIn('<protocolVersion>2.0</protocolVersion>', result)
         self.assertIn('<adminEmail>oai-admin at example.org</adminEmail>', result)
+        self.assertIn(
+            '<earliestDatestamp>{}</earliestDatestamp>'.format(earliest_datestamp),
+            result)
         self.assertIn('<deletedRecord>no', result)
         self.assertIn('<granularity>YYYY-MM-DDThh:mm:ssZ', result)
+        with self.admin_access.cnx() as cnx:
+            entity = cnx.create_entity('Thing', identifier=u'123')
+            cnx.commit()
+            earliest_datestamp = datetime_isoformat(entity.creation_date)
+        with self.admin_access.web_request() as req:
+            result = self.oai_request(req, verb='Identify')
+        self.assertIn(
+            '<earliestDatestamp>{}</earliestDatestamp>'.format(earliest_datestamp),
+            result)
 
     def test_identify_badargument(self):
         with self.admin_access.web_request() as req:
diff --git a/views.py b/views.py
--- a/views.py
+++ b/views.py
@@ -399,10 +399,8 @@ class OAIIdentifyView(OAIView):
             pass
         else:
             yield E('adminEmail', admin_email)
-        oldest = self._cw.execute(
-            'Any D WHERE X creation_date D, X identity MX'
-            ' WITH MX BEING (Any MIN(X))')[0][0]
-        yield E('earliestDatestamp', datetime_isoformat(oldest))
+        yield E('earliestDatestamp',
+                datetime_isoformat(oai.earliest_datestamp()))
         yield E('deletedRecord', oai.deleted_handling)
         yield E('granularity', 'YYYY-MM-DDThh:mm:ssZ')
 



More information about the saem-devel mailing list