diff --git a/app/oaipmh/oai.py b/app/oaipmh/oai.py index 4869599ff..17ed2a49a 100644 --- a/app/oaipmh/oai.py +++ b/app/oaipmh/oai.py @@ -158,23 +158,26 @@ def listIdentifiers(self, root, metadata_dict, verb, metadata_prefix, from_date= root.append(error_element) return etree.tostring(root, pretty_print=True, encoding='unicode') - # if from_date is not None: - # from_date_dt = self.isValidDate(from_date) - # if until_date is not None: - # until_date_dt = self.isValidDate(until_date) - - # # Filter identifiers based on the date range specified by from_date and until_date - # filtered_identifiers = [] + if from_date is not None: + from_date_dt = self.isValidDate(from_date) + if until_date is not None: + until_date_dt = self.isValidDate(until_date) - # for record_identifier, record_data in metadata_dict.items(): - # record_date_str = record_data["metadata"]["date"] + # Filter identifiers based on the date range specified by from_date and until_date + filtered_identifiers = list(metadata_dict.keys()) - # # Convert the date string to a datetime object - # record_date = datetime.strptime(record_date_str, "%Y-%m-%d") + if from_date is not None or until_date is not None: + filtered_identifiers = [] + for record_identifier, record_data in metadata_dict.items(): + if record_data.get("creation_date"): + record_date = datetime.combine(record_data.get("creation_date"), datetime.min.time()) + else: + # Convert the date string to a datetime object + record_date = datetime.strptime(self.earliest_datestamp, "%Y-%m-%d") - # if (from_date_dt is None or record_date >= from_date_dt) and \ - # (until_date_dt is None or record_date <= until_date_dt): - # filtered_identifiers.append((record_identifier, record_date_str)) + if (from_date is None or record_date >= from_date_dt) and \ + (until_date is None or record_date <= until_date_dt): + filtered_identifiers.append(record_identifier) # Create the ListIdentifiers element list_identifiers_element = etree.Element('ListIdentifiers') @@ -184,15 +187,15 @@ def listIdentifiers(self, root, metadata_dict, verb, metadata_prefix, from_date= error_element = Errors.noRecordsMatch() root.append(error_element) else: - for record_identifier, record_medatada in metadata_dict.items(): + for record_identifier in filtered_identifiers: record_element = etree.Element('record') header_element = etree.Element('header') identifier_element = etree.Element('identifier') identifier_element.text = f'{self.repository_indentifier_base_url}{record_identifier}' datestamp_element = etree.Element('datestamp') - if record_medatada.get('creation_date'): - datestamp_element.text = record_medatada.get('creation_date').strftime("%Y-%m-%d") + if metadata_dict[record_identifier].get('creation_date'): + datestamp_element.text = metadata_dict[record_identifier].get('creation_date').strftime("%Y-%m-%d") header_element.append(identifier_element) header_element.append(datestamp_element) diff --git a/app/tests/test_app.py b/app/tests/test_app.py index ddb158f8a..e31d5f868 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -791,6 +791,12 @@ def test_oai(self): self.assertEqual(root.find(".//oaipmh:identifier", namespaces).text, "https://github.com/grycap/tosca/blob/main/templates/simple-node-disk.yml") + # Test ListIdentifiers with from + res = self.client.get('/oai?verb=ListIdentifiers&metadataPrefix=oai_dc&from=2020-09-10') + self.assertEqual(200, res.status_code) + root = etree.fromstring(res.data) + elems = root.findall(".//oaipmh:identifier", namespaces) + # Test ListRecords oai_dc res = self.client.get('/oai?verb=ListRecords&metadataPrefix=oai_dc') self.assertEqual(200, res.status_code)