← Back to team overview

openerp-dev-web team mailing list archive

lp:~openerp-dev/openobject-server/trunk-temporal-db-improvements-write-rpa into lp:~openerp-dev/openobject-server/trunk-temporal-db

 

Rucha (Open ERP) has proposed merging lp:~openerp-dev/openobject-server/trunk-temporal-db-improvements-write-rpa into lp:~openerp-dev/openobject-server/trunk-temporal-db.

Requested reviews:
  qdp (OpenERP) (qdp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-server/trunk-temporal-db-improvements-write-rpa/+merge/61571

Improvement in write function, and computation of temporal date to

-- 
https://code.launchpad.net/~openerp-dev/openobject-server/trunk-temporal-db-improvements-write-rpa/+merge/61571
Your team OpenERP R&D Team is subscribed to branch lp:~openerp-dev/openobject-server/trunk-temporal-db.
=== modified file 'openerp/osv/orm.py'
--- openerp/osv/orm.py	2011-04-29 05:38:31 +0000
+++ openerp/osv/orm.py	2011-05-19 13:41:24 +0000
@@ -4285,20 +4285,40 @@
     def set_date(self, obj, cr, uid, ids, name, args, context=None):
         res = {}
         for record in obj.browse(cr, uid, ids, context=context):
-            if record.temporal_parent_id:
-                res[record.id] = context.get('temporal_date_from', record.temporal_parent_id.temporal_date_from)
-            else:
-                res[record.id] = False
-        return res
+            context.update({'all_temporal': True})
+            next_id = self.get_next_id_in_timeline(obj, cr, uid, [record.id], context=context)
+            res[record.id] = next_id and obj.read(cr, uid, next_id, ['temporal_date_from'])[0]['temporal_date_from'] or False
+        return res
+
+    def get_ids_of_same_group(self, cr, uid, ids, context=None):
+        res = {}
+        for record in self.browse(cr, uid, ids, context=context):
+            search_ids = [record.id]
+            if record.temporal_parent_id.id:
+                search_ids.append(record.temporal_parent_id.id)
+            res[record.id] = self.search(cr, uid,  ['|',('temporal_parent_id', 'in', search_ids), ('id', 'in', search_ids)], context=context)
+        return res
+
+    def get_next_id_in_timeline(self, obj, cr, uid, ids, context=None):
+        res = obj.get_ids_of_same_group(cr, uid, ids, context)
+        for record in obj.browse(cr, uid, ids, context=context):
+            search_id = obj.search(cr, uid, [('id', 'in', res[record.id]), ('temporal_date_from', '>', record.temporal_date_from)], order='temporal_date_from asc', limit=1, context={'all_temporal': True})
+        return search_id
+
+    def get_previous_id_in_timeline(self, obj, cr, uid, ids, context=None):
+        res = obj.get_ids_of_same_group(cr, uid, ids, context)
+        for record in obj.browse(cr, uid, ids, context=context):
+            search_id = obj.search(cr, uid, [('id', 'in', res[record.id]), ('temporal_date_from', '<', record.temporal_date_from)], order='temporal_date_from desc', limit=1, context={'all_temporal': True})
+        return search_id + ids
 
     def __init__(self, cr):
         self._columns.update({
-        'temporal_date_from': fields.datetime('Temporal From Date'),
-        'temporal_date_to': fields.function(self.set_date, method=True, string='Temporal To Date', type='datetime',
+        'temporal_date_from': fields.datetime('Temporal From Date', select=True),
+        'temporal_date_to': fields.function(self.set_date, method=True, select=True, string='Temporal To Date', type='datetime',
             store = {
-                self._name: (lambda self, cr, uid, ids, c={}: ids, ['temporal_date_from'], 10),
+                        self._name: (self.get_previous_id_in_timeline , ['temporal_date_from'], 10),
             }),
-        'temporal_parent_id': fields.many2one(self._name, 'Temporal Parent ID', ondelete='cascade')
+        'temporal_parent_id': fields.many2one(self._name, 'Temporal Parent ID', select=True, ondelete='cascade')
     })
         return super(orm_temporal, self).__init__(cr)
 
@@ -4312,12 +4332,19 @@
             context = {}
         if isinstance(ids, (int, long)):
             ids = [ids]
-        timenow = time.strftime('%Y-%m-%d %H:%M:%S')
-        context.update({'temporal_date_from': timenow}) # get current time when make new copy of the current record
-        vals.update({'temporal_date_from': timenow})
-        for record in self.browse(cr, uid, ids, context=context):
-            defaults = {'temporal_date_from': record.temporal_date_from,'temporal_parent_id': record.id}
-            orm.copy(self, cr, uid, record.id, defaults, context=context)
+        if vals.get('temporal_date_from'):
+            return super(orm_temporal, self).write(cr, uid, ids, vals, context=context) # if temporal_date_from pass in vals just call super without any copy
+
+        if context.get('temporal_mode', True) and not vals.get('temporal_date_from'):
+            for record in self.browse(cr, uid, ids, context=context):
+    #            avoid creating history  records when writing on a reocrd that is already an history record
+                if record.temporal_parent_id:
+                   continue
+
+                vals.update({'temporal_date_from': time.strftime('%Y-%m-%d %H:%M:%S')}) # get current time when make new copy of the current record
+                defaults = {'temporal_date_from': record.temporal_date_from, 'temporal_parent_id': record.id}
+                orm.copy(self, cr, uid, record.id, defaults, context=context)
+
         return super(orm_temporal, self).write(cr, uid, ids, vals, context=context)
 
     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
@@ -4338,7 +4365,7 @@
         #add the time constraint
         timenow = time.strftime('%Y-%m-%d %H:%M:%S')
         temporal_date = context.get("temporal_date", timenow)
-        new_args += [('temporal_date_from', '<=', temporal_date), '|', ('temporal_date_to', '=', False), ('temporal_date_to', '>', temporal_date)]
+        new_args += ['|',('temporal_date_from','=',False),('temporal_date_from', '<=', temporal_date), '|', ('temporal_date_to', '=', False), ('temporal_date_to', '>', temporal_date)]
         return super(orm_temporal, self).search(cr, user, new_args, offset, limit, order, context, count)
 
     def read(self, cr, uid, ids_orig, fields=None, context=None, load='_classic_read'):


Follow ups