Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preset datetime fix #160

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.

## [0.8.4] - 2023-04-04

### Changed

- Invalid datetime preset values will now throw a 406 error for `/preset` routes [#160](https://github.com/datajoint/pharus/pull/160)

## [0.8.3] - 2023-03-24

### Changed
Expand Down Expand Up @@ -290,6 +296,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
- Support for DataJoint attribute types: `varchar`, `int`, `float`, `datetime`, `date`, `time`, `decimal`, `uuid`.
- Check dependency utility to determine child table references.

[0.8.4]: https://github.com/datajoint/pharus/compare/0.8.3...0.8.4
[0.8.3]: https://github.com/datajoint/pharus/compare/0.8.2...0.8.3
[0.8.2]: https://github.com/datajoint/pharus/compare/0.8.1...0.8.2
[0.8.1]: https://github.com/datajoint/pharus/compare/0.8.0...0.8.1
Expand Down
36 changes: 29 additions & 7 deletions pharus/component_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from uuid import UUID
import cv2
import base64
from dateutil import parser


class NumpyEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -246,6 +247,9 @@ def __init__(self, *args, **kwargs):
for sub_m in (m.get("map", []) + [m])
}
self.input_lookup = {v: k for k, v in self.destination_lookup.items()}
self.datatype_lookup = {
k: v[1] for t in self.tables for k, v in t.heading.attributes.items()
}

if "presets" in self.component_config:
lcls = locals()
Expand Down Expand Up @@ -351,6 +355,14 @@ def presets_route(self):
# If you have a name mapping it will be applied to each preset
# Route will 404 if no preset query is defined and 500 if there is an Exception

# Helper function to convert datetime strings
def convert_datetime_string(datetime_string):
try:
dt = parser.parse(datetime_string)
return dt.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
raise ValueError("Invalid datetime string")

# Helper function to filter out fields not in the insert,
# as well as apply the fields_map
def filter_preset(preset: dict):
Expand All @@ -367,10 +379,13 @@ def filter_preset(preset: dict):
}
return {
(
self.input_lookup[k.split(".").pop()]
if k.split(".").pop() in self.input_lookup
else k.split(".").pop()
): v
self.input_lookup[a]
if (a := k.split(".").pop()) in self.input_lookup
else a
): convert_datetime_string(v)
if a in self.datatype_lookup
and re.search(r"^date.*|time.*$", self.datatype_lookup[a])
else v
for k, v in preset_with_tables_filtered.items()
}

Expand All @@ -381,9 +396,16 @@ def filter_preset(preset: dict):
{"Content-Type": "text/plain"},
)

filtered_preset_dictionary = {
k: filter_preset(v) for k, v in self.presets_dict.items()
}
try:
filtered_preset_dictionary = {
k: filter_preset(v) for k, v in self.presets_dict.items()
}
except ValueError as e:
return (
str(e),
406,
{"Content-Type": "text/plain"},
)

return (
NumpyEncoder.dumps(filtered_preset_dictionary),
Expand Down
2 changes: 1 addition & 1 deletion pharus/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Package metadata."""
__version__ = "0.8.3"
__version__ = "0.8.4"