aboutsummaryrefslogtreecommitdiff
path: root/qc_app/dbinsert.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-12-14 19:21:14 +0300
committerFrederick Muriuki Muriithi2023-12-14 19:21:14 +0300
commit8e9abe8eccd1a95d34ab9a6bc7b92d1e660dcae7 (patch)
tree95adb02c79bf0a92b0b0da842471a248b2e62031 /qc_app/dbinsert.py
parent2515dfb7100edf5aaeb0f1a1a1be1d034e39904e (diff)
downloadgn-uploader-8e9abe8eccd1a95d34ab9a6bc7b92d1e660dcae7.tar.gz
Pass connection to `species_by_id` function.
To make `species_by_id` function reusable even outside of the application context, pass in the database connection instead of creating the connection inside the function.
Diffstat (limited to 'qc_app/dbinsert.py')
-rw-r--r--qc_app/dbinsert.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/qc_app/dbinsert.py b/qc_app/dbinsert.py
index ab1c350..2282c8d 100644
--- a/qc_app/dbinsert.py
+++ b/qc_app/dbinsert.py
@@ -5,6 +5,7 @@ from typing import Union
from functools import reduce
from datetime import datetime
+import MySQLdb as mdb
from redis import Redis
from MySQLdb.cursors import DictCursor
from flask import (
@@ -12,7 +13,7 @@ from flask import (
current_app as app)
from . import jobs
-from .db_utils import database_connection
+from .db_utils import with_db_connection, database_connection
dbinsertbp = Blueprint("dbinsert", __name__)
@@ -41,17 +42,16 @@ def species() -> tuple:
return tuple()
-def species_by_id(speciesid) -> Union[dict, None]:
+def species_by_id(conn: mdb.Connection, speciesid) -> Union[dict, None]:
"Retrieve the species from the database by id."
- with database_connection() as conn:
- with conn.cursor(cursorclass=DictCursor) as cursor:
- cursor.execute(
- (
- "SELECT "
- "SpeciesId, SpeciesName, LOWER(Name) AS Name, MenuName "
- "FROM Species WHERE SpeciesId=%s"),
- (speciesid,))
- return cursor.fetchone()
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cursor.execute(
+ (
+ "SELECT "
+ "SpeciesId, SpeciesName, LOWER(Name) AS Name, MenuName "
+ "FROM Species WHERE SpeciesId=%s"),
+ (speciesid,))
+ return cursor.fetchone()
def genechips():
"Retrieve the genechip information from the database"
@@ -362,7 +362,8 @@ def final_confirmation():
filetype=form["filetype"], totallines=form["totallines"],
species=speciesid, genechipid=genechipid, studyid=studyid,
datasetid=datasetid, the_species=selected_keys(
- species_by_id(speciesid), ("SpeciesName", "Name", "MenuName")),
+ with_db_connection(lambda conn: species_by_id(conn, speciesid)),
+ ("SpeciesName", "Name", "MenuName")),
platform=selected_keys(
platform_by_id(genechipid),
("GeneChipName", "Name", "GeoPlatform", "Title", "GO_tree_value")),