"""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"]))