[IMP] calendar: simplify/optimize `rrule` computed field
authorOlivier Dony <odo@openerp.com>
Tue, 5 Aug 2014 15:22:56 +0000 (17:22 +0200)
committerOlivier Dony <odo@openerp.com>
Thu, 7 Aug 2014 16:03:08 +0000 (18:03 +0200)
Avoid reading one record at a time, leading to multiple
calls to read() which is special-cased and even slower
than a normal one.

addons/calendar/calendar.py

index b7f249a..6fa9034 100644 (file)
@@ -776,21 +776,17 @@ class calendar_event(osv.Model):
         if not isinstance(ids, list):
             ids = [ids]
 
-        for id in ids:
-            #read these fields as SUPERUSER because if the record is private a normal search could return False and raise an error
-            data = self.browse(cr, SUPERUSER_ID, id, context=context)
-
-            if data.interval and data.interval < 0:
-                raise osv.except_osv(_('Warning!'), _('Interval cannot be negative.'))
-            if data.count and data.count <= 0:
-                raise osv.except_osv(_('Warning!'), _('Count cannot be negative or 0.'))
-
-            data = self.read(cr, uid, id, ['id', 'byday', 'recurrency', 'final_date', 'rrule_type', 'month_by', 'interval', 'count', 'end_type', 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'su', 'day', 'week_list'], context=context)
-            event = data['id']
-            if data['recurrency']:
-                result[event] = self.compute_rule_string(data)
+        #read these fields as SUPERUSER because if the record is private a normal search could raise an error
+        events = self.read(cr, SUPERUSER_ID, ids,
+                           ['id', 'byday', 'recurrency', 'final_date', 'rrule_type', 'month_by',
+                            'interval', 'count', 'end_type', 'mo', 'tu', 'we', 'th', 'fr', 'sa',
+                            'su', 'day', 'week_list'], context=context)
+        for event in events:
+            if event['recurrency']:
+                result[event['id']] = self.compute_rule_string(event)
             else:
-                result[event] = ""
+                result[event['id']] = ''
+
         return result
 
     # retro compatibility function
@@ -1180,6 +1176,11 @@ class calendar_event(osv.Model):
         @param data: dictionary of freq and interval value
         @return: string containing recurring rule (empty if no rule)
         """
+        if data['interval'] and data['interval'] < 0:
+            raise osv.except_osv(_('warning!'), _('interval cannot be negative.'))
+        if data['count'] and data['count'] <= 0:
+            raise osv.except_osv(_('warning!'), _('count cannot be negative or 0.'))
+
         def get_week_string(freq, data):
             weekdays = ['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su']
             if freq == 'weekly':