36
36
import datetime as DT
42
41
from psycopg2 import Binary
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
273
272
return DT.date.today().strftime(
274
273
tools.DEFAULT_SERVER_DATE_FORMAT)
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.
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
288
:param dict context: the 'tz' key in the context should give the
289
name of the User/Client timezone (otherwise
293
today = timestamp or DT.datetime.now()
295
if context and context.get('tz'):
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)
302
_logger.debug("failed to compute context/client-specific today date, "
303
"using the UTC value for `today`",
305
return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
276
307
class datetime(_column):
277
308
_type = 'datetime'
286
317
return DT.datetime.now().strftime(
287
318
tools.DEFAULT_SERVER_DATETIME_FORMAT)
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.
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
334
:return: timestamp converted to timezone-aware datetime in context
337
assert isinstance(timestamp, DT.datetime), 'Datetime instance expected'
338
if context and context.get('tz'):
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)
345
_logger.debug("failed to compute context/client-specific timestamp, "
346
"using the UTC value",
289
350
class time(_column):
291
352
_deprecated = True
1289
1350
value = value or []
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]):