diff --git a/ckanext/dataset_series/actions.py b/ckanext/dataset_series/actions.py index 1468cad..d7845da 100644 --- a/ckanext/dataset_series/actions.py +++ b/ckanext/dataset_series/actions.py @@ -1,6 +1,12 @@ +import logging + +from dateutil.parser import parse as parse_date, ParserError as DateParserError + import ckan.plugins.toolkit as toolkit +log = logging.getLogger(__name__) +@toolkit.side_effect_free @toolkit.chained_action def package_show(up_func, context, data_dict): @@ -30,10 +36,13 @@ def _add_series_member_navigation(dataset_dict: dict) -> dict: ) if series_dict.get("series_order_field"): + + is_date = series_dict.get("series_order_type", "").lower() == "date" prev, next_ = _get_series_prev_and_next( series_id, series_dict["series_order_field"], dataset_dict[series_dict["series_order_field"]], + is_date=is_date, ) if "series_navigation" not in dataset_dict: dataset_dict["series_navigation"] = [] @@ -64,11 +73,21 @@ def _add_series_member_navigation(dataset_dict: dict) -> dict: return dataset_dict -def _get_series_prev_and_next(series_id, order_field, current_value): +def _get_series_prev_and_next(series_id, order_field, current_value, is_date=False): prev = None next_ = None + if is_date: + try: + date = parse_date(current_value) + current_value = date.isoformat() + if not date.tzinfo: + current_value += "Z" + except DateParserError: + log.warning(f"Wrong date value for series navigation: {current_value}") + return None, None + prev_result = toolkit.get_action("package_search")( {}, { diff --git a/ckanext/dataset_series/tests/test_action.py b/ckanext/dataset_series/tests/test_action.py index 774414e..6a74d27 100644 --- a/ckanext/dataset_series/tests/test_action.py +++ b/ckanext/dataset_series/tests/test_action.py @@ -177,3 +177,44 @@ def test_series_only_dataset(): assert dataset_dict["series_navigation"][0]["previous"] is None assert dataset_dict["series_navigation"][0]["next"] is None + + +@pytest.mark.usefixtures("with_plugins", "clean_db") +@pytest.mark.ckan_config("ckan.plugins", "dataset_series scheming_datasets") +@pytest.mark.ckan_config( + "scheming.dataset_schemas", + "ckanext.dataset_series.schemas:dataset_series.yaml " + "ckanext.dataset_series.schemas:dataset_in_series.yaml", +) +def test_series_date_field(): + + dataset_series = factories.Dataset( + type="dataset-series", + series_order_field="metadata_created", + series_order_type="date", + ) + + dataset1 = factories.Dataset( + name="test-series-member-1", in_series=dataset_series["id"] + ) + dataset2 = factories.Dataset( + name="test-series-member-2", in_series=dataset_series["id"] + ) + dataset3 = factories.Dataset( + name="test-series-member-3", in_series=dataset_series["id"] + ) + + dataset_dict = call_action("package_show", id=dataset1["id"]) + + assert dataset_dict["series_navigation"][0]["previous"] is None + assert dataset_dict["series_navigation"][0]["next"]["id"] == dataset2["id"] + + dataset_dict = call_action("package_show", id=dataset2["id"]) + + assert dataset_dict["series_navigation"][0]["previous"]["id"] == dataset1["id"] + assert dataset_dict["series_navigation"][0]["next"]["id"] == dataset3["id"] + + dataset_dict = call_action("package_show", id=dataset3["id"]) + + assert dataset_dict["series_navigation"][0]["previous"]["id"] == dataset2["id"] + assert dataset_dict["series_navigation"][0]["next"] is None diff --git a/requirements.txt b/requirements.txt index e69de29..0f08daa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +python-dateutil