~openerp-dev/openobject-client-web/6.0-opw-591397-xal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
###############################################################################
#
#  Copyright (C) 2007-TODAY OpenERP SA. All Rights Reserved.
#
#  $Id$
#
#  Developed by OpenERP (http://openerp.com) and Axelor (http://axelor.com).
#
#  The OpenERP web client is distributed under the "OpenERP Public License".
#  It's based on Mozilla Public License Version (MPL) 1.1 with following 
#  restrictions:
#
#  -   All names, links and logos of OpenERP must be kept as in original
#      distribution without any changes in all software screens, especially
#      in start-up page and the software header, even if the application
#      source code has been changed or updated or code has been added.
#
#  You can see the MPL licence at: http://www.mozilla.org/MPL/MPL-1.1.html
#
###############################################################################
import datetime as DT
import re
import time

import cherrypy
import pytz
from babel import dates, numbers

from openobject.i18n.utils import get_locale
#from openerp.utils import cache
#from openerp.utils import rpc

__all__ = ['DT_SERVER_FORMATS', 'get_datetime_format',
           'format_datetime', 'parse_datetime',
           'format_decimal', 'parse_decimal',
           'tz_convert'
          ]

DT_SERVER_FORMATS = {
  'datetime' : '%Y-%m-%d %H:%M:%S',
  'date' : '%Y-%m-%d',
  'time' : '%H:%M:%S'
}

__pat = re.compile("%\(([dMy]+)\)s")
__sub = {'d': '%d', 'M': '%m', 'y': '%Y'}
def _to_posix_format(format):
    """Convert LDML format string to posix format string.
    """
    return __pat.sub(lambda m: __sub[m.group(1)[0]], format)

def format_date_custom(dt, fmt="y-M-d"):
    return dates.format_date(dt, format=fmt, locale=get_locale())

def get_datetime_format(kind="datetime"):
    """Get local datetime format.

    @param kind: type (date, time or datetime)
    @return: string

    @todo: cache formats to improve performance.
    """
    if 'lang' in cherrypy.session:
        # server-defined formatting
        if kind == 'time':
            return cherrypy.session['lang']['time_format']
        elif kind == 'date':
            return cherrypy.session['lang']['date_format']
        else:
            return "%(date_format)s %(time_format)s"% cherrypy.session['lang']

    # TODO: correctly convert from LDML to POSIX datetime formatting
    # current converter is trivial and lame and probably very easy to break
    date_format = _to_posix_format(dates.get_date_format(
            format='short', locale=get_locale())).format
    if kind == 'time':
        # Should use dates.get_time_format(locale=get_locale())
        return '%H:%M:%S'
    elif kind == 'date':
        return date_format
    else:
        # Should use dates.get_datetime_format, but that one returns
        # a 2.6-style formats
        return "%s %s" % (date_format, '%H:%M:%S')

def tz_convert(struct_time, action):
    # if no client timezone is configured, consider the client is in the same
    # timezone as the server
    lzone = pytz.timezone(cherrypy.session['client_timezone']
                          or cherrypy.session['remote_timezone'])
    szone = pytz.timezone(cherrypy.session['remote_timezone'])
    dt = DT.datetime.fromtimestamp(time.mktime(struct_time))

    if action == 'parse':
        fromzone = lzone
        tozone = szone
    elif action == 'format':
        fromzone = szone
        tozone = lzone
    else:
        raise Exception("_tz_convert action should be 'parse' or 'format'. Not '%s'" % (action, ))

    localized_original_datetime = fromzone.localize(dt, is_dst=True)
    destination_datetime = localized_original_datetime.astimezone(tozone)
    return destination_datetime.timetuple()

