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

added new endpoint in rest api to get list of distinct node types #2745

Merged
merged 2 commits into from
Apr 30, 2019
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
15 changes: 15 additions & 0 deletions aiida/backends/tests/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,18 @@ def test_schema(self):
available_properties = response["data"]["fields"].keys()
for prop in response["data"]["ordering"]:
self.assertIn(prop, available_properties)

def test_node_types(self):
"""
Test the rest api call to get list of available node types
"""
url = self.get_url_prefix() + '/nodes/types'
with self.app.test_client() as client:
rv = client.get(url)
response = json.loads(rv.data)
self.assertEqual(response["data"]["calculation"], ["calculation.Calculation."])
expected_data_types = ["data.array.kpoints.KpointsData.", "data.cif.CifData.", "data.parameter.ParameterData.", "data.structure.StructureData."]
self.assertEqual(response["data"]["data"], expected_data_types)
RESTApiTestCase.compare_extra_response_data(self, "nodes",
url,
response)
4 changes: 4 additions & 0 deletions aiida/restapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(self, app=None, **kwargs):
'/nodes/',
'/nodes/schema/',
'/nodes/statistics/',
'/nodes/types/',
'/nodes/page/',
'/nodes/page/<int:page>/',
'/nodes/<id>/',
Expand All @@ -139,6 +140,7 @@ def __init__(self, app=None, **kwargs):

self.add_resource(Calculation,
'/calculations/',
'/calculations/types/',
'/calculations/schema/',
'/calculations/page/',
'/calculations/page/<int:page>/',
Expand All @@ -160,6 +162,7 @@ def __init__(self, app=None, **kwargs):

self.add_resource(Data,
'/data/',
'/data/types/',
'/data/schema/',
'/data/page/',
'/data/page/<int:page>',
Expand All @@ -181,6 +184,7 @@ def __init__(self, app=None, **kwargs):

self.add_resource(Code,
'/codes/',
'/codes/types/',
'/codes/schema/',
'/codes/page/',
'/codes/page/<int:page>/',
Expand Down
8 changes: 8 additions & 0 deletions aiida/restapi/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ def parse_path(self, path_string, parse_pk_uuid=None):
"admit further fields")
else:
return (resource_type, page, id, query_type)
elif path[0] == 'types':
query_type = path.pop(0)
if path:
raise RestInputValidationError(
"url requesting statistics resources do not "
"admit further fields")
else:
return (resource_type, page, id, query_type)
elif path[0] == "io" or path[0] == "content":
path.pop(0)
query_type = path.pop(0)
Expand Down
4 changes: 4 additions & 0 deletions aiida/restapi/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ def get(self, id=None, page=None):
usr = None
results = self.trans.get_statistics(usr)

elif query_type == "types":
headers = self.utils.build_headers(url=request.url, total_count=0)
results = self.trans.get_types()

# TODO improve the performance of tree endpoint by getting the data from database faster
# TODO add pagination for this endpoint (add default max limit)
elif query_type == "tree":
Expand Down
25 changes: 25 additions & 0 deletions aiida/restapi/translator/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,31 @@ def get_statistics(self, user_pk=None):
qmanager = QueryFactory()()
return qmanager.get_creation_statistics(user_pk=user_pk)

def get_types(self):
"""
return available distinct types of nodes from database
"""
from aiida.orm.querybuilder import QueryBuilder

qb = QueryBuilder()
qb.append(self._aiida_class, project=["type"])
qb_response = qb.distinct().all()

results = {}
if len(qb_response) > 0:
for ntype in qb_response:
ntype = ntype[0]
ntype_parts = ntype.split(".")
if len(ntype_parts) > 0:
dict_key =ntype_parts[0]
if dict_key not in results.keys():
results[dict_key] = []
results[dict_key].append(ntype)

for key, values in results.items():
results[key] = sorted(values)

return results

def get_io_tree(self, uuid_pattern, tree_in_limit, tree_out_limit):
from aiida.orm.querybuilder import QueryBuilder
Expand Down