From 493f8fbe747650a4fbac2e0b153ad0074b4f91e4 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 6 Dec 2023 13:00:53 +0300 Subject: Feature: Upload Samples/Cases Implements the code enabling the upload of the samples/cases to the database. --- qc_app/samples.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 qc_app/samples.py (limited to 'qc_app/samples.py') diff --git a/qc_app/samples.py b/qc_app/samples.py new file mode 100644 index 0000000..cc745ca --- /dev/null +++ b/qc_app/samples.py @@ -0,0 +1,108 @@ +"""Code regarding samples""" +import MySQLdb as mdb +from MySQLdb.cursors import DictCursor +from flask import ( + flash, request, url_for, redirect, Blueprint, render_template, current_app as app) + +from .db_utils import with_db_connection +from .dbinsert import species_by_id, groups_by_species + +samples = Blueprint("samples", __name__) + +@samples.route("/upload/species", methods=["POST"]) +def select_species(): + """Select the species.""" + index_page = redirect(url_for("entry.upload_file")) + species_id = request.form.get("species_id") + if bool(species_id): + species_id = int(species_id) + species = species_by_id(species_id) + if bool(species): + return render_template( + "samples/select-population.html", + species=species, + populations=groups_by_species(species_id)) + flash("Invalid species selected!", "alert-error") + flash("You need to select a species", "alert-error") + return index_page + +def save_population(conn: mdb.Connection, population_details: dict) -> int: + """Save the population details to the db.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT MAX(Id) AS last_id FROM InbredSet") + new_id = cursor.fetchone()["last_id"] + 1 + cursor.execute( + "INSERT INTO InbredSet(" + "Id, InbredSetId, InbredSetName, Name, SpeciesId, FullName, " + "MenuOrderId, Description" + ") " + "VALUES (" + "%(Id)s, %(InbredSetId)s, %(InbredSetName)s, %(Name)s, " + "%(SpeciesId)s, %(FullName)s, %(MenuOrderId)s, %(Description)s" + ")", + { + "Id": new_id, + "InbredSetId": new_id, + "MenuOrderId": 0, + **population_details + }) + return new_id + +def population_by_id(conn: mdb.Connection, population_id: int) -> dict: + """Get the grouping/population by id.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM InbredSet WHERE InbredSetId=%s", + (population_id,)) + return cursor.fetchone() + +@samples.route("/upload/create-population", methods=["POST"]) +def create_population(): + """Create new grouping/population.""" + species_page = redirect(url_for("samples.select_species"), code=307) + species = species_by_id(request.form.get("species_id")) + pop_name = request.form.get("inbredset_name").strip() + pop_fullname = request.form.get("inbredset_fullname").strip() + + if not bool(species): + flash("Invalid species!", "alert-error error-create-population") + return species_page + if (not bool(pop_name)) or (not bool(pop_fullname)): + flash("You *MUST* provide a grouping/population name", + "alert-error error-create-population") + return species_page + + pop_id = with_db_connection(lambda conn: save_population(conn, { + "SpeciesId": species["SpeciesId"], + "Name": pop_name, + "InbredSetName": pop_fullname, + "FullName": pop_fullname, + "Family": request.form.get("inbredset_family") or None, + "Description": request.form.get("description") or None + })) + flash("Grouping/Population created successfully.", "alert-success") + return render_template( + "samples/upload-samples.html", + species=species, + population=with_db_connection( + lambda conn: population_by_id(conn, pop_id))) + +@samples.route("/upload/select-population", methods=["POST"]) +def select_population(): + """Select from existing groupings/populations.""" + species_page = redirect(url_for("samples.select_species"), code=307) + species = species_by_id(request.form.get("species_id")) + pop_id = int(request.form.get("inbredset_id")) + population = with_db_connection(lambda conn: population_by_id(conn, pop_id)) + + if not bool(species): + flash("Invalid species!", "alert-error error-select-population") + return species_page + + if not bool(population): + flash("Invalid grouping/population!", + "alert-error error-select-population") + return species_page + + return render_template("samples/upload-samples.html", + species=species, + population=population) -- cgit v1.2.3