From 1ae0306a3cf2ea27f60b2d205789994d260c2cce Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 11 Oct 2020 13:29:45 +0200 Subject: add i18n FSFS --- talermerchantdemos/httpcommon/__init__.py | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'talermerchantdemos/httpcommon') diff --git a/talermerchantdemos/httpcommon/__init__.py b/talermerchantdemos/httpcommon/__init__.py index 1036f33..d07fe69 100644 --- a/talermerchantdemos/httpcommon/__init__.py +++ b/talermerchantdemos/httpcommon/__init__.py @@ -1,6 +1,10 @@ import flask import requests from urllib.parse import urljoin +from flask import request +from datetime import datetime +import time + ## # Return a error response to the client. @@ -79,5 +83,50 @@ def backend_get(backend_url, endpoint, params): print("Backend responds to {}: {}".format(final_url, str(response_json))) return response_json +## +# Helper function used inside Jinja2 logic to create a links +# to the current page but in a different language. Used to +# implement the "Language" menu. +# +def self_localized(lang): + """ + Return URL for the current page in another locale. + """ + path = request.path + # path must have the form "/$LANG/$STUFF" + parts = path.split('/', 2) + if (2 >= len(parts)): + # Totally unexpected path format, do not localize + return path + return "/" + lang + "/" + parts[2] + + +## +# Return a error response to the client. +# +# @param abort_status_code status code to return along the response. +# @param params _kw_ arguments to passed verbatim to the templating engine. +def err_abort(abort_status_code, **params): + t = flask.render_template("templates/error.html.j2", **params) + flask.abort(flask.make_response(t, abort_status_code)) + + + def fallback_404(error): return "Page not found" + + + +class Deadline: + def __init__(self, value): + self.value = value + def isExpired(self): + if self.value == "never": + return False + now = int(round(time.time()) * 1000) + now_dt = datetime.fromtimestamp(now / 1000) + deadline_dt = datetime.fromtimestamp(self.value / 1000) + print("debug: checking refund expiration, now: {}, deadline: {}".format( + now_dt.strftime("%c"), deadline_dt.strftime("%c") + )) + return now > self.value -- cgit v1.2.3