about summary refs log tree commit diff
path: root/qc_app/dbinsert.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-07-07 09:24:29 +0300
committerFrederick Muriuki Muriithi2022-07-07 09:24:29 +0300
commit23a5e2c8dc2cbbf1ce55c22f80519824e413b883 (patch)
treeaa1d39e3ba4f870b2adbb7644ff8dc3ad2ad7bd5 /qc_app/dbinsert.py
parent2e12c23648be1b6827f1717ca143359d29043a39 (diff)
downloadgn-uploader-23a5e2c8dc2cbbf1ce55c22f80519824e413b883.tar.gz
Provide selection for GeneChips
The GeneChipId value is required for the data being inserted, so this
commit provides the UI to enable selection of the chip.
Diffstat (limited to 'qc_app/dbinsert.py')
-rw-r--r--qc_app/dbinsert.py46
1 files changed, 44 insertions, 2 deletions
diff --git a/qc_app/dbinsert.py b/qc_app/dbinsert.py
index 0733e5f..5381fb5 100644
--- a/qc_app/dbinsert.py
+++ b/qc_app/dbinsert.py
@@ -5,9 +5,11 @@ from functools import reduce
 
 import requests
 from redis import Redis
+from MySQLdb.cursors import DictCursor
 from flask import request, Blueprint, render_template, current_app as app
 
 from . import jobs
+from .db_utils import database_connection
 
 dbinsertbp = Blueprint("dbinsert", __name__)
 
@@ -25,6 +27,24 @@ def make_menu_items_grouper(grouping_fn=lambda item: item):
         return {**acc, grouping: (acc[grouping] + (row_values,))}
     return __grouper__
 
+def genechips():
+    "Retrieve the genechip information from the database"
+    def __organise_by_species__(acc, chip):
+        species = chip["species_name"]
+        if acc.get(species) is None:
+            return {**acc, species: (chip,)}
+        return {**acc, species: acc[species] + (chip,)}
+
+    with database_connection() as conn:
+        with conn.cursor(cursorclass=DictCursor) as cursor:
+            cursor.execute(
+                "SELECT GeneChip.*, LOWER(Species.Name) AS species_name "
+                "FROM GeneChip INNER JOIN Species "
+                "ON GeneChip.SpeciesId=Species.SpeciesId")
+            return reduce(__organise_by_species__, cursor.fetchall(), {})
+
+    return {}
+
 @dbinsertbp.route("/select-dataset", methods=["POST"])
 def select_dataset():
     "Select the dataset to add the file contents against"
@@ -52,12 +72,34 @@ def select_dataset():
                     "datasets"][default_species][default_group][
                         default_type]
 
+                gchips = genechips()
+
                 return render_template(
-                    "select_dataset.html", job_id=job_id, job_name=filename,
+                    "select_dataset.html", filename=filename,
                     species=menu_contents["species"],
                     default_species=default_species, groups=mouse_groups,
                     types=group_types, datasets=datasets,
-                    menu_contents=json.dumps(menu_contents))
+                    menu_contents=json.dumps(menu_contents),
+                    genechips_data=json.dumps(gchips),
+                    genechips=gchips.get(default_species, []))
             return render_error(f"File '{filename}' no longer exists.")
         return render_error(f"Job '{job_id}' no longer exists.")
     return render_error("Unknown error")
+
+
+@dbinsertbp.route("/insert_data", methods=["POST"])
+def insert_data():
+    "Preview the data before triggering entry into the database"
+    form = request.form
+    filename = form["filename"]
+    filepath = f"{app.config['UPLOAD_FOLDER']}/{filename}"
+    if os.path.exists(filepath):
+        try:
+            dataset_id = int(form["dataset"])
+            return (f"Would insert data in file '{filepath}' "
+                    "into the database with the dataset "
+                    f"{dataset_id}")
+        except ValueError as ve:
+            msg = "::".join(ve.args)
+            return render_error(f"Invalid value: {msg}")
+    return render_error(f"File '{filename}' no longer exists.")