Skip to content

Commit 21d8ce9

Browse files
authored
Admin: coerce resource temporal extents into datetime objects (geopython#1877)
* coerce resource temporal extents into datetime objects * fix ref * fix test * fix test * fix test * fix test * fix ref
1 parent 8b1a987 commit 21d8ce9

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

pygeoapi/admin.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Authors: Tom Kralidis <[email protected]>
44
# Benjamin Webb <[email protected]>
55
#
6-
# Copyright (c) 2023 Tom Kralidis
6+
# Copyright (c) 2024 Tom Kralidis
77
# Copyright (c) 2023 Benjamin Webb
88
#
99
# Permission is hereby granted, free of charge, to any person
@@ -32,16 +32,16 @@
3232
from copy import deepcopy
3333
import os
3434
import json
35-
from jsonpatch import make_patch
36-
from jsonschema.exceptions import ValidationError
3735
import logging
3836
from typing import Any, Tuple, Union
3937

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
4141

42+
from pygeoapi.api import API, APIRequest, F_HTML, pre_process
4243
from pygeoapi.config import get_config, validate_config
4344
from pygeoapi.openapi import get_oas
44-
# from pygeoapi.openapi import validate_openapi_document
4545
from pygeoapi.util import to_json, render_j2_template, yaml_dump
4646

4747

@@ -221,6 +221,8 @@ def put_config(
221221

222222
try:
223223
data = json.loads(data)
224+
for key, value in data.get('resources', {}).items():
225+
temporal_extents_str2datetime(value.get('extents', {}))
224226
except (json.decoder.JSONDecodeError, TypeError) as err:
225227
# Input is not valid JSON
226228
LOGGER.error(err)
@@ -277,6 +279,8 @@ def patch_config(
277279

278280
try:
279281
data = json.loads(data)
282+
for key, value in data.get('resources', {}).items():
283+
temporal_extents_str2datetime(value.get('extents', {}))
280284
except (json.decoder.JSONDecodeError, TypeError) as err:
281285
# Input is not valid JSON
282286
LOGGER.error(err)
@@ -367,6 +371,8 @@ def post_resource(
367371

368372
try:
369373
data = json.loads(data)
374+
res = list(data.keys())[0]
375+
temporal_extents_str2datetime(data[res].get('extents', {}))
370376
except (json.decoder.JSONDecodeError, TypeError) as err:
371377
# Input is not valid JSON
372378
LOGGER.error(err)
@@ -528,6 +534,7 @@ def put_resource(
528534

529535
try:
530536
data = json.loads(data)
537+
temporal_extents_str2datetime(data.get('extents', {}))
531538
except (json.decoder.JSONDecodeError, TypeError) as err:
532539
# Input is not valid JSON
533540
LOGGER.error(err)
@@ -594,6 +601,7 @@ def patch_resource(
594601

595602
try:
596603
data = json.loads(data)
604+
temporal_extents_str2datetime(data.get('extents', {}))
597605
except (json.decoder.JSONDecodeError, TypeError) as err:
598606
# Input is not valid JSON
599607
LOGGER.error(err)
@@ -621,3 +629,19 @@ def patch_resource(
621629
content = to_json(resource, self.pretty_print)
622630

623631
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')

tests/test_admin_api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Authors: Tom Kralidis <[email protected]>
44
# Authors: Benjamin Webb <[email protected]>
55
#
6-
# Copyright (c) 2023 Tom Kralidis
6+
# Copyright (c) 2024 Tom Kralidis
77
# Copyright (c) 2023 Benjamin Webb
88
#
99
# Permission is hereby granted, free of charge, to any person
@@ -29,13 +29,15 @@
2929
#
3030
# =================================================================
3131

32+
from datetime import datetime
3233
import time
33-
34-
from pathlib import Path
3534
import unittest
3635

36+
from pathlib import Path
3737
from requests import Session
3838

39+
from pygeoapi.util import yaml_load
40+
3941
THISDIR = Path(__file__).resolve().parent
4042

4143

@@ -110,12 +112,16 @@ def test_resources_crud(self):
110112
content = self.http.get(url).json()
111113
self.assertEqual(len(content.keys()), 2)
112114

115+
with get_abspath('../../pygeoapi-test-config-admin.yml').open() as fh:
116+
d = yaml_load(fh)
117+
temporal_extent_begin = d['resources']['data2']['extents']['temporal']['begin'] # noqa
118+
self.assertIsInstance(temporal_extent_begin, datetime)
119+
113120
# PUT an existing resource
114121
url = f'{self.admin_endpoint}/resources/data2'
115122
with get_abspath('resource-put.json').open() as fh:
116123
post_data = fh.read()
117-
print(url)
118-
print(get_abspath('resource-put.json'))
124+
119125
response = self.http.put(url, data=post_data)
120126
self.assertEqual(response.status_code, 204)
121127

0 commit comments

Comments
 (0)