def format_datetime(value, kind="datetime", as_timetuple=False):
    """Convert date value to the local datetime considering timezone info.

    @param value: the date value
    @param kind: type of the date value (date, time or datetime)
    @param as_timetuple: return timetuple

    @type value: basestring or time.time_tuple)

    @return: string or timetuple
    """

    server_format = DT_SERVER_FORMATS[kind]
    local_format = get_datetime_format(kind)

    if not value:
        return ''

    if isinstance(value, (time.struct_time, tuple)):
        value = time.strftime(server_format, value)

    if isinstance(value, DT.datetime):
        value = ustr(value)
        try:
            value = DT.datetime.strptime(value[:10], server_format)
            return value.strftime(local_format)
        except:
            return ''

    value = value.strip()

    # remove trailing miliseconds
    value = re.sub("(.*?)(\s+\d{2}:\d{2}:\d{2})(\.\d+)?$", "\g<1>\g<2>", value)

    # add time part in value if missing
    if kind == 'datetime' and not re.search('\s+\d{2}:\d{2}:\d{2}?$', value):
        value += ' 00:00:00'

    # remove time part from value
    elif kind == 'date':
        value = re.sub('\s+\d{2}:\d{2}:\d{2}(\.\d+)?$', '', value)

    value = time.strptime(value, server_format)

    if kind == "datetime":
        try:
            value = tz_convert(value, 'format')
        except Exception:
            cherrypy.log.error("Error in timezone formatting:\n", traceback=True)

    if as_timetuple:
        return value

    return time.strftime(local_format, value)

def parse_datetime(value, kind="datetime", as_timetuple=False):
    """Convert date value to the server datetime considering timezone info.

    @param value: the date value
    @param kind: type of the date value (date, time or datetime)
    @param as_timetuple: return timetuple

    @type value: basestring or time.time_tuple)

    @return: string or timetuple
    """

    server_format = DT_SERVER_FORMATS[kind]
    local_format = get_datetime_format(kind)

    if not value:
        return False

    if isinstance(value, (time.struct_time, tuple)):
        value = time.strftime(local_format, value)

    try:
        value = time.strptime(value, local_format)
    except ValueError:
        try:
            # might be in server format already (e.g. filter domain)
            value = time.strptime(value, server_format)
        except ValueError:
            try:
                dt = list(time.localtime())
                dt[2] = int(value)
                value = tuple(dt)
            except:
                return False

    if kind == "datetime":
        try:
            value = tz_convert(value, 'parse')
        except Exception:
            cherrypy.log.error("Error in timezone parsing:\n", traceback=True)

    if as_timetuple:
        return value

    return time.strftime(server_format, value)

def convert_date_format_in_domain(domain, fields, context):
    try:
        return _convert_date_format_in_domain(domain, fields, context)
    except Exception, e:
        cherrypy.log.error("Error in convert_date_format_in_domain:\n", traceback=True)
        return domain

