From 09fc4bbc3ef66b02f0711dadaf3b10ed53d9d968 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 10 Dec 2025 12:57:14 -0600 Subject: Feature Flags: Generically deal with HTTP-based feature flags. * Define a default `FEATURE_FLAGS_HTTP` configuration variable that's an empty list to help defining http-based feature flags that can be used to turn on/off features * Build macro to include hidden fields for feature flags where necessary. * Extend flask's `url_for` function to deal with defined feature flags in a mostly transparent way --- uploader/flask_extensions.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 uploader/flask_extensions.py (limited to 'uploader/flask_extensions.py') diff --git a/uploader/flask_extensions.py b/uploader/flask_extensions.py new file mode 100644 index 0000000..30fbad7 --- /dev/null +++ b/uploader/flask_extensions.py @@ -0,0 +1,33 @@ +"""Custom extensions to the default flask functions/classes.""" +import logging +from typing import Any, Optional + +from flask import (request, current_app as app, url_for as flask_url_for) + +logger = logging.getLogger(__name__) + + +def url_for( + endpoint: str, + _anchor: Optional[str] = None, + _method: Optional[str] = None, + _scheme: Optional[str] = None, + _external: Optional[bool] = None, + **values: Any) -> str: + """Extension to flask's `url_for` function.""" + flags = {} + for flag in app.config["FEATURE_FLAGS_HTTP"]: + flag_value = (request.args.get(flag) or request.form.get(flag) or "").strip() + if bool(flag_value): + flags[flag] = flag_value + continue + continue + + logger.debug("HTTP FEATURE FLAGS: %s, other variables: %s", flags, values) + return flask_url_for(endpoint=endpoint, + _anchor=_anchor, + _method=_method, + _scheme=_scheme, + _external=_external, + **values, + **flags) -- cgit 1.4.1