Skip to content

Commit

Permalink
✨ More helper functions (get_local_timezone, timestamp_utc, `…
Browse files Browse the repository at this point in the history
…`timestamp_utc``) are created.

* ``initial_bundle_data`` method is now available in Base Elasticsearch engine class,
* fixes: Default bundle initial data was constructed ``meta.lastUpdated`` value with utc now time but without timezone offset
  • Loading branch information
nazrulworld committed Nov 16, 2020
1 parent f6dc81c commit d079c11
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
11 changes: 10 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ History
0.10.3 (unreleased)
-------------------

- Nothing changed yet.
Improvements

- More helper functions (``get_local_timezone``, ``timestamp_utc``, ``timestamp_utc``) are created.

- ``initial_bundle_data`` method is now available in Base Elasticsearch engine class,
meaning that it is possible construct Bundle initial data into the derived class, so more flexibility.

Bugfixes

- Default bundle initial data was constructed ``meta.lastUpdated`` value with utc now time but without timezone offset, so
during json serialization, timezone info was missed as a result reverse construct of Bundle complains validation error.

0.10.2 (2020-11-06)
-------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run(self):
"zope.interface>=5.1.2",
"multidict",
"fhirspec>=0.2.5",
"fhir.resources>=6.0.0b9,<7.0",
"fhir.resources>=6.0.0b10,<7.0",
"yarl",
"isodate",
]
Expand Down
9 changes: 8 additions & 1 deletion src/fhirpath/engine/es/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def navigate_indexed_path(source, path_):


class ElasticsearchEngineBase(Engine):
def initial_bundle_data(self):
"""Can be overridden in sub class"""
return BundleWrapper.init_data()

def extract_hits(self, source_filters, hits, container, doc_type="_doc"):
""" """
for res in hits:
Expand Down Expand Up @@ -92,7 +96,10 @@ def wrapped_with_bundle(self, result, includes=None, as_json=False):
url = self.current_url()
if includes is None:
includes = list()
wrapper = BundleWrapper(self, result, includes, url, "searchset")
init_data = self.initial_bundle_data()
wrapper = BundleWrapper(
self, result, includes, url, "searchset", init_data=init_data
)
return wrapper(as_json=as_json)

def generate_mappings(
Expand Down
47 changes: 42 additions & 5 deletions src/fhirpath/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pkgutil
import re
import sys
import time
import uuid
from importlib import import_module
from inspect import signature
Expand Down Expand Up @@ -50,6 +51,8 @@

__author__ = "Md Nazrul Islam <email2nazrul@gmail.com>"

LOCAL_TIMEZONE: Optional[datetime.timezone] = None


def _reraise(tp, value, tb=None):
if value is None:
Expand Down Expand Up @@ -529,15 +532,21 @@ class BundleWrapper:
""" """

def __init__(
self, engine, result, includes: List, url: URL, bundle_type="searchset"
self,
engine,
result,
includes: List,
url: URL,
bundle_type="searchset",
init_data: Dict[str, Any] = None,
):
""" """
self.fhir_version = engine.fhir_release
self.bundle_model = lookup_fhir_class("Bundle", fhir_release=self.fhir_version)
self.data: Dict[str, Any] = dict()
self.data["id"] = str(uuid.uuid4())

self.data["meta"] = {"lastUpdated": datetime.datetime.utcnow()}
if init_data:
self.data = init_data
else:
self.data = BundleWrapper.init_data()

self.data["type"] = bundle_type
# our pagination is based main query result.
Expand All @@ -553,6 +562,12 @@ def __init__(

self.attach_links(url, len(result.body))

@staticmethod
def init_data() -> Dict[str, Any]:
"""Initialized Bundle data"""
data = {"id": str(uuid.uuid4()), "meta": {"lastUpdated": timestamp_utc()}}
return data

def attach_entry(self, result, mode="match"):
""" """
if "entry" not in self.data:
Expand Down Expand Up @@ -653,3 +668,25 @@ def __call__(self, as_json=False):
def json(self):
""" """
return self.__call__().json()


def get_local_timezone() -> datetime.timezone:
if LOCAL_TIMEZONE is not None:
return LOCAL_TIMEZONE

is_dst = time.daylight and time.localtime().tm_isdst > 0
seconds = -(time.altzone if is_dst else time.timezone)
tz = datetime.timezone(datetime.timedelta(seconds=seconds))
return tz


def timestamp_utc() -> datetime.datetime:
"""UTC datetime with timezone offset"""
dt_now = datetime.datetime.utcnow()
dt_now.replace(tzinfo=datetime.timezone.utc)
return dt_now


def timestamp_local() -> datetime.datetime:
"""Timezone aware datetime with local timezone offset"""
return datetime.datetime.now(tz=get_local_timezone())

0 comments on commit d079c11

Please sign in to comment.