Skip to content

Commit

Permalink
added new endpoint in rest api to get list of distinct node types (#2745
Browse files Browse the repository at this point in the history
)

* added new endpoint in rest api to get list of distinct node types
* added test for node types rest api call
  • Loading branch information
Snehal Kumbhar authored and ltalirz committed Apr 30, 2019
1 parent 300903f commit db12cdd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
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

0 comments on commit db12cdd

Please sign in to comment.