From d91dcfcc09a1ad0655f90e6a27a3de796a2ed41f Mon Sep 17 00:00:00 2001 From: Paul Stretenowich Date: Thu, 26 Oct 2023 13:06:44 -0400 Subject: [PATCH] Using project rather than project_name or project_id in routes - test 1 --- project_tracking/api/project.py | 51 ++++++++++++++++----------------- project_tracking/db_action.py | 11 +++++-- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/project_tracking/api/project.py b/project_tracking/api/project.py index 1299897..6593fdd 100644 --- a/project_tracking/api/project.py +++ b/project_tracking/api/project.py @@ -29,43 +29,40 @@ def unroll(string): return unroll_list -def capitalize(func): +def standardize_project(func): """ - Capitalize project_name + Standardize project used by the client: allowing ID and name to be used """ @functools.wraps(func) - def wrap(*args, project_name = None, **kwargs): - if isinstance(project_name, str): - project_name = project_name.upper() - if project_name not in [p.name for p in db_action.projects(project_name)]: - return abort( - 404, - f"Project {project_name} not found" - ) - return func(*args, project_name=project_name, **kwargs) + def wrap(*args, project=None, **kwargs): + project_dict = { + "id": None, + "name": None + } + if project is None: + pass + elif project.isdigit(): + project_dict["id"] = project + else: + project_dict["name"] = project.upper() + project_dict["id"] = db_action.name_to_id("Project", project_dict["name"]) + + return func(*args, project=project_dict, **kwargs) return wrap @bp.route('/') -@bp.route('/') -# @capitalize -def projects(project_id: str = None): +@bp.route('/') +@standardize_project +def projects(project: str = None): """ - patient_id: uses the form "/project/1" - patient_name: uses the form "/project/'?name='" + project: uses the form "/project/1" for project ID and "/project/name" for project name return: list of all the details of the poject with name "project_name" or ID "project_id" """ - query = request.args - # valid query - name = None - if query.get('name'): - name = query['name'] - if name: - project_id = db_action.name_to_id("Project", name) - if project_id is None: - return {"Project list": [f"id: {i.id}, name: {i.name}" for i in db_action.projects(project_id)]} - return [i.flat_dict for i in db_action.projects(project_id)] + if project["id"] is None: + return {"Project list": [f"id: {project.id}, name: {project.name}" for project in db_action.projects(project["id"])]} + return [i.flat_dict for i in db_action.projects(project["id"])] @@ -255,7 +252,7 @@ def files(project_id: str, patient_id: str=None, sample_id: str=None, readset_id @bp.route('//patients//metrics') @bp.route('//samples//metrics') @bp.route('//readsets//metrics') -@capitalize +# @capitalize def metrics(project_id: str, patient_id: str=None, sample_id: str=None, readset_id: str=None, metric_id: str=None): """ metric_id: uses the form "1,3-8,9". Select metric by ids diff --git a/project_tracking/db_action.py b/project_tracking/db_action.py index 78b8812..6dee400 100644 --- a/project_tracking/db_action.py +++ b/project_tracking/db_action.py @@ -92,9 +92,16 @@ def projects(project_id=None, session=None): .where(Project.id.in_(project_id)) ) else: - raise DidNotFindError(f"Requested Project doesn't exist. Please try again with one of the following: {session.scalars(select(Project.name)).unique().all()}") + all_available = [f"id: {project.id}, name: {project.name}" for project in session.scalars(select(Project)).unique().all()] + raise DidNotFindError(f"Requested Project doesn't exist. Please try again with one of the following: {all_available}") - return session.scalars(stmt).unique().all() + ret = session.scalars(stmt).unique().all() + + if not ret: + all_available = [f"id: {project.id}, name: {project.name}" for project in session.scalars(select(Project)).unique().all()] + raise DidNotFindError(f"Requested Project doesn't exist. Please try again with one of the following: {all_available}") + + return ret def metrics_deliverable(project_id: str, deliverable: bool, patient_id=None, sample_id=None, readset_id=None, metric_id=None): """