aboutsummaryrefslogtreecommitdiff
path: root/uploader/population/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/population/views.py')
-rw-r--r--uploader/population/views.py57
1 files changed, 48 insertions, 9 deletions
diff --git a/uploader/population/views.py b/uploader/population/views.py
index 003787a..39a5762 100644
--- a/uploader/population/views.py
+++ b/uploader/population/views.py
@@ -2,7 +2,10 @@
import re
import json
import base64
+import traceback
+from requests.models import Response
+from MySQLdb.cursors import DictCursor
from flask import (flash,
request,
url_for,
@@ -10,10 +13,13 @@ from flask import (flash,
Blueprint,
current_app as app)
+from uploader.samples.views import samplesbp
+from uploader.oauth2.client import oauth2_post
from uploader.ui import make_template_renderer
from uploader.authorisation import require_login
+from uploader.genotypes.views import genotypesbp
from uploader.db_utils import database_connection
-from uploader.samples.views import samplesbp
+from uploader.datautils import enumerate_sequence
from uploader.species.models import (all_species,
species_by_id,
order_species_by_family)
@@ -27,6 +33,7 @@ from .models import (save_population,
__active_link__ = "populations"
popbp = Blueprint("populations", __name__)
popbp.register_blueprint(samplesbp, url_prefix="/")
+popbp.register_blueprint(genotypesbp, url_prefix="/")
render_template = make_template_renderer("populations")
@@ -58,7 +65,8 @@ def list_species_populations(species_id: int):
return render_template(
"populations/list-populations.html",
species=species,
- populations=populations_by_species(conn, species_id),
+ populations=enumerate_sequence(populations_by_species(
+ conn, species_id)),
activelink="list-populations")
@@ -89,7 +97,8 @@ def valid_population_name(population_name: str) -> bool:
@require_login
def create_population(species_id: int):
"""Create a new population."""
- with database_connection(app.config["SQL_URI"]) as conn:
+ with (database_connection(app.config["SQL_URI"]) as conn,
+ conn.cursor(cursorclass=DictCursor) as cursor):
species = species_by_id(conn, species_id)
if request.method == "GET":
@@ -100,7 +109,7 @@ def create_population(species_id: int):
).decode("utf8")
error_values = json.loads(base64.b64decode(
- error_values.encode("utf8")).decode("utf8"))
+ error_values.encode("utf8")).decode("utf8"))# type: ignore[union-attr]
return render_template(
"populations/create-population.html",
species=species,
@@ -119,7 +128,7 @@ def create_population(species_id: int):
flash("You must select a species.", "alert-danger")
return redirect(url_for("species.populations.index"))
- errors = tuple()
+ errors: tuple[tuple[str, str], ...] = tuple()
population_name = (request.form.get(
"population_name") or "").strip()
@@ -149,7 +158,7 @@ def create_population(species_id: int):
species_id=species["SpeciesId"],
error_values=values))
- new_population = save_population(conn, {
+ new_population = save_population(cursor, {
"SpeciesId": species["SpeciesId"],
"Name": population_name,
"InbredSetName": population_fullname,
@@ -161,9 +170,39 @@ def create_population(species_id: int):
"GeneticType": request.form.get("population_genetic_type") or None
})
- return redirect(url_for("species.populations.view_population",
- species_id=species["SpeciesId"],
- population_id=new_population["InbredSetId"]))
+ def __handle_error__(error):
+ error_format = (
+ "\n\nThere was an error creating the population:\n\t%s\n\n")
+ if issubclass(type(error), Exception):
+ app.logger.debug(error_format, traceback.format_exc())
+ raise error
+ if issubclass(type(error), Response):
+ try:
+ _data = error.json()
+ except Exception as _exc:
+ raise Exception(error.content) from _exc
+ raise Exception(_data)
+
+ app.logger.debug(error_format, error)
+ raise Exception(error)
+
+ def __flash_success__(_success):
+ flash("Successfully created resource.", "alert-success")
+ return redirect(url_for(
+ "species.populations.view_population",
+ species_id=species["SpeciesId"],
+ population_id=new_population["InbredSetId"]))
+
+ app.logger.debug("We begin setting up the privileges hereā€¦")
+ return oauth2_post(
+ "auth/resource/populations/create",
+ json={
+ **dict(request.form),
+ "species_id": species_id,
+ "population_id": new_population["Id"],
+ "public": "on"
+ }
+ ).either(__handle_error__, __flash_success__)
@popbp.route("/<int:species_id>/populations/<int:population_id>",