Skip to content

Commit

Permalink
Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed May 8, 2024
1 parent 0e70ba7 commit bbcf6a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
37 changes: 20 additions & 17 deletions app/oaipmh/oai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bbcf6a4

Please sign in to comment.