aboutsummaryrefslogtreecommitdiff
path: root/uploader/population/views.py
blob: cd5e20b0f70e97fc02c750ed3011b589e786d086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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 …")