[Kimchi-devel] [PATCH] [Wok] Fix to provide date convertion as per locale

Aline Manera alinefm at linux.vnet.ibm.com
Thu Dec 1 12:51:53 UTC 2016


Hi,

On 11/29/2016 04:29 AM, jkatta at linux.vnet.ibm.com wrote:
> From: Jayavardhan Katta <jkatta at linux.vnet.ibm.com>
>
> This fix provides template and utils methods for the date convertion
> as per the locale settings from UI login page
>
> Signed-off-by: Jayavardhan Katta <jkatta at linux.vnet.ibm.com>
> ---
>   src/wok/template.py | 26 +++++++++++++++++++++++++-
>   src/wok/utils.py    | 10 ++++++++++
>   2 files changed, 35 insertions(+), 1 deletion(-)
>
> diff --git a/src/wok/template.py b/src/wok/template.py
> index 43a34db..e8d083e 100644
> --- a/src/wok/template.py
> +++ b/src/wok/template.py
> @@ -22,10 +22,13 @@
>   import cherrypy
>   import errno
>   import json
> +import threading
>   import time
> +import locale
> +
>   from Cheetah.Template import Template
>   from glob import iglob
> -
> +from contextlib import contextmanager
>
>   from wok import config as config
>   from wok.config import paths
> @@ -133,3 +136,24 @@ def render(resource, data):
>           return render_cheetah_file(resource, data)
>       else:
>           raise cherrypy.HTTPError(406)
> +


> +
> +def get_locale():
> +    cookie = cherrypy.request.cookie
> +    if "wokLocale" in cookie.keys():
> +        return [cookie["wokLocale"].value]
> +    langs = get_lang()
> +    return langs

I am not seeing the clue between the locale used on backend and UI.
On UI, the locale settings is defined in the drop down, so when there is 
no 'wokLocale' cookie it fallbacks to the browser language settings and 
compare the values. If the browser language is one of the values in the 
drop down, that is used as locale otherwise, it fallbacks to 'en-US'

Seems the backend will always fallback to the browser language settings 
so in that case, the UI can use a value and the backend a different one.

Does that make sense to you?

Maybe, the locale settings should be send in the request as a header so 
we guarantee UI and backend using the same value.

> +
> +LOCALE_LOCK = threading.Lock()
> +
> +
> + at contextmanager
> +def setlocale(name):
> +    with LOCALE_LOCK:
> +        saved = locale.setlocale(locale.LC_TIME)
> +        try:
> +            yield locale.setlocale(locale.LC_TIME, name)
> +        finally:
> +            locale.setlocale(locale.LC_TIME, saved)
> diff --git a/src/wok/utils.py b/src/wok/utils.py
> index 9a08001..807893d 100644
> --- a/src/wok/utils.py
> +++ b/src/wok/utils.py
> @@ -42,6 +42,7 @@ from threading import Timer
>   from wok.config import paths, PluginPaths
>   from wok.exception import InvalidParameter, TimeoutExpired
>   from wok.stringutils import decode_value
> +from wok.template import get_locale, setlocale
>
>
>   wok_log = cherrypy.log.error_log
> @@ -619,3 +620,12 @@ def upgrade_objectstore_schema(objstore=None, field=None):
>           wok_log.error("Cannot upgrade objectstore schema: %s" % e.args[0])
>           return False
>       return True
> +
> +
> +def dateNtime_to_locale(dateNtime):
> +    dt = datetime.strptime(dateNtime, "%Y-%m-%d %H:%M:%S")
> +    cokies_locale = get_locale()
> +    cokies_locale = cokies_locale[0].replace("-", "_")
> +    with setlocale(cokies_locale):
> +        dt_locale = (dt.strftime('%c'))
> +    return dt_locale




More information about the Kimchi-devel mailing list