From 0729e38e2f5bbc5ab23153adfed3d35ee59dc3d5 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 27 Sep 2024 11:40:08 -0500 Subject: Extract common functionality into reusable function. --- uploader/population/views.py | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) (limited to 'uploader/population') 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("//populations/", -- cgit v1.2.3