|
3 | 3 | # Authors: Tom Kralidis <[email protected]> |
4 | 4 | # Benjamin Webb <[email protected]> |
5 | 5 | # |
6 | | -# Copyright (c) 2023 Tom Kralidis |
| 6 | +# Copyright (c) 2024 Tom Kralidis |
7 | 7 | # Copyright (c) 2023 Benjamin Webb |
8 | 8 | # |
9 | 9 | # Permission is hereby granted, free of charge, to any person |
|
32 | 32 | from copy import deepcopy |
33 | 33 | import os |
34 | 34 | import json |
35 | | -from jsonpatch import make_patch |
36 | | -from jsonschema.exceptions import ValidationError |
37 | 35 | import logging |
38 | 36 | from typing import Any, Tuple, Union |
39 | 37 |
|
40 | | -from pygeoapi.api import API, APIRequest, F_HTML, pre_process |
| 38 | +from dateutil.parser import parse as parse_date |
| 39 | +from jsonpatch import make_patch |
| 40 | +from jsonschema.exceptions import ValidationError |
41 | 41 |
|
| 42 | +from pygeoapi.api import API, APIRequest, F_HTML, pre_process |
42 | 43 | from pygeoapi.config import get_config, validate_config |
43 | 44 | from pygeoapi.openapi import get_oas |
44 | | -# from pygeoapi.openapi import validate_openapi_document |
45 | 45 | from pygeoapi.util import to_json, render_j2_template, yaml_dump |
46 | 46 |
|
47 | 47 |
|
@@ -221,6 +221,8 @@ def put_config( |
221 | 221 |
|
222 | 222 | try: |
223 | 223 | data = json.loads(data) |
| 224 | + for key, value in data.get('resources', {}).items(): |
| 225 | + temporal_extents_str2datetime(value.get('extents', {})) |
224 | 226 | except (json.decoder.JSONDecodeError, TypeError) as err: |
225 | 227 | # Input is not valid JSON |
226 | 228 | LOGGER.error(err) |
@@ -277,6 +279,8 @@ def patch_config( |
277 | 279 |
|
278 | 280 | try: |
279 | 281 | data = json.loads(data) |
| 282 | + for key, value in data.get('resources', {}).items(): |
| 283 | + temporal_extents_str2datetime(value.get('extents', {})) |
280 | 284 | except (json.decoder.JSONDecodeError, TypeError) as err: |
281 | 285 | # Input is not valid JSON |
282 | 286 | LOGGER.error(err) |
@@ -367,6 +371,8 @@ def post_resource( |
367 | 371 |
|
368 | 372 | try: |
369 | 373 | data = json.loads(data) |
| 374 | + res = list(data.keys())[0] |
| 375 | + temporal_extents_str2datetime(data[res].get('extents', {})) |
370 | 376 | except (json.decoder.JSONDecodeError, TypeError) as err: |
371 | 377 | # Input is not valid JSON |
372 | 378 | LOGGER.error(err) |
@@ -528,6 +534,7 @@ def put_resource( |
528 | 534 |
|
529 | 535 | try: |
530 | 536 | data = json.loads(data) |
| 537 | + temporal_extents_str2datetime(data.get('extents', {})) |
531 | 538 | except (json.decoder.JSONDecodeError, TypeError) as err: |
532 | 539 | # Input is not valid JSON |
533 | 540 | LOGGER.error(err) |
@@ -594,6 +601,7 @@ def patch_resource( |
594 | 601 |
|
595 | 602 | try: |
596 | 603 | data = json.loads(data) |
| 604 | + temporal_extents_str2datetime(data.get('extents', {})) |
597 | 605 | except (json.decoder.JSONDecodeError, TypeError) as err: |
598 | 606 | # Input is not valid JSON |
599 | 607 | LOGGER.error(err) |
@@ -621,3 +629,19 @@ def patch_resource( |
621 | 629 | content = to_json(resource, self.pretty_print) |
622 | 630 |
|
623 | 631 | return headers, 204, content |
| 632 | + |
| 633 | + |
| 634 | +def temporal_extents_str2datetime(extents: dict) -> None: |
| 635 | + """ |
| 636 | + Helper function to coerce datetime strings into datetime objects |
| 637 | +
|
| 638 | + :extents: `dict` of pygeoapi resource extents object |
| 639 | +
|
| 640 | + :returns: `None` (changes made directly) |
| 641 | + """ |
| 642 | + |
| 643 | + try: |
| 644 | + extents['temporal']['begin'] = parse_date(extents['temporal']['begin']) |
| 645 | + extents['temporal']['end'] = parse_date(extents['temporal']['end']) |
| 646 | + except (KeyError, TypeError): |
| 647 | + LOGGER.debug('No temporal extents found') |
0 commit comments