diff options
Diffstat (limited to 'uploader/route_utils.py')
| -rw-r--r-- | uploader/route_utils.py | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/uploader/route_utils.py b/uploader/route_utils.py index ce718fb..63b2852 100644 --- a/uploader/route_utils.py +++ b/uploader/route_utils.py @@ -1,8 +1,17 @@ """Generic routing utilities.""" -from flask import flash, url_for, redirect, render_template, current_app as app +import json +from json.decoder import JSONDecodeError + +from flask import (flash, + url_for, + request, + redirect, + render_template, + current_app as app) from gn_libs.mysqldb import database_connection +from uploader.datautils import base64_encode_dict, base64_decode_to_dict from uploader.population.models import (populations_by_species, population_by_species_and_id) @@ -40,3 +49,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")) + return redirect(url_for( + next_page["uri"], + **{key:value for key,value in next_page.items()})) + 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()})) + + +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 + }) |
