[FIX] google_{account|calendar}: fix synchro
authorcod-odoo <cod@openerp.com>
Tue, 5 Aug 2014 08:59:02 +0000 (14:29 +0530)
committerRichard Mathot <rim@openerp.com>
Thu, 14 Aug 2014 13:48:05 +0000 (15:48 +0200)
- no more tracebacks when trying to sync more than once with Google Calendar
- no more context hacks, explicit return of `ask_time`.

(Closes #1588)

addons/google_account/google_account.py
addons/google_calendar/google_calendar.py

index 51c7e2c..3033081 100644 (file)
@@ -92,7 +92,7 @@ class google_service(osv.osv_memory):
             uri = self.get_uri_oauth(a='token')
             data = werkzeug.url_encode(params)
 
-            st, res = self._do_request(cr, uid, uri, params=data, headers=headers, type='POST', preuri='', context=context)
+            st, res, ask_time = self._do_request(cr, uid, uri, params=data, headers=headers, type='POST', preuri='', context=context)
         except urllib2.HTTPError:
             error_msg = "Something went wrong during your token generation. Maybe your Authorization Code is invalid"
             raise self.pool.get('res.config.settings').get_config_warning(cr, _(error_msg), context=context)
@@ -116,7 +116,7 @@ class google_service(osv.osv_memory):
             uri = self.get_uri_oauth(a='token')
 
             data = werkzeug.url_encode(params)
-            st, res = self._do_request(cr, uid, uri, params=data, headers=headers, type='POST', preuri='', context=context)
+            st, res, ask_time = self._do_request(cr, uid, uri, params=data, headers=headers, type='POST', preuri='', context=context)
         except urllib2.HTTPError, e:
             if e.code == 400:  # invalid grant
                 registry = openerp.modules.registry.RegistryManager.get(request.session.db)
@@ -129,7 +129,8 @@ class google_service(osv.osv_memory):
         return res
 
     def _do_request(self, cr, uid, uri, params={}, headers={}, type='POST', preuri="https://www.googleapis.com", context=None):
-        context = dict(context or {})
+        if context is None:
+            context = {}
 
         """ Return a tuple ('HTTP_CODE', 'HTTP_RESPONSE') """
         _logger.debug("Uri: %s - Type : %s - Headers: %s - Params : %s !" % (uri, type, headers, werkzeug.url_encode(params) if type == 'GET' else params))
@@ -155,19 +156,17 @@ class google_service(osv.osv_memory):
                 content = request.read()
                 response = simplejson.loads(content)
 
-            if context.get('ask_time'):
-                try:
-                    date = datetime.strptime(request.headers.get('date'), "%a, %d %b %Y %H:%M:%S %Z")
-                except:
-                    date = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
-                context['ask_time'] = date
+            try:
+                ask_time = datetime.strptime(request.headers.get('date'), "%a, %d %b %Y %H:%M:%S %Z")
+            except:
+                ask_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
         except urllib2.HTTPError, e:
             if e.code in (400, 401, 410):
                 raise e
 
             _logger.exception("Bad google request : %s !" % e.read())
             raise self.pool.get('res.config.settings').get_config_warning(cr, _("Something went wrong with your request to google"), context=context)
-        return (status, response)
+        return (status, response, ask_time)
 
     def get_base_url(self, cr, uid, context=None):
         return self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://www.openerp.com?NoBaseUrl', context=context)
index c787ddb..7df4fa3 100644 (file)
@@ -277,7 +277,7 @@ class google_calendar(osv.AbstractModel):
         url = "/calendar/v3/calendars/primary"
 
         try:
-            st, content = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
+            st, content, ask_time = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
         except Exception, e:
 
             if (e.code == 401):  # Token invalid / Acces unauthorized
@@ -290,7 +290,7 @@ class google_calendar(osv.AbstractModel):
                 raise self.pool.get('res.config.settings').get_config_warning(cr, _(error_msg), context=context)
             raise
 
-        return status_response(st) and content['id'] or False
+        return (status_response(st), content['id'] or False, ask_time)
 
     def get_event_synchro_dict(self, cr, uid, lastSync=False, token=False, nextPageToken=False, context=None):
         if not token:
@@ -315,7 +315,7 @@ class google_calendar(osv.AbstractModel):
         if nextPageToken:
             params['pageToken'] = nextPageToken
 
-        status, content = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
+        status, content, ask_time = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
 
         google_events_dict = {}
         for google_event in content['items']:
@@ -341,7 +341,7 @@ class google_calendar(osv.AbstractModel):
 
         url = "/calendar/v3/calendars/%s/events/%s" % ('primary', google_id)
         try:
-            status, content = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
+            status, content, ask_time = self.pool['google.service']._do_request(cr, uid, url, params, headers, type='GET', context=context)
         except:
             _logger.info("Calendar Synchro - In except of get_one_event_synchro")
             pass
@@ -357,7 +357,7 @@ class google_calendar(osv.AbstractModel):
         data['sequence'] = google_event.get('sequence', 0)
         data_json = simplejson.dumps(data)
 
-        status, content = self.pool['google.service']._do_request(cr, uid, url, data_json, headers, type='PATCH', context=context)
+        status, content, ask_time = self.pool['google.service']._do_request(cr, uid, url, data_json, headers, type='PATCH', context=context)
 
         update_date = datetime.strptime(content['updated'], "%Y-%m-%dT%H:%M:%S.%fz")
         calendar_event.write(cr, uid, [oe_event.id], {'oe_update_date': update_date})
@@ -372,7 +372,7 @@ class google_calendar(osv.AbstractModel):
         headers = {}
         data['access_token'] = self.get_token(cr, uid, context)
 
-        status, response = self.pool['google.service']._do_request(cr, uid, url, data, headers, type='GET', context=context)
+        status, response, ask_time = self.pool['google.service']._do_request(cr, uid, url, data, headers, type='GET', context=context)
         #TO_CHECK : , if http fail, no event, do DELETE ?
         return response
 
@@ -514,8 +514,7 @@ class google_calendar(osv.AbstractModel):
 
         current_user = self.pool['res.users'].browse(cr, uid, uid, context=context)
 
-        context_with_time = dict(context.copy(), ask_time=True)
-        current_google = self.get_calendar_primary_id(cr, uid, context=context_with_time)
+        st, current_google, ask_time = self.get_calendar_primary_id(cr, uid, context=context)
 
         if current_user.google_calendar_cal_id:
             if current_google != current_user.google_calendar_cal_id:
@@ -545,7 +544,7 @@ class google_calendar(osv.AbstractModel):
 
         res = self.update_events(cr, uid, lastSync, context)
 
-        current_user.write({'google_calendar_last_sync_date': context_with_time.get('ask_time')}, context=context)
+        current_user.write({'google_calendar_last_sync_date': ask_time}, context=context)
         return {
             "status": res and "need_refresh" or "no_new_event_form_google",
             "url": ''
@@ -571,7 +570,7 @@ class google_calendar(osv.AbstractModel):
                                     ], context=context_norecurrent)
         for att in att_obj.browse(cr, uid, my_att_ids, context=context):
             if not att.event_id.recurrent_id or att.event_id.recurrent_id == 0:
