diff options
Diffstat (limited to 'uploader')
-rw-r--r-- | uploader/genotypes/views.py | 22 | ||||
-rw-r--r-- | uploader/monadic_requests.py | 32 | ||||
-rw-r--r-- | uploader/population/views.py | 28 |
3 files changed, 37 insertions, 45 deletions
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py index 9f08ca6..0821eca 100644 --- a/uploader/genotypes/views.py +++ b/uploader/genotypes/views.py @@ -1,5 +1,4 @@ """Views for the genotypes.""" -from requests.models import Response from MySQLdb.cursors import DictCursor from flask import (flash, request, @@ -14,6 +13,7 @@ from uploader.oauth2.client import oauth2_post from uploader.authorisation import require_login from uploader.db_utils import database_connection from uploader.species.models import all_species, species_by_id +from uploader.monadic_requests import make_either_error_handler from uploader.request_checks import with_species, with_population from uploader.datautils import safe_int, order_by_family, enumerate_sequence from uploader.population.models import (populations_by_species, @@ -186,21 +186,6 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable= species_id=species["SpeciesId"], population_id=population["Id"])) - def __fail__(error): - msg = "There was an error creating the genotype dataset." - if issubclass(type(error), Exception): - app.logger.debug("\n\n%s (Exception)\n\n", msg, exc_info=True) - 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("\n\n%s\n\n", msg) - raise Exception(error) - return oauth2_post( "auth/resource/genotypes/create", json={ @@ -213,4 +198,7 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable= "dataset_shortname": form["geno-dataset-shortname"], "public": "on" } - ).either(__fail__, __success__) + ).either( + make_either_error_handler( + "There was an error creating the genotype dataset."), + __success__) diff --git a/uploader/monadic_requests.py b/uploader/monadic_requests.py index aa34951..c492df5 100644 --- a/uploader/monadic_requests.py +++ b/uploader/monadic_requests.py @@ -5,13 +5,12 @@ from typing import Union, Optional, Callable import requests from requests.models import Response from pymonad.either import Left, Right, Either -from flask import ( - flash, - request, - redirect, - render_template, - current_app as app, - escape as flask_escape) +from flask import (flash, + request, + redirect, + render_template, + current_app as app, + escape as flask_escape) # HTML Status codes indicating a successful request. SUCCESS_CODES = (200, 201, 202, 203, 204, 205, 206, 207, 208, 226) @@ -84,3 +83,22 @@ def post(url, data=None, json=None, **kwargs) -> Either: return Left(resp) except requests.exceptions.RequestException as exc: return Left(exc) + + +def make_either_error_handler(msg): + """Make generic error handler for pymonads Either objects.""" + def __fail__(error): + if issubclass(type(error), Exception): + app.logger.debug("\n\n%s (Exception)\n\n", msg, exc_info=True) + 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("\n\n%s\n\n", msg) + raise Exception(error) + + return __fail__ diff --git a/uploader/population/views.py b/uploader/population/views.py index 23bf242..3638453 100644 --- a/uploader/population/views.py +++ b/uploader/population/views.py @@ -2,9 +2,7 @@ import re import json import base64 -import traceback -from requests.models import Response from MySQLdb.cursors import DictCursor from flask import (flash, request, @@ -18,10 +16,11 @@ 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.phenotypes.views import phenotypesbp -from uploader.expression_data.views import exprdatabp from uploader.db_utils import database_connection from uploader.datautils import enumerate_sequence +from uploader.phenotypes.views import phenotypesbp +from uploader.expression_data.views import exprdatabp +from uploader.monadic_requests import make_either_error_handler from uploader.species.models import (all_species, species_by_id, order_species_by_family) @@ -174,22 +173,6 @@ def create_population(species_id: int): "GeneticType": request.form.get("population_genetic_type") or None }) - 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( @@ -206,7 +189,10 @@ def create_population(species_id: int): "population_id": new_population["Id"], "public": "on" } - ).either(__handle_error__, __flash_success__) + ).either( + make_either_error_handler( + "There was an error creating the population"), + __flash_success__) @popbp.route("/<int:species_id>/populations/<int:population_id>", |