diff options
Diffstat (limited to 'uploader/population/views.py')
-rw-r--r-- | uploader/population/views.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/uploader/population/views.py b/uploader/population/views.py new file mode 100644 index 0000000..cd5e20b --- /dev/null +++ b/uploader/population/views.py @@ -0,0 +1,120 @@ +"""Views dealing with populations/inbredsets""" +from flask import (flash, + request, + url_for, + redirect, + Blueprint, + current_app as app) + +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, + order_species_by_family) + +from .models import (save_population, + populations_by_species) + +__active_link__ = "populations" +popbp = Blueprint("populations", __name__) +render_template = make_template_renderer("populations") + + +@popbp.route("/", methods=["GET", "POST"]) +@require_login +def index(): + """Entry point for populations.""" + with database_connection(app.config["SQL_URI"]) as conn: + if not bool(request.args.get("species_id")): + return render_template( + "populations/index.html", + species=order_species_by_family(all_species(conn))) + species = species_by_id(conn, request.args.get("species_id")) + if not bool(species): + flash("Invalid species identifier provided!", "alert-danger") + return redirect(url_for("populations.index")) + return redirect(url_for("populations.list_species_populations", + species_id=species["SpeciesId"])) + +@popbp.route("/<int:species_id>", methods=["GET"]) +@require_login +def list_species_populations(species_id: int): + """List a particular species' populations.""" + with database_connection(app.config["SQL_URI"]) as conn: + species = species_by_id(conn, species_id) + if not bool(species): + flash("No species was found for given ID.", "alert-danger") + return redirect(url_for("populations.index")) + return render_template( + "populations/list-populations.html", + species=species, + populations=populations_by_species(conn, species_id), + activelink="list-populations") + + +def valid_population_name(population_name) -> bool: + """Check whether the given name is a valid population name.""" + raise NotImplementedError("Please implement this…") + + +@popbp.route("/<int:species_id>/create-population/", methods=["GET", "POST"]) +@require_login +def create_population(species_id: int): + """Create a new population.""" + with database_connection(app.config["SQL_URI"]) as conn: + species = species_by_id(conn, species_id) + + if request.method == "GET": + return render_template( + "populations/create-population.html", + species=species, + continue_to=request.args.get("continue_uri")) + + error = False + + if not bool(species): + flash("You must select a species.", "alert-danger") + error = True + + population_name = request.form.get("population_name", "").strip() + if not bool(population_name): + flash("You must provide a name for the population!", "alert-danger") + error = True + + if not valid_population_name(population_name): + flash("The population name can only contain letters, numbers, " + "hyphens and underscores.", + "alert-danger") + error = True + + population_fullname = request.form.get("inbredset_fullname", "").strip() + if not bool(population_fullname): + flash("You MUST provide a Full Name for the population." + "alert-danger") + error = True + + if error: + return redirect(url_for("populations.create_population", + **dict(request.args))) + + new_population = save_population(conn, { + "SpeciesId": species["SpeciesId"], + "Name": population_name, + "InbredSetName": population_fullname, + "FullName": population_fullname, + "Family": request.form.get("inbredset_family") or None, + "Description": request.form.get("description") or None + }) + + return redirect(url_for("populations.view_population", + species_id=species["SpeciesId"], + population_id=new_population["InbredSetId"])) + + +@popbp.route("/<int:species_id>/populations/<int:population_id>", + methods=["GET"]) +@require_login +def view_population(species_id: int, population_id: int): + """View the details of a population.""" + raise NotImplementedError("Please implement this too …") |