[MERGE] forward port of branch saas-3 up to 3d80dc2
[odoo/odoo.git] / addons / calendar / calendar.py
index 3e51407..f25e530 100644 (file)
@@ -415,11 +415,12 @@ class calendar_alarm_manager(osv.AbstractModel):
         return res
 
     def get_next_mail(self, cr, uid, context=None):
-        cron = self.pool.get('ir.cron').search(cr, uid, [('model', 'ilike', self._name)], context=context)
-        if cron and len(cron) == 1:
-            cron = self.pool.get('ir.cron').browse(cr, uid, cron[0], context=context)
-        else:
-            _logger.exception("Cron for " + self._name + " can not be identified !")
+        try:
+            cron = self.pool['ir.model.data'].get_object(
+                cr, uid, 'calendar', 'ir_cron_scheduler_alarm', context=context)
+        except ValueError:
+            _logger.error("Cron for " + self._name + " can not be identified !")
+            return False
 
         if cron.interval_type == "weeks":
             cron_interval = cron.interval_number * 7 * 24 * 60 * 60
@@ -431,9 +432,12 @@ class calendar_alarm_manager(osv.AbstractModel):
             cron_interval = cron.interval_number * 60
         elif cron.interval_type == "seconds":
             cron_interval = cron.interval_number
+        else:
+            cron_interval = False
 
         if not cron_interval:
-            _logger.exception("Cron delay can not be computed !")
+            _logger.error("Cron delay can not be computed !")
+            return False
 
         all_events = self.get_next_potential_limit_alarm(cr, uid, cron_interval, notif=False, context=context)
 
@@ -558,6 +562,35 @@ class calendar_alarm(osv.Model):
         'interval': 'hours',
     }
 
+    def _update_cron(self, cr, uid, context=None):
+        try:
+            cron = self.pool['ir.model.data'].get_object(
+                cr, uid, 'calendar', 'ir_cron_scheduler_alarm', context=context)
+        except ValueError:
+            return False
+        return cron.toggle(model=self._name, domain=[('type', '=', 'email')])
+
+    def create(self, cr, uid, values, context=None):
+        res = super(calendar_alarm, self).create(cr, uid, values, context=context)
+
+        self._update_cron(cr, uid, context=context)
+
+        return res
+
+    def write(self, cr, uid, ids, values, context=None):
+        res = super(calendar_alarm, self).write(cr, uid, ids, values, context=context)
+
+        self._update_cron(cr, uid, context=context)
+
+        return res
+
+    def unlink(self, cr, uid, ids, context=None):
+        res = super(calendar_alarm, self).unlink(cr, uid, ids, context=context)
+
+        self._update_cron(cr, uid, context=context)
+
+        return res
+
 
 class ir_values(osv.Model):
     _inherit = 'ir.values'