def _convert_date_format_in_domain(domain, fields, context):
    from view_calendar.widgets.utils import DT_FORMAT_INFO
    from openerp.utils import rpc

    date_fields = dict([(field_name, field_def['type'])
                            for field_name, field_def
                                in fields.items()
                                    if field_def['type'] in ['date', 'datetime', 'time']])

    if 'lang' not in cherrypy.session:
        return domain
    lang_def = cherrypy.session['lang']
    fixed_domain = []

    # not supported on all systems: %C %D %e %F %g %G %h %l %P %r %R %s %T %u %V %z
    time_format_convert_map = {
        '%D': '%m/%d/%y',
        '%e': '%d',
        '%F': '%Y-%m-%d',
        '%g': '%y',
        '%h': '%b',
        '%l': '%I',
        '%P': '%p',
        '%R': '%H:%M',
        '%r': '%I:%M:%S %p',
        '%T': '%H:%M:%S',
        '%z': '%Z',
    }

    for item in domain:
        if len(item) != 3:
            fixed_domain.append(item)
        else:
            key, op, val = item
            if key in date_fields:
                dtype = date_fields[key]
                if dtype == 'date':
                    user_dformat = lang_def['date_format']
                    server_dformat = DT_FORMAT_INFO['date'][0]
                elif dtype == 'time':
                    user_dformat = lang_def['time_format']
                    server_dformat = DT_FORMAT_INFO['time'][0]
                elif dtype == 'datetime':
                    user_dformat = lang_def['date_format'] + ' ' + lang_def['time_format']
                    server_dformat = DT_FORMAT_INFO['datetime'][0]

                ok = True
                for k, v in time_format_convert_map.items():
                    if k in user_dformat:
                        user_dformat = user_dformat.replace(k, v)
                if re.findall(r'%[CGsuV]', user_dformat):
                    ok = False

                value_is_list = isinstance(val, list)
                if value_is_list:
                    val = val[0]

                if ok:
                    val = parse_datetime(val, dtype)
                    if val:
                        val = DT.datetime.strptime(
                                DT.datetime.strptime(val, server_dformat)\
                                    .strftime(user_dformat), user_dformat)\
                            .strftime(server_dformat)
                    else:
                        formated_date = format_datetime(val, dtype)
                        val = parse_datetime(formated_date, dtype)
                        if val:
                            val = DT.datetime.strptime(
                                    DT.datetime.strptime(val, server_dformat)\
                                        .strftime(user_dformat), user_dformat)\
                                .strftime(server_dformat)
                if value_is_list:
                    val = [val]
            fixed_domain.append((key, op, val))

    return fixed_domain


def get_lang_float_format(locale_lang,monetary=False):
    thousands_sep = cherrypy.session['lang'].get('thousands_sep') or numbers.get_group_symbol(locale_lang)
    decimal_point = cherrypy.session['lang'].get('decimal_point')
    grouping      = cherrypy.session['lang'].get('grouping')
    return (grouping, thousands_sep, decimal_point)


def format_decimal(value, digits=2, grouping=True, monetary=False):
    locale = cherrypy.session['lang'].get('code') or get_locale()
    formatted = ("%%.%df" % digits) % value
    lang_grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
    
    seps = 0
    parts = formatted.split('.')

    if grouping:
        parts[0], seps = group(parts[0], monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep)

    formatted = decimal_point.join(parts)
    while seps:
        sp = formatted.find(' ')
        if sp == -1: break
        formatted = formatted[:sp] + formatted[sp+1:]
        seps -= 1
    return formatted


def group(value, monetary=False, grouping=False, thousands_sep=''):

    """ This function will convert the value in appropriate format after applying
        thousands_sep, grouping etc

        @param value:The value to be converted
        @param monetary:True or False by default False
        @param grouping:True or False by default False
        @param thousands_sep: The symbol to be applied at the thousand's place by default blank

        @return: The converted value"""

    grouping = eval(grouping)
    if not grouping:
        return (value, 0)

    result = ""
    seps = 0
    spaces = ""

    if value[-1] == ' ':
        sp = value.find(' ')
        spaces = value[sp:]
        value = value[:sp]

    while value and grouping:
        # if grouping is -1, we are done
        if grouping[0] == -1:
            break
        # 0: re-use last group ad infinitum
        elif grouping[0] != 0:
            #process last group
            group = grouping[0]
            grouping = grouping[1:]
        if result:
            result = value[-group:] + thousands_sep + result
            seps += 1
        else:
            result = value[-group:]
        value = value[:-group]
        if value and value[-1] not in "0123456789":
            # the leading string is only spaces and signs
            return value + result + spaces, seps
    if not result:
        return value + spaces, seps
    if value:
        result = value + thousands_sep + result
        seps += 1
    return result + spaces, seps         
        

def parse_decimal(value):
    
    locale = cherrypy.session['lang'].get('code') or get_locale()
    if isinstance(value, basestring):
        
        value = ustr(value)
        
        grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
        #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char)
        value = value.replace(' ', '')
        value = value.replace(thousands_sep, '').replace(decimal_point, '.')

    if not isinstance(value, float):
        return float(value)

    return value