diff options
Diffstat (limited to 'uploader/route_utils.py')
| -rw-r--r-- | uploader/route_utils.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/uploader/route_utils.py b/uploader/route_utils.py index ce718fb..4449475 100644 --- a/uploader/route_utils.py +++ b/uploader/route_utils.py @@ -1,11 +1,22 @@ """Generic routing utilities.""" -from flask import flash, url_for, redirect, render_template, current_app as app +import logging +from json.decoder import JSONDecodeError + +from flask import (flash, + request, + redirect, + render_template, + current_app as app) from gn_libs.mysqldb import database_connection +from uploader.flask_extensions import url_for +from uploader.datautils import base64_encode_dict, base64_decode_to_dict from uploader.population.models import (populations_by_species, population_by_species_and_id) +logger = logging.getLogger(__name__) + def generic_select_population( # pylint: disable=[too-many-arguments, too-many-positional-arguments] species: dict, @@ -40,3 +51,40 @@ def generic_select_population( return redirect(url_for(forward_to, species_id=species["SpeciesId"], population_id=population["Id"])) + + +def redirect_to_next(default: dict): + """Redirect to the next uri if specified, else redirect to default.""" + assert "uri" in default, "You must provide at least the 'uri' value." + try: + next_page = base64_decode_to_dict(request.args.get("next")) + _uri = next_page["uri"] + next_page.pop("uri") + return redirect(url_for(_uri, **next_page)) + except (TypeError, JSONDecodeError) as _err: + logger.debug("We could not decode the next value '%s'", + next_page, + exc_info=True) + + return redirect(url_for( + default["uri"], + **{key:value for key,value in default.items() if key != "uri"})) + + +def build_next_argument(uri: str, **kwargs) -> str: + """Build the `next` URI argument from provided details.""" + dumps_keywords = ( + "skipkeys", "ensure_ascii", "check_circular", "allow_nan", "cls", + "indent", "separators", "default", "sort_keys") + return base64_encode_dict( + { + "uri": uri, + **{ + key: val for key,val in kwargs.items() + if key not in dumps_keywords + } + }, + **{ + key: val for key,val in kwargs.items() + if key in dumps_keywords + }) |