-                st, response = self.create_an_event(cr, uid, att.event_id, context=context)
+                st, response, ask_time = self.create_an_event(cr, uid, att.event_id, context=context)
                 if status_response(st):
                     update_date = datetime.strptime(response['updated'], "%Y-%m-%dT%H:%M:%S.%fz")
                     ev_obj.write(cr, uid, att.event_id.id, {'oe_update_date': update_date})
@@ -617,7 +616,7 @@ class google_calendar(osv.AbstractModel):
                 if new_google_internal_event_id:
                     #TODO WARNING, NEED TO CHECK THAT EVENT and ALL instance NOT DELETE IN GMAIL BEFORE !
                     try:
-                        st, response = self.update_recurrent_event_exclu(cr, uid, new_google_internal_event_id, source_attendee_record.google_internal_event_id, att.event_id, context=context)
+                        st, response, ask_time = self.update_recurrent_event_exclu(cr, uid, new_google_internal_event_id, source_attendee_record.google_internal_event_id, att.event_id, context=context)
                         if status_response(st):
                             att_obj.write(cr, uid, [att.id], {'google_internal_event_id': new_google_internal_event_id}, context=context)
                             new_ids.append(new_google_internal_event_id)
@@ -837,7 +836,7 @@ class google_calendar(osv.AbstractModel):
 
         url = "/calendar/v3/calendars/%s/events/%s" % ('primary', instance_id)
 
-        st, content = gs_pool._do_request(cr, uid, url, params, headers, type='GET', context=context)
+        st, content, ask_time = gs_pool._do_request(cr, uid, url, params, headers, type='GET', context=context)
         return content.get('sequence', 0)
 #################################
 ##  MANAGE CONNEXION TO GMAIL  ##