From f274c3f4d0f3f17a7d62e499467a58b10aa81af7 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsko Date: Wed, 30 Aug 2023 20:25:15 +0300 Subject: [PATCH 1/3] Fixed ISO range parsing --- src/server/_params.py | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/server/_params.py b/src/server/_params.py index 39c25ce1e..805f1f8cf 100644 --- a/src/server/_params.py +++ b/src/server/_params.py @@ -425,44 +425,21 @@ def extract_dates(key: Union[str, Sequence[str]]) -> Optional[TimeValues]: return None values: TimeValues = [] - def push_range(first: str, last: str): - first_d = parse_date(first) - last_d = parse_date(last) - if first_d == last_d: - # the first and last numbers are the same, just treat it as a singe value - return first_d - if last_d > first_d: - # add the range as an array - return (first_d, last_d) - # the range is inverted, this is an error - raise ValidationFailedException(f"{key}: the given range is inverted") - for part in parts: - if "-" not in part and ":" not in part: - # YYYYMMDD - values.append(parse_date(part)) + if "*" in part: + values.append(part) continue if ":" in part: # YYYY-MM-DD:YYYY-MM-DD range_part = part.split(":", 2) - r = push_range(range_part[0], range_part[1]) - if r is None: - return None - values.append(r) - continue - # YYYY-MM-DD or YYYYMMDD-YYYYMMDD - # split on the dash - range_part = part.split("-") - if len(range_part) == 2: - # YYYYMMDD-YYYYMMDD - r = push_range(range_part[0], range_part[1]) + r = _verify_range(parse_date(range_part[0]), parse_date(range_part[1])) if r is None: return None values.append(r) continue - # YYYY-MM-DD - values.append(parse_date(part)) - # success, return the list + # parse another date formats + r = parse_day_value(part) + values.append(r) return values def parse_source_signal_sets() -> List[SourceSignalSet]: From e7cabf103927e73aab52c297cdc7c5dda55622d0 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsko Date: Mon, 18 Sep 2023 19:58:21 +0300 Subject: [PATCH 2/3] Add suggested changes from PR comments --- src/server/_params.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/server/_params.py b/src/server/_params.py index 805f1f8cf..134bc400d 100644 --- a/src/server/_params.py +++ b/src/server/_params.py @@ -426,18 +426,17 @@ def extract_dates(key: Union[str, Sequence[str]]) -> Optional[TimeValues]: values: TimeValues = [] for part in parts: - if "*" in part: - values.append(part) - continue + if part == "*": + return ["*"] if ":" in part: # YYYY-MM-DD:YYYY-MM-DD - range_part = part.split(":", 2) + range_part = part.split(":", 1) r = _verify_range(parse_date(range_part[0]), parse_date(range_part[1])) if r is None: return None values.append(r) continue - # parse another date formats + # parse other date formats r = parse_day_value(part) values.append(r) return values From f5909269a30bf83946993c1eee316d5e7aaec724 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsko Date: Mon, 18 Sep 2023 19:58:46 +0300 Subject: [PATCH 3/3] Added 2 more test cases for extract_dates func --- tests/server/test_params.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/server/test_params.py b/tests/server/test_params.py index 6d6de4fdc..cd287445c 100644 --- a/tests/server/test_params.py +++ b/tests/server/test_params.py @@ -477,6 +477,12 @@ def test_extract_dates(self): with self.subTest("multiple param mixed iso"): with app.test_request_context("/?s=2020-01-01&s=2020-01-02,2020-01-03"): self.assertEqual(extract_dates("s"), [20200101, 20200102, 20200103]) + with self.subTest("iso range"): + with app.test_request_context("/?s=2020-01-01--2020-01-30"): + self.assertEqual(extract_dates("s"), [(20200101, 20200130)]) + with self.subTest("wildcard"): + with app.test_request_context("/?s=*"): + self.assertEqual(extract_dates("s"), ["*"]) with self.subTest("not a date"): with app.test_request_context("/?s=a"):