[Kimchi-devel] [PATCH] [Wok] Fix to provide date convertion as per locale
jkatta at linux.vnet.ibm.com
jkatta at linux.vnet.ibm.com
Tue Nov 29 06:29:47 UTC 2016
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
+
+
+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
--
2.7.4
More information about the Kimchi-devel
mailing list