From 44a07c95a3ae77441e1b45b9f5fba9c6d77a2871 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 25 Sep 2024 10:26:15 -0500 Subject: PoC: Common checks in decorators work. --- uploader/genotypes/views.py | 47 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 32 deletions(-) (limited to 'uploader') diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py index 1ac36b2..41cd21e 100644 --- a/uploader/genotypes/views.py +++ b/uploader/genotypes/views.py @@ -11,6 +11,7 @@ from uploader.ui import make_template_renderer from uploader.authorisation import require_login from uploader.db_utils import database_connection from uploader.species.models import all_species, species_by_id +from uploader.request_checks import with_species, with_population from uploader.datautils import safe_int, order_by_family, enumerate_sequence from uploader.population.models import (populations_by_species, population_by_species_and_id) @@ -44,14 +45,10 @@ def index(): @genotypesbp.route("//populations/genotypes/select-population", methods=["GET"]) @require_login -def select_population(species_id: int): +@with_species(redirect_uri="species.populations.genotypes.index") +def select_population(species: dict, species_id: int): """Select the population under which the genotypes go.""" with database_connection(app.config["SQL_URI"]) as conn: - species = species_by_id(conn, species_id) - if not bool(species): - flash("Invalid species provided!", "alert-danger") - return redirect(url_for("species.populations.genotypes.index")) - if not bool(request.args.get("population_id")): return render_template("genotypes/select-population.html", species=species, @@ -77,55 +74,41 @@ def select_population(species_id: int): "//populations//genotypes", methods=["GET"]) @require_login -def list_genotypes(species_id: int, population_id: int): +@with_population(species_redirect_uri="species.populations.genotypes.index", + redirect_uri="species.populations.genotypes.select_population") +def list_genotypes(species: dict, population: dict, **kwargs): """List genotype details for species and population.""" with database_connection(app.config["SQL_URI"]) as conn: - species = species_by_id(conn, species_id) - if not bool(species): - flash("Invalid species provided!", "alert-danger") - return redirect(url_for("species.populations.genotypes.index")) - - population = population_by_species_and_id( - conn, species_id, population_id) - if not bool(population): - flash("Invalid population selected!", "alert-danger") - return redirect(url_for( - "species.populations.genotypes.select_population", - species_id=species_id)) - return render_template("genotypes/list-genotypes.html", species=species, population=population, genocode=genocode_by_population( - conn, population_id), + conn, population["Id"]), total_markers=genotype_markers_count( - conn, species_id), - dataset=genotype_dataset( - conn, species_id, population_id), + conn, species["SpeciesId"]), + dataset=genotype_dataset(conn, + species["SpeciesId"], + population["Id"]), activelink="list-genotypes") @genotypesbp.route("//genotypes/list-markers", methods=["GET"]) @require_login -def list_markers(species_id: int): +@with_species(redirect_uri="species.populations.genotypes.index") +def list_markers(species: dict, **kwargs): """List a species' genetic markers.""" with database_connection(app.config["SQL_URI"]) as conn: - species = species_by_id(conn, species_id) - if not bool(species): - flash("Invalid species provided!", "alert-danger") - return redirect(url_for("species.populations.genotypes.index")) - start_from = max(safe_int(request.args.get("start_from") or 0), 0) count = safe_int(request.args.get("count") or 20) return render_template("genotypes/list-markers.html", species=species, total_markers=genotype_markers_count( - conn, species_id), + conn, species["SpeciesId"]), start_from=start_from, count=count, markers=enumerate_sequence( genotype_markers(conn, - species_id, + species["SpeciesId"], offset=start_from, limit=count), start=start_from+1), -- cgit v1.2.3