diff options
author | Frederick Muriuki Muriithi | 2024-09-30 16:34:34 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-30 16:34:34 -0500 |
commit | 35419644dd9093630649093a4e2fba06c19ffb0c (patch) | |
tree | 836a97f0b258e2179de9b79e64bbc873a0803b10 | |
parent | 8c4b605b2ac78f316ddd1199b413ba0f84241104 (diff) | |
download | gn-uploader-35419644dd9093630649093a4e2fba06c19ffb0c.tar.gz |
Extract common check into decorator for reusability.
-rw-r--r-- | uploader/phenotypes/views.py | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/uploader/phenotypes/views.py b/uploader/phenotypes/views.py index 4565844..a79e863 100644 --- a/uploader/phenotypes/views.py +++ b/uploader/phenotypes/views.py @@ -1,4 +1,6 @@ """Views handling ('classical') phenotypes.""" +from functools import wraps + from flask import (flash, request, url_for, @@ -89,19 +91,60 @@ def list_datasets(species: dict, population: dict, **kwargs):# pylint: disable=[ activelink="list-datasets") +def with_dataset( + species_redirect_uri: str, + population_redirect_uri: str, + redirect_uri: str +): + """Ensure the dataset actually exists.""" + def __decorator__(func): + @wraps(func) + @with_population(species_redirect_uri, population_redirect_uri) + def __with_dataset__(**kwargs): + try: + _spcid = int(kwargs["species_id"]) + _popid = int(kwargs["population_id"]) + _dsetid = int(kwargs.get("dataset_id")) + select_dataset_uri = redirect(url_for( + redirect_uri, species_id=_spcid, population_id=_popid)) + if not bool(_dsetid): + flash("You need to select a valid 'dataset_id' value.", + "alert-danger") + return select_dataset_uri + with database_connection(app.config["SQL_URI"]) as conn: + dataset = dataset_by_id(conn, _spcid, _popid, _dsetid) + if not bool(dataset): + flash("You must select a valid dataset.", + "alert-danger") + return select_dataset_uri + except ValueError as _verr: + app.logger.debug( + "Exception converting 'dataset_id' to integer: %s", + kwargs.get("dataset_id"), + exc_info=True) + flash("Expected 'dataset_id' value to be an integer." + "alert-danger") + return select_dataset_uri + return func(dataset=dataset, **kwargs) + return __with_dataset__ + return __decorator__ + + @phenotypesbp.route( "<int:species_id>/populations/<int:population_id>/phenotypes/datasets" "/<int:dataset_id>/view", methods=["GET"]) @require_login -@with_population(species_redirect_uri="species.populations.phenotypes.index", - redirect_uri="species.populations.phenotypes.select_population") +@with_dataset( + species_redirect_uri="species.populations.phenotypes.index", + population_redirect_uri="species.populations.phenotypes.select_population", + redirect_uri="species.populations.phenotypes.list_datasets") def view_dataset(# pylint: disable=[unused-argument] - species: dict, population: dict, dataset_id: int, **kwargs): + species: dict, population: dict, dataset: dict, **kwargs): """View a specific dataset""" with database_connection(app.config["SQL_URI"]) as conn: dataset = dataset_by_id( - conn, species["SpeciesId"], population["Id"], dataset_id) + conn, species["SpeciesId"], population["Id"], dataset["Id"]) if not bool(dataset): flash("Could not find such a phenotype dataset!", "alert-danger") return redirect(url_for( @@ -114,13 +157,13 @@ def view_dataset(# pylint: disable=[unused-argument] return render_template("phenotypes/view-dataset.html", species=species, population=population, - dataset={"Id": dataset_id}, + dataset=dataset, phenotype_count=phenotypes_count( - conn, population["Id"], dataset_id), + conn, population["Id"], dataset["Id"]), phenotypes=enumerate_sequence( dataset_phenotypes(conn, population["Id"], - dataset_id, + dataset["Id"], offset=start_at, limit=count), start=start_at+1), |