Skip to content

Commit

Permalink
Fixes #5, Allow sorting with date values
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jan 28, 2025
1 parent 44a15df commit 343b867
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ckanext/dataset_series/actions.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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"] = []
Expand Down Expand Up @@ -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")(
{},
{
Expand Down
41 changes: 41 additions & 0 deletions ckanext/dataset_series/tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-dateutil

0 comments on commit 343b867

Please sign in to comment.