aboutsummaryrefslogtreecommitdiff
path: root/uploader/route_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/route_utils.py')
-rw-r--r--uploader/route_utils.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/uploader/route_utils.py b/uploader/route_utils.py
new file mode 100644
index 0000000..18eadda
--- /dev/null
+++ b/uploader/route_utils.py
@@ -0,0 +1,41 @@
+"""Generic routing utilities."""
+from flask import flash, url_for, redirect, render_template, current_app as app
+
+from gn_libs.mysqldb import database_connection
+
+from uploader.population.models import (populations_by_species,
+ population_by_species_and_id)
+
+def generic_select_population(# pylint: disable=[too-many-arguments]
+ species: dict,
+ template: str,
+ population_id: str,
+ back_to: str,
+ forward_to: str,
+ activelink: str,
+ error_message: str = "No such population found!"
+):
+ """Handles common flow for 'select population' step."""
+ with database_connection(app.config["SQL_URI"]) as conn:
+ if not bool(population_id):
+ return render_template(
+ template,
+ species=species,
+ populations=populations_by_species(conn, species["SpeciesId"]),
+ activelink=activelink)
+
+ if population_id == "CREATE-POPULATION":
+ return redirect(url_for(
+ "species.populations.create_population",
+ species_id=species["SpeciesId"],
+ return_to=forward_to))
+
+ population = population_by_species_and_id(
+ conn, species["SpeciesId"], int(population_id))
+ if not bool(population):
+ flash(error_message, "alert-danger")
+ return redirect(url_for(back_to, species_id=species["SpeciesId"]))
+
+ return redirect(url_for(forward_to,
+ species_id=species["SpeciesId"],
+ population_id=population["Id"]))