From 040f2ce15b8d6c512116ae75cb7d8bad5e499b81 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 18:01:30 +0200 Subject: [PATCH] [FIX] use correct default callable for a datetime to get the current datetime as of the object's creation Add staticmethods to fields.time, fields.date and fields.datetime so they can generate default values which they can actually consume bzr revid: xmo@openerp.com-20101012160130-jwgl7cok853fiyap --- bin/addons/base/res/res_log.py | 7 +++---- bin/osv/fields.py | 29 +++++++++++++++++++++++++++++ bin/tools/misc.py | 5 ++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/bin/addons/base/res/res_log.py b/bin/addons/base/res/res_log.py index 1c0102c1c79..8d5f501866a 100644 --- a/bin/addons/base/res/res_log.py +++ b/bin/addons/base/res/res_log.py @@ -33,11 +33,11 @@ class res_log(osv.osv_memory): 'res_id': fields.integer('Object ID'), 'secondary': fields.boolean('Secondary Log', help='Do not display this log if it belongs to the same object the user is working on'), 'create_date': fields.datetime('Created Date', readonly=True), - 'read': field.boolean('Read status', help="If this log item has been read, get() should not send it to the client") + 'read': fields.boolean('Read status', help="If this log item has been read, get() should not send it to the client") } _defaults = { 'user_id': lambda self,cr,uid,ctx: uid, - 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'), + 'create_date': fields.datetime.now, 'context': "{}", 'read': False } @@ -51,7 +51,7 @@ class res_log(osv.osv_memory): unread_logs = self.read(cr, uid, unread_log_ids, ['name','res_model','res_id'], context=context) - self.write(cr, uid, ids, {'read': True}, context=context) + self.write(cr, uid, unread_log_ids, {'read': True}, context=context) return unread_logs def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False): @@ -64,5 +64,4 @@ class res_log(osv.osv_memory): logs.update({log.res_model: res_dict}) res = map(lambda x: x.values(), logs.values()) return tools.flatten(res) - res_log() diff --git a/bin/osv/fields.py b/bin/osv/fields.py index 64432ba15d7..f4442761bb8 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -32,6 +32,7 @@ # size # from collections import defaultdict +import datetime as DT import string import netsvc import sys @@ -189,14 +190,42 @@ class float(_column): class date(_column): _type = 'date' + @staticmethod + def today(*args): + """ Returns the current date in a format fit for being a + default value to a ``date`` field. + This method should be provided as is to the _defaults dict, it + should not be called. + """ + return DT.date.today().strftime( + tools.DEFAULT_SERVER_DATE_FORMAT) class datetime(_column): _type = 'datetime' + @staticmethod + def now(*args): + """ Returns the current datetime in a format fit for being a + default value to a ``datetime`` field. + This method should be provided as is to the _defaults dict, it + should not be called. + """ + return DT.datetime.now().strftime( + tools.DEFAULT_SERVER_DATETIME_FORMAT) class time(_column): _type = 'time' + @staticmethod + def now( *args): + """ Returns the current time in a format fit for being a + default value to a ``time`` field. + + This method should be proivided as is to the _defaults dict, + it should not be called. + """ + return DT.datetime.now().strftime( + tools.DEFAULT_SERVER_TIME_FORMAT) class binary(_column): _type = 'binary' diff --git a/bin/tools/misc.py b/bin/tools/misc.py index b3b3d7d9baa..81707156244 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -1323,7 +1323,10 @@ def get_server_timezone(): DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d" -DEFAULT_SERVER_DATETIME_FORMAT = DEFAULT_SERVER_DATE_FORMAT + " %H:%M:%S" +DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S" +DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % ( + DEFAULT_SERVER_DATE_FORMAT, + DEFAULT_SERVER_TIME_FORMAT) def server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name, tz_offset=True, ignore_unparsable_time=True):