Skip to content

Commit

Permalink
Merge pull request #159 from A-Baji/unique-route
Browse files Browse the repository at this point in the history
separate attribute and uniques route
  • Loading branch information
jverswijver authored Mar 24, 2023
2 parents d4b06d1 + 9f9a10b commit 58d98ff
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@

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.3] - 2023-03-24

### Changed

- Separated table attribute and unique values routes [#159](https://github.com/datajoint/pharus/pull/159)

## [0.8.2] - 2023-03-23

### Added

- Forms now allow you to specify presets using their schemas and tables to avoid collisions [#158](https://github.com/datajoint/pharus/pull/158)

## [0.8.1] - 2023-03-20

### Added

- Api endpoint `/spec` which returns the spec for the current dynamic routes [#156](https://github.com/datajoint/pharus/pull/156)
- Support for presets in Dynamic forms [#157](https://github.com/datajoint/pharus/pull/157)

### Bugfix

- Added print statement to let user know if their component override has gone through [#156](https://github.com/datajoint/pharus/pull/156)

## [0.8.0] - 2023-02-06

### Added

- Support for new `slideshow` component [#155](https://github.com/datajoint/pharus/pull/155) [#156](https://github.com/datajoint/pharus/pull/156)

## [0.7.3] - 2023-01-31
Expand Down Expand Up @@ -280,6 +290,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.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
[0.8.0]: https://github.com/datajoint/pharus/compare/0.7.3...0.8.0
Expand Down
40 changes: 39 additions & 1 deletion pharus/component_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ def filter_preset(preset: dict):

class TableComponent(FetchComponent):
attributes_route_format = "{route}/attributes"
uniques_route_format = "{route}/uniques"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -464,7 +465,7 @@ def dj_query_route(self):

def attributes_route(self):
attributes_meta = _DJConnector._get_attributes(
self.fetch_metadata["query"] & self.restriction, include_unique_values=True
self.fetch_metadata["query"] & self.restriction
)
return (
NumpyEncoder.dumps(
Expand All @@ -477,6 +478,43 @@ def attributes_route(self):
{"Content-Type": "application/json"},
)

def uniques_route(self):
query = self.fetch_metadata["query"] & self.restriction
query_attributes = dict(primary=[], secondary=[])
for attribute_name, attribute_info in query.heading.attributes.items():
if attribute_info.in_key:
query_attributes["primary"].append(
(
[
dict({"text": str(v), "value": v})
for (v,) in (dj.U(attribute_name) & query).fetch()
]
if True
else None,
)
)
else:
query_attributes["secondary"].append(
(
[
dict({"text": str(v), "value": v})
for (v,) in (dj.U(attribute_name) & query).fetch()
]
if True
else None,
)
)

return (
NumpyEncoder.dumps(
dict(
unique_values=query_attributes,
)
),
200,
{"Content-Type": "application/json"},
)


class MetadataComponent(TableComponent):
def __init__(self, *args, **kwargs):
Expand Down
16 changes: 16 additions & 0 deletions pharus/dynamic_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def {method_name}() -> dict:
attributes_route = type_map[
comp["type"]
].attributes_route_format.format(route=comp["route"])
uniques_route = type_map[
comp["type"]
].uniques_route_format.format(route=comp["route"])
f.write(
(active_route_template).format(
route=attributes_route,
Expand All @@ -216,3 +219,16 @@ def {method_name}() -> dict:
method_name_type="attributes_route",
)
)
f.write(
(active_route_template).format(
route=uniques_route,
rest_verb=[TableComponent.rest_verb[0]],
method_name=uniques_route.replace("/", ""),
component_type=comp["type"],
component_name=comp_name,
component=json.dumps(comp),
static_config=static_config,
payload="",
method_name_type="uniques_route",
)
)
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.2"
__version__ = "0.8.3"
49 changes: 39 additions & 10 deletions tests/test_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,15 @@ def test_get_attributes(token, client, schemas_simple):
False,
None,
False,
[{"text": "0", "value": 0}, {"text": "1", "value": 1}],
None,
],
[
"b_id",
"int",
False,
None,
False,
[
{"text": "10", "value": 10},
{"text": "11", "value": 11},
{"text": "21", "value": 21},
],
None,
],
],
"secondary": [
Expand All @@ -104,17 +100,50 @@ def test_get_attributes(token, client, schemas_simple):
False,
None,
False,
[
{"text": "Raphael", "value": "Raphael"},
{"text": "Bernie", "value": "Bernie"},
],
None,
],
[
"b_number",
"float",
False,
None,
False,
None,
],
],
},
}

assert expected_json == REST_response.get_json()


def test_get_uniques(token, client, schemas_simple):
REST_response = client.get(
"/query1/uniques", headers=dict(Authorization=f"Bearer {token}")
)

expected_json = {
"unique_values": {
"primary": [
[
[{"text": "0", "value": 0}, {"text": "1", "value": 1}],
],
[
[
{"text": "10", "value": 10},
{"text": "11", "value": 11},
{"text": "21", "value": 21},
]
],
],
"secondary": [
[
[
{"text": "Raphael", "value": "Raphael"},
{"text": "Bernie", "value": "Bernie"},
],
],
[
[
{"text": "22.12", "value": 22.12},
{"text": "-1.21", "value": -1.21},
Expand Down

0 comments on commit 58d98ff

Please sign in to comment.