From 1eb3d3269c367fa67525c3b65470045b9038582d Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 23 Oct 2013 16:57:29 +0200 Subject: [PATCH] [ADD] parsing of localized date & datetime t-fields turns out we use a bunch of them in the events section thing bzr revid: xmo@openerp.com-20131023145729-tglusl27mt6ajwzn --- addons/website/models/ir_qweb.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/addons/website/models/ir_qweb.py b/addons/website/models/ir_qweb.py index 948422e..d6bd6a5 100644 --- a/addons/website/models/ir_qweb.py +++ b/addons/website/models/ir_qweb.py @@ -7,17 +7,19 @@ Also, adds methods to convert values back to openerp models. """ import cStringIO +import datetime import itertools import logging import re import urllib2 import werkzeug.utils +from dateutil import parser from lxml import etree, html from PIL import Image as I from openerp.osv import orm, fields -from openerp.tools import ustr +from openerp.tools import ustr, DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT REMOTE_CONNECTION_TIMEOUT = 2.5 @@ -75,19 +77,48 @@ class Float(orm.AbstractModel): return float(value.replace(lang.thousands_sep, '') .replace(lang.decimal_point, '.')) + +def parse_fuzzy(in_format, value): + day_first = in_format.find('%d') < in_format.find('%m') + + if '%y' in in_format: + year_first = in_format.find('%y') < in_format.find('%d') + else: + year_first = in_format.find('%Y') < in_format.find('%d') + + return parser.parse(value, dayfirst=day_first, yearfirst=year_first) + class Date(orm.AbstractModel): _name = 'website.qweb.field.date' _inherit = ['website.qweb.field', 'ir.qweb.field.date'] def from_html(self, cr, uid, model, column, element, context=None): - raise NotImplementedError("Can not parse and save localized dates") + lang = self.user_lang(cr, uid, context=context) + in_format = lang.date_format.encode('utf-8') + + value = element.text_content().strip() + try: + dt = datetime.datetime.strptime(in_format, value) + except ValueError: + dt = parse_fuzzy(in_format, value) + + return dt.strftime(DEFAULT_SERVER_DATE_FORMAT) class DateTime(orm.AbstractModel): _name = 'website.qweb.field.datetime' _inherit = ['website.qweb.field', 'ir.qweb.field.datetime'] def from_html(self, cr, uid, model, column, element, context=None): - raise NotImplementedError("Can not parse and save localized datetimes") + lang = self.user_lang(cr, uid, context=context) + in_format = (u"%s %s" % (lang.date_format, lang.time_format)).encode('utf-8') + + value = element.text_content().strip() + try: + dt = datetime.datetime.strptime(in_format, value) + except ValueError: + dt = parse_fuzzy(in_format, value) + + return dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT) class Text(orm.AbstractModel): _name = 'website.qweb.field.text' -- 1.7.10.4