diff options
author | Frederick Muriuki Muriithi | 2024-09-16 15:46:16 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-16 15:46:16 -0500 |
commit | e0d831a003af8732dfc281a263d34a0ed8918ab5 (patch) | |
tree | b3310a3bcc14c397d1892030615728ede4be9fbb /uploader/population | |
parent | 1ff6fb43ceb22f55b94f15a6d875cdadd9d70244 (diff) | |
download | gn-uploader-e0d831a003af8732dfc281a263d34a0ed8918ab5.tar.gz |
Use new endpoint to create resource and assign appropriate roles.
Diffstat (limited to 'uploader/population')
-rw-r--r-- | uploader/population/views.py | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/uploader/population/views.py b/uploader/population/views.py index af0a043..5136ee0 100644 --- a/uploader/population/views.py +++ b/uploader/population/views.py @@ -4,8 +4,8 @@ import json import base64 import traceback +from requests.models import Response from MySQLdb.cursors import DictCursor -from pymonad.either import Left, Right, Either from flask import (flash, request, url_for, @@ -14,11 +14,11 @@ from flask import (flash, 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.db_utils import database_connection from uploader.genotypes.views import genotypesbp -from uploader.oauth2.client import oauth2_get, oauth2_post +from uploader.db_utils import database_connection from uploader.species.models import (all_species, species_by_id, order_species_by_family) @@ -107,7 +107,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, @@ -126,7 +126,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() @@ -168,48 +168,39 @@ def create_population(species_id: int): "GeneticType": request.form.get("population_genetic_type") or None }) - def __select_inbredset_resource_category__(categories) -> Either: - from uploader.debug import __pk__ - _cat = tuple( - category for category in categories - if category["resource_category_key"] == "inbredset-group") - if len(_cat) == 0: - return Left("Could not find appropriate category key.") - if len(_cat) > 1: - return Left( - f"Invalid categories — Expected one, got {{len(_cat)}}") - return Right(_cat[0]) - 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) - return oauth2_get( - "auth/resource/categories" - ).then( - __select_inbredset_resource_category__ - ).then( - lambda category: oauth2_post( - "auth/resource/create", - json={ - "resource_name": f"Population — {population_name}", - "resource_category": category["resource_category_id"], - "public": "on" - }) - ).then( - lambda resp: flash("Successfully created resource.", - "alert-success") or resp - ).either( - __handle_error__, - lambda _success: redirect(url_for( + 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"]))) + 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>", |