[IMP] Google calendar - add function to allow the cron to run google calendar synchro...
authorJeremy Kersten <jke@odoo.com>
Fri, 3 Oct 2014 15:52:31 +0000 (17:52 +0200)
committerJeremy Kersten <jke@odoo.com>
Fri, 3 Oct 2014 15:52:31 +0000 (17:52 +0200)
addons/google_calendar/__openerp__.py
addons/google_calendar/controllers/main.py
addons/google_calendar/google_calendar.py

index d00202a..56fab36 100644 (file)
@@ -37,6 +37,7 @@ The module adds the possibility to synchronize Google Calendar with OpenERP
         'security/ir.model.access.csv',
         'views/google_calendar.xml',
         'views/res_users.xml',
+        'google_calendar.xml',
     ],
     'demo': [],
     'installable': True,
index 1f30713..63034e5 100644 (file)
@@ -40,7 +40,7 @@ class google_calendar_controller(http.Controller):
                 }
 
             # If App authorized, and user access accepted, We launch the synchronization
-            return gc_obj.synchronize_events(request.cr, request.uid, [], context=kw.get('local_context'))
+            return gc_obj.synchronize_events(request.cr, request.uid, [request.uid], context=kw.get('local_context'))
 
         return {"status": "success"}
 
index b4483f9..cffc274 100644 (file)
@@ -7,7 +7,8 @@ import urllib2
 import openerp
 from openerp import tools
 from openerp import SUPERUSER_ID
-from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
+from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, exception_to_unicode
+
 from openerp.tools.translate import _
 from openerp.http import request
 from datetime import datetime, timedelta
@@ -496,6 +497,22 @@ class google_calendar(osv.AbstractModel):
         current_user.write(reset_data, context=context)
         return True
 
+    def synchronize_events_cron(self, cr, uid, context=None):
+        ids = self.pool['res.users'].search(cr, uid, [('google_calendar_last_sync_date', '!=', False)], context=context)
+        _logger.info("Calendar Synchro - Started by cron")
+
+        for user_to_sync in ids:
+            _logger.info("Calendar Synchro - Starting synchronization for a new user [%s] " % user_to_sync)
+            try:
+                resp = self.synchronize_events(cr, uid, [user_to_sync], lastSync=True, context=None)
+                if resp.get("status") == "need_reset":
+                    _logger.info("[%s] Calendar Synchro - Failed - NEED RESET  !" % user_to_sync)
+                else:
+                    _logger.info("[%s] Calendar Synchro - Done with status : %s  !" % (user_to_sync, resp.get("status")))
+            except Exception, e:
+                _logger.info("[%s] Calendar Synchro - Exception : %s !" % exception_to_unicode(e))
+        _logger.info("Calendar Synchro - Ended by cron")
+
     def synchronize_events(self, cr, uid, ids, lastSync=True, context=None):
         if context is None:
             context = {}
@@ -512,7 +529,8 @@ class google_calendar(osv.AbstractModel):
         #     status, response = gs_pool._do_request(cr, uid, url, params, type='GET', context=context)
         #     return int(status) != 410
 
-        current_user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
+        user_to_sync = ids and ids[0] or uid
+        current_user = self.pool['res.users'].browse(cr, SUPERUSER_ID, user_to_sync, context=context)
 
         st, current_google, ask_time = self.get_calendar_primary_id(cr, uid, context=context)
 
@@ -529,14 +547,14 @@ class google_calendar(osv.AbstractModel):
 
             if lastSync and self.get_last_sync_date(cr, uid, context=context) and not self.get_disable_since_synchro(cr, uid, context=context):
                 lastSync = self.get_last_sync_date(cr, uid, context)
-                _logger.info("Calendar Synchro - MODE SINCE_MODIFIED : %s !" % lastSync.strftime(DEFAULT_SERVER_DATETIME_FORMAT))
+                _logger.info("[%s] Calendar Synchro - MODE SINCE_MODIFIED : %s !" % (user_to_sync, lastSync.strftime(DEFAULT_SERVER_DATETIME_FORMAT)))
             else:
                 lastSync = False
-                _logger.info("Calendar Synchro - MODE FULL SYNCHRO FORCED")
+                _logger.info("[%s] Calendar Synchro - MODE FULL SYNCHRO FORCED" % user_to_sync)
         else:
             current_user.write({'google_calendar_cal_id': current_google}, context=context)
             lastSync = False
-            _logger.info("Calendar Synchro - MODE FULL SYNCHRO - NEW CAL ID")
+            _logger.info("[%s] Calendar Synchro - MODE FULL SYNCHRO - NEW CAL ID" % user_to_sync)
 
         new_ids = []
         new_ids += self.create_new_events(cr, uid, context=context)