diff options
author | Frederick Muriuki Muriithi | 2022-07-06 10:19:49 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-07-06 10:23:46 +0300 |
commit | 2e12c23648be1b6827f1717ca143359d29043a39 (patch) | |
tree | 51dad8a9eff165f37bb91b0541cb38bb142fd259 /qc_app/dbinsert.py | |
parent | e68c807e6598a4087d7c83510ba33c81139f5544 (diff) | |
download | gn-uploader-2e12c23648be1b6827f1717ca143359d29043a39.tar.gz |
Implement UI for dataset selection
As part of updating the database with the new data, there is a need to
select the appropriate dataset that the data belongs to, and this
commit provides the UI to assist the user do that.
Diffstat (limited to 'qc_app/dbinsert.py')
-rw-r--r-- | qc_app/dbinsert.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/qc_app/dbinsert.py b/qc_app/dbinsert.py new file mode 100644 index 0000000..0733e5f --- /dev/null +++ b/qc_app/dbinsert.py @@ -0,0 +1,63 @@ +"Handle inserting data into the database" +import os +import json +from functools import reduce + +import requests +from redis import Redis +from flask import request, Blueprint, render_template, current_app as app + +from . import jobs + +dbinsertbp = Blueprint("dbinsert", __name__) + +def render_error(error_msg): + "Render the generic error page" + return render_template("dbupdate_error.html", error_message=error_msg), 400 + +def make_menu_items_grouper(grouping_fn=lambda item: item): + "Build function to be used to group menu items." + def __grouper__(acc, row): + grouping = grouping_fn(row[2]) + row_values = (row[0].strip(), row[1].strip()) + if acc.get(grouping) is None: + return {**acc, grouping: (row_values,)} + return {**acc, grouping: (acc[grouping] + (row_values,))} + return __grouper__ + +@dbinsertbp.route("/select-dataset", methods=["POST"]) +def select_dataset(): + "Select the dataset to add the file contents against" + job_id = request.form["job_id"] + with Redis.from_url(app.config["REDIS_URL"], decode_responses=True) as rconn: + job = jobs.job(rconn, job_id) + if job: + filename = job["filename"] + filepath = f"{app.config['UPLOAD_FOLDER']}/{filename}" + if os.path.exists(filepath): + req = requests.get( + "https://genenetwork.org/api3/api/menu/generate/json") + menu_contents = req.json() + default_species = "mouse" + mouse_groups = reduce( + make_menu_items_grouper( + lambda item: item.strip()[7:].strip()), + menu_contents["groups"][default_species], {}) + default_group = "BXD" + group_types = reduce( + make_menu_items_grouper(), + menu_contents["types"][default_species][default_group], {}) + default_type = group_types[tuple(group_types)[0]][0][0] + datasets = menu_contents[ + "datasets"][default_species][default_group][ + default_type] + + return render_template( + "select_dataset.html", job_id=job_id, job_name=filename, + species=menu_contents["species"], + default_species=default_species, groups=mouse_groups, + types=group_types, datasets=datasets, + menu_contents=json.dumps(menu_contents)) + return render_error(f"File '{filename}' no longer exists.") + return render_error(f"Job '{job_id}' no longer exists.") + return render_error("Unknown error") |