Skip to content

Commit 58d98ff

Browse files
authored
Merge pull request #159 from A-Baji/unique-route
separate attribute and uniques route
2 parents d4b06d1 + 9f9a10b commit 58d98ff

File tree

5 files changed

+106
-12
lines changed

5 files changed

+106
-12
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@
22

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.
44

5+
## [0.8.3] - 2023-03-24
6+
7+
### Changed
8+
9+
- Separated table attribute and unique values routes [#159](https://github.com/datajoint/pharus/pull/159)
10+
511
## [0.8.2] - 2023-03-23
612

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

1017
## [0.8.1] - 2023-03-20
1118

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

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

1928
## [0.8.0] - 2023-02-06
2029

2130
### Added
31+
2232
- Support for new `slideshow` component [#155](https://github.com/datajoint/pharus/pull/155) [#156](https://github.com/datajoint/pharus/pull/156)
2333

2434
## [0.7.3] - 2023-01-31
@@ -280,6 +290,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
280290
- Support for DataJoint attribute types: `varchar`, `int`, `float`, `datetime`, `date`, `time`, `decimal`, `uuid`.
281291
- Check dependency utility to determine child table references.
282292

293+
[0.8.3]: https://github.com/datajoint/pharus/compare/0.8.2...0.8.3
283294
[0.8.2]: https://github.com/datajoint/pharus/compare/0.8.1...0.8.2
284295
[0.8.1]: https://github.com/datajoint/pharus/compare/0.8.0...0.8.1
285296
[0.8.0]: https://github.com/datajoint/pharus/compare/0.7.3...0.8.0

pharus/component_interface.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def filter_preset(preset: dict):
394394

395395
class TableComponent(FetchComponent):
396396
attributes_route_format = "{route}/attributes"
397+
uniques_route_format = "{route}/uniques"
397398

398399
def __init__(self, *args, **kwargs):
399400
super().__init__(*args, **kwargs)
@@ -464,7 +465,7 @@ def dj_query_route(self):
464465

465466
def attributes_route(self):
466467
attributes_meta = _DJConnector._get_attributes(
467-
self.fetch_metadata["query"] & self.restriction, include_unique_values=True
468+
self.fetch_metadata["query"] & self.restriction
468469
)
469470
return (
470471
NumpyEncoder.dumps(
@@ -477,6 +478,43 @@ def attributes_route(self):
477478
{"Content-Type": "application/json"},
478479
)
479480

481+
def uniques_route(self):
482+
query = self.fetch_metadata["query"] & self.restriction
483+
query_attributes = dict(primary=[], secondary=[])
484+
for attribute_name, attribute_info in query.heading.attributes.items():
485+
if attribute_info.in_key:
486+
query_attributes["primary"].append(
487+
(
488+
[
489+
dict({"text": str(v), "value": v})
490+
for (v,) in (dj.U(attribute_name) & query).fetch()
491+
]
492+
if True
493+
else None,
494+
)
495+
)
496+
else:
497+
query_attributes["secondary"].append(
498+
(
499+
[
500+
dict({"text": str(v), "value": v})
501+
for (v,) in (dj.U(attribute_name) & query).fetch()
502+
]
503+
if True
504+
else None,
505+
)
506+
)
507+
508+
return (
509+
NumpyEncoder.dumps(
510+
dict(
511+
unique_values=query_attributes,
512+
)
513+
),
514+
200,
515+
{"Content-Type": "application/json"},
516+
)
517+
480518

481519
class MetadataComponent(TableComponent):
482520
def __init__(self, *args, **kwargs):

pharus/dynamic_api_gen.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def {method_name}() -> dict:
203203
attributes_route = type_map[
204204
comp["type"]
205205
].attributes_route_format.format(route=comp["route"])
206+
uniques_route = type_map[
207+
comp["type"]
208+
].uniques_route_format.format(route=comp["route"])
206209
f.write(
207210
(active_route_template).format(
208211
route=attributes_route,
@@ -216,3 +219,16 @@ def {method_name}() -> dict:
216219
method_name_type="attributes_route",
217220
)
218221
)
222+
f.write(
223+
(active_route_template).format(
224+
route=uniques_route,
225+
rest_verb=[TableComponent.rest_verb[0]],
226+
method_name=uniques_route.replace("/", ""),
227+
component_type=comp["type"],
228+
component_name=comp_name,
229+
component=json.dumps(comp),
230+
static_config=static_config,
231+
payload="",
232+
method_name_type="uniques_route",
233+
)
234+
)

pharus/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""Package metadata."""
2-
__version__ = "0.8.2"
2+
__version__ = "0.8.3"

tests/test_api_gen.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,15 @@ def test_get_attributes(token, client, schemas_simple):
8282
False,
8383
None,
8484
False,
85-
[{"text": "0", "value": 0}, {"text": "1", "value": 1}],
85+
None,
8686
],
8787
[
8888
"b_id",
8989
"int",
9090
False,
9191
None,
9292
False,
93-
[
94-
{"text": "10", "value": 10},
95-
{"text": "11", "value": 11},
96-
{"text": "21", "value": 21},
97-
],
93+
None,
9894
],
9995
],
10096
"secondary": [
@@ -104,17 +100,50 @@ def test_get_attributes(token, client, schemas_simple):
104100
False,
105101
None,
106102
False,
107-
[
108-
{"text": "Raphael", "value": "Raphael"},
109-
{"text": "Bernie", "value": "Bernie"},
110-
],
103+
None,
111104
],
112105
[
113106
"b_number",
114107
"float",
115108
False,
116109
None,
117110
False,
111+
None,
112+
],
113+
],
114+
},
115+
}
116+
117+
assert expected_json == REST_response.get_json()
118+
119+
120+
def test_get_uniques(token, client, schemas_simple):
121+
REST_response = client.get(
122+
"/query1/uniques", headers=dict(Authorization=f"Bearer {token}")
123+
)
124+
125+
expected_json = {
126+
"unique_values": {
127+
"primary": [
128+
[
129+
[{"text": "0", "value": 0}, {"text": "1", "value": 1}],
130+
],
131+
[
132+
[
133+
{"text": "10", "value": 10},
134+
{"text": "11", "value": 11},
135+
{"text": "21", "value": 21},
136+
]
137+
],
138+
],
139+
"secondary": [
140+
[
141+
[
142+
{"text": "Raphael", "value": "Raphael"},
143+
{"text": "Bernie", "value": "Bernie"},
144+
],
145+
],
146+
[
118147
[
119148
{"text": "22.12", "value": 22.12},
120149
{"text": "-1.21", "value": -1.21},

0 commit comments

Comments
 (0)