[IMP] labelling of read field on res.log
[odoo/odoo.git] / bin / addons / base / res / res_log.py
index b83e380..e475f2f 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
@@ -20,6 +20,8 @@
 ##############################################################################
 
 from osv import fields, osv
+import tools
+import time
 
 class res_log(osv.osv_memory):
     _name = 'res.log'
@@ -27,18 +29,39 @@ class res_log(osv.osv_memory):
         'name': fields.char('Message', size=128, help='The logging message.', required=True),
         'user_id': fields.many2one('res.users','User', required=True),
         'res_model': fields.char('Object', size=128),
+        'context': fields.char('Context', size=250),
         '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')
+        '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', 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
+        'user_id': lambda self,cr,uid,ctx: uid,
+        'create_date': fields.datetime.now,
+        'context': "{}",
+        'read': False
     }
-    _order='id desc'
+    _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
-res_log()
+    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, 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):
+        res = []
+        log_ids = super(res_log, self).search(cr, uid, args, offset, limit, order, context, count)
+        logs = {}
+        for log in self.browse(cr, uid, log_ids, context=context):
+            res_dict = logs.get(log.res_model, {})
+            res_dict.update({log.res_id: log.id})
+            logs.update({log.res_model: res_dict})
+        res = map(lambda x: x.values(), logs.values())
+        return tools.flatten(res)
+res_log()