From 2e6d551372b260b301123ee6a387d07da1037d01 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 17:26:02 +0200 Subject: [PATCH 1/6] [IMP] do not delete res.log items after reading them Just mark them as read bzr revid: xmo@openerp.com-20101012152602-0adn5ed1ztgch2xa --- bin/addons/base/res/res_log.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/bin/addons/base/res/res_log.py b/bin/addons/base/res/res_log.py index 8165d05acac..39a9bac9983 100644 --- a/bin/addons/base/res/res_log.py +++ b/bin/addons/base/res/res_log.py @@ -33,20 +33,26 @@ 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") } _defaults = { 'user_id': lambda self,cr,uid,ctx: uid, 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'), - 'context': lambda *args: "{}" + 'context': lambda *args: "{}", + 'read': False } _order='create_date desc' # TODO: do not return secondary log if same object than in the model (but unlink it) - def get(self, cr, uid, context={}): - ids = self.search(cr, uid, [('user_id','=',uid)], context=context) - result = self.read(cr, uid, ids, ['name','res_model','res_id'], context=context) - self.unlink(cr, uid, ids, context=context) - return result + def get(self, cr, uid, context=None): + unread_log_ids = self.search(cr, uid, [('user_id','=',uid), + ('read', '=', False)], + context=context) + 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) + return unread_logs def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False): res = [] From d515172dd982402c35b7296d86ca9f4c9e5f31b4 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 17:26:30 +0200 Subject: [PATCH 2/6] [IMP] res.log: no need to use lambda for a constant in trunk bzr revid: xmo@openerp.com-20101012152630-3sf9k6p3yc6e3s78 --- bin/addons/base/res/res_log.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/addons/base/res/res_log.py b/bin/addons/base/res/res_log.py index 39a9bac9983..1c0102c1c79 100644 --- a/bin/addons/base/res/res_log.py +++ b/bin/addons/base/res/res_log.py @@ -38,7 +38,7 @@ class res_log(osv.osv_memory): _defaults = { 'user_id': lambda self,cr,uid,ctx: uid, 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'), - 'context': lambda *args: "{}", + 'context': "{}", 'read': False } _order='create_date desc' @@ -66,4 +66,3 @@ class res_log(osv.osv_memory): return tools.flatten(res) res_log() - From 040f2ce15b8d6c512116ae75cb7d8bad5e499b81 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 18:01:30 +0200 Subject: [PATCH 3/6] [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): From 727c9f171abec26a4824cfa742205cb23943fc88 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 18:23:48 +0200 Subject: [PATCH 4/6] [IMP] labelling of read field on res.log bzr revid: xmo@openerp.com-20101012162348-94jbd5ufwgh1cm5c --- bin/addons/base/res/res_log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/addons/base/res/res_log.py b/bin/addons/base/res/res_log.py index 8d5f501866a..e475f2f6278 100644 --- a/bin/addons/base/res/res_log.py +++ b/bin/addons/base/res/res_log.py @@ -33,7 +33,7 @@ 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': fields.boolean('Read status', help="If this log item has been read, get() should not send it to the client") + 'read': fields.boolean('Read', 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, From 370f2a477d6439bc3ef3717cb3389c2352e12df4 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 18:32:41 +0200 Subject: [PATCH 5/6] [IMP] add read status in the log views (and search) bzr revid: xmo@openerp.com-20101012163241-ly59hdn375nfg0pt --- bin/addons/base/res/res_log_view.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/addons/base/res/res_log_view.xml b/bin/addons/base/res/res_log_view.xml index 6dac6c044c6..aa85272cfac 100644 --- a/bin/addons/base/res/res_log_view.xml +++ b/bin/addons/base/res/res_log_view.xml @@ -13,6 +13,7 @@ + @@ -22,9 +23,10 @@ tree - + + @@ -34,8 +36,11 @@ form
+ + + From 9efc48eadfb896a70197f8963caa6034d9b4856d Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 12 Oct 2010 19:09:04 +0200 Subject: [PATCH 6/6] [IMP] store res_log in database instead of memory, add index bzr revid: xmo@openerp.com-20101012170904-o3woe7qpdie2pflv --- bin/addons/base/res/res_log.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/addons/base/res/res_log.py b/bin/addons/base/res/res_log.py index e475f2f6278..39fa8ec8969 100644 --- a/bin/addons/base/res/res_log.py +++ b/bin/addons/base/res/res_log.py @@ -23,7 +23,7 @@ from osv import fields, osv import tools import time -class res_log(osv.osv_memory): +class res_log(osv.osv): _name = 'res.log' _columns = { 'name': fields.char('Message', size=128, help='The logging message.', required=True), @@ -37,12 +37,20 @@ class res_log(osv.osv_memory): } _defaults = { 'user_id': lambda self,cr,uid,ctx: uid, - 'create_date': fields.datetime.now, 'context': "{}", 'read': False } _order='create_date desc' + _index_name = 'res_log_uid_read' + def _auto_init(self, cr, context={}): + super(res_log, self)._auto_init(cr, context) + cr.execute('SELECT 1 FROM pg_indexes WHERE indexname=%s', + (self._index_name,)) + if not cr.fetchone(): + cr.execute('CREATE INDEX %s ON res_log (user_id, read)' % + self._index_name) + # TODO: do not return secondary log if same object than in the model (but unlink it) def get(self, cr, uid, context=None): unread_log_ids = self.search(cr, uid, [('user_id','=',uid),