From cdc81e51888ca067b8bd254918dd8dfd1302b1e2 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 20 Apr 2021 16:52:01 +0200 Subject: formatting and method de-duplication --- talermerchantdemos/landing/landing.py | 74 ++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 28 deletions(-) (limited to 'talermerchantdemos/landing') diff --git a/talermerchantdemos/landing/landing.py b/talermerchantdemos/landing/landing.py index 170a950..071a44e 100644 --- a/talermerchantdemos/landing/landing.py +++ b/talermerchantdemos/landing/landing.py @@ -35,13 +35,17 @@ import sys if not sys.version_info.major == 3 and sys.version_info.minor >= 6: print("Python 3.6 or higher is required.") - print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor)) + print( + "You are using Python {}.{}.".format( + sys.version_info.major, sys.version_info.minor + ) + ) sys.exit(1) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = flask.Flask(__name__, template_folder=BASE_DIR) app.debug = True -app.secret_key = base64.b64encode(os.urandom(64)).decode('utf-8') +app.secret_key = base64.b64encode(os.urandom(64)).decode("utf-8") LOGGER = logging.getLogger(__name__) TC = TalerConfig.from_env() @@ -56,18 +60,21 @@ except ConfigurationError as ce: app.config.from_object(__name__) babel = Babel(app) -LOGGER.info("Using translations from:" + ':'.join(list(babel.translation_directories))) +LOGGER.info("Using translations from:" + ":".join(list(babel.translation_directories))) translations = [str(translation) for translation in babel.list_translations()] -if not 'en' in translations: - translations.append('en') -LOGGER.info("Operating with the following translations available: " + ' '.join(translations)) +if not "en" in translations: + translations.append("en") +LOGGER.info( + "Operating with the following translations available: " + " ".join(translations) +) app.jinja_env.globals.update(self_localized=self_localized) + @babel.localeselector def get_locale(): - parts = request.path.split('/', 2) - if (2 >= len(parts)): + parts = request.path.split("/", 2) + if 2 >= len(parts): # Totally unexpected path format, do not localize return "en" lang = parts[1] @@ -75,6 +82,7 @@ def get_locale(): return lang return "en" + ## # Make the environment available into templates. # @@ -108,20 +116,21 @@ def internal_error(e): "templates/error.html.j2", message=gettext("Internal error"), stack=traceback.format_exc(), - lang=get_locale() + lang=get_locale(), ) + ## # Serve the /favicon.ico requests. # # @return the favicon.ico file. @app.route("/favicon.ico") def favicon(): - LOGGER.info("will look into: " + os.path.join(app.root_path, 'static')) + LOGGER.info("will look into: " + os.path.join(app.root_path, "static")) return flask.send_from_directory( - os.path.join(app.root_path, 'static'), + os.path.join(app.root_path, "static"), "favicon.ico", - mimetype="image/vnd.microsoft.ico" + mimetype="image/vnd.microsoft.ico", ) @@ -131,10 +140,11 @@ def favicon(): # @return response object of the index page. @app.route("/") def index(): - default = 'en' + default = "en" target = flask.request.accept_languages.best_match(translations, default) return flask.redirect("/" + target + "/", code=302) + ## # Serve the internationalized main index page. # @@ -142,17 +152,25 @@ def index(): @app.route("//", methods=["GET"]) def start(lang): - if x:= os.environ.get("TALER_ENV_URL_BANK"): bank_register_url = "/".join([x.strip("/"), f"{lang}/register"]) - else: bank_register_url = "#" + if x := os.environ.get("TALER_ENV_URL_BANK"): + bank_register_url = "/".join([x.strip("/"), f"{lang}/register"]) + else: + bank_register_url = "#" - if x:= os.environ.get("TALER_ENV_URL_MERCHANT_BLOG"): merchant_blog_url = "/".join([x.strip("/"), lang]) - else: merchant_blog_url = "#" + if x := os.environ.get("TALER_ENV_URL_MERCHANT_BLOG"): + merchant_blog_url = "/".join([x.strip("/"), lang]) + else: + merchant_blog_url = "#" - if x:= os.environ.get("TALER_ENV_URL_MERCHANT_DONATIONS"): merchant_donations_url = "/".join([x.strip("/"), lang]) - else: merchant_donations_url = "#" + if x := os.environ.get("TALER_ENV_URL_MERCHANT_DONATIONS"): + merchant_donations_url = "/".join([x.strip("/"), lang]) + else: + merchant_donations_url = "#" - if x:= os.environ.get("TALER_ENV_URL_MERCHANT_SURVEY"): merchant_survey_url = "/".join([x.strip("/"), lang]) - else: merchant_survey_url = "#" + if x := os.environ.get("TALER_ENV_URL_MERCHANT_SURVEY"): + merchant_survey_url = "/".join([x.strip("/"), lang]) + else: + merchant_survey_url = "#" return flask.render_template( "templates/index.html.j2", @@ -161,21 +179,21 @@ def start(lang): bank_url=bank_register_url, merchant_blog_url=merchant_blog_url, merchant_donations_url=merchant_donations_url, - merchant_survey_url=merchant_survey_url + merchant_survey_url=merchant_survey_url, ) + @app.errorhandler(404) -def handler(e): +def handler_404(e): return flask.render_template( - "templates/error.html.j2", - message=gettext("Page not found"), - lang=get_locale() + "templates/error.html.j2", message=gettext("Page not found"), lang=get_locale() ) + @app.errorhandler(405) -def handler(e): +def handler_405(e): return flask.render_template( "templates/error.html.j2", message=gettext("HTTP method not allowed for this page"), - lang=get_locale() + lang=get_locale(), ) -- cgit v1.2.3