~openerp-dev/openobject-server/trunk-gunicorn-signaling-vmt

« back to all changes in this revision

Viewing changes to openerp/osv/fields.py

  • Committer: Vo Minh Thu
  • Date: 2012-03-07 11:10:30 UTC
  • mfrom: (4001.1.83 server)
  • Revision ID: vmt@openerp.com-20120307111030-8hzhzm0zoo34nuj7
[MERGE] merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import base64
36
36
import datetime as DT
37
37
import logging
 
38
import pytz
38
39
import re
39
 
import string
40
 
import sys
41
40
import xmlrpclib
42
41
from psycopg2 import Binary
43
42
 
44
43
import openerp
45
 
import openerp.netsvc as netsvc
46
44
import openerp.tools as tools
47
45
from openerp.tools.translate import _
48
46
from openerp.tools import float_round, float_repr
262
260
 
263
261
class date(_column):
264
262
    _type = 'date'
 
263
 
265
264
    @staticmethod
266
265
    def today(*args):
267
266
        """ Returns the current date in a format fit for being a
273
272
        return DT.date.today().strftime(
274
273
            tools.DEFAULT_SERVER_DATE_FORMAT)
275
274
 
 
275
    @staticmethod
 
276
    def context_today(model, cr, uid, context=None, timestamp=None):
 
277
        """Returns the current date as seen in the client's timezone
 
278
           in a format fit for date fields.
 
279
           This method may be passed as value to initialize _defaults.
 
280
 
 
281
           :param Model model: model (osv) for which the date value is being
 
282
                               computed - technical field, currently ignored,
 
283
                               automatically passed when used in _defaults.
 
284
           :param datetime timestamp: optional datetime value to use instead of
 
285
                                      the current date and time (must be a
 
286
                                      datetime, regular dates can't be converted
 
287
                                      between timezones.)
 
288
           :param dict context: the 'tz' key in the context should give the
 
289
                                name of the User/Client timezone (otherwise
 
290
                                UTC is used)
 
291
           :rtype: str 
 
292
        """
 
293
        today = timestamp or DT.datetime.now()
 
294
        context_today = None
 
295
        if context and context.get('tz'):
 
296
            try:
 
297
                utc = pytz.timezone('UTC')
 
298
                context_tz = pytz.timezone(context['tz'])
 
299
                utc_today = utc.localize(today, is_dst=False) # UTC = no DST
 
300
                context_today = utc_today.astimezone(context_tz)
 
301
            except Exception:
 
302
                _logger.debug("failed to compute context/client-specific today date, "
 
303
                              "using the UTC value for `today`",
 
304
                              exc_info=True)
 
305
        return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
 
306
 
276
307
class datetime(_column):
277
308
    _type = 'datetime'
278
309
    @staticmethod
286
317
        return DT.datetime.now().strftime(
287
318
            tools.DEFAULT_SERVER_DATETIME_FORMAT)
288
319
 
 
320
    @staticmethod
 
321
    def context_timestamp(cr, uid, timestamp, context=None):
 
322
        """Returns the given timestamp converted to the client's timezone.
 
323
           This method is *not* meant for use as a _defaults initializer,
 
324
           because datetime fields are automatically converted upon
 
325
           display on client side. For _defaults you :meth:`fields.datetime.now`
 
326
           should be used instead.
 
327
 
 
328
           :param datetime timestamp: naive datetime value (expressed in UTC)
 
329
                                      to be converted to the client timezone
 
330
           :param dict context: the 'tz' key in the context should give the
 
331
                                name of the User/Client timezone (otherwise
 
332
                                UTC is used)
 
333
           :rtype: datetime
 
334
           :return: timestamp converted to timezone-aware datetime in context
 
335
                    timezone
 
336
        """
 
337
        assert isinstance(timestamp, DT.datetime), 'Datetime instance expected'
 
338
        if context and context.get('tz'):
 
339
            try:
 
340
                utc = pytz.timezone('UTC')
 
341
                context_tz = pytz.timezone(context['tz'])
 
342
                utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST
 
343
                return utc_timestamp.astimezone(context_tz)
 
344
            except Exception:
 
345
                _logger.debug("failed to compute context/client-specific timestamp, "
 
346
                              "using the UTC value",
 
347
                              exc_info=True)
 
348
        return timestamp
 
349
 
289
350
class time(_column):
290
351
    _type = 'time'
291
352
    _deprecated = True
1289
1350
                    value = value or []
1290
1351
                    if value:
1291
1352
                        # filter out deleted records as superuser
1292
 
                        relation_obj = obj.pool.get(self.relation)
 
1353
                        relation_obj = obj.pool.get(obj._columns[field_name].relation)
1293
1354
                        value = relation_obj.exists(cr, openerp.SUPERUSER_ID, value)
1294
1355
                if type(value) in (int,long) and field_type == 'many2one':
1295
 
                    relation_obj = obj.pool.get(self.relation)
 
1356
                    relation_obj = obj.pool.get(obj._columns[field_name].relation)
1296
1357
                    # check for deleted record as superuser
1297
1358
                    if not relation_obj.exists(cr, openerp.SUPERUSER_ID, [value]):
1298
1359
                        value = False