summaryrefslogtreecommitdiff
path: root/addons/google_calendar/controllers
diff options
context:
space:
mode:
authorLucas Lefèvre <lul@odoo.com>2020-01-15 14:59:13 +0100
committerLucas Lefèvre <lul@odoo.com>2020-04-09 10:59:33 +0200
commitae3d37c201bb1e5064096c6fc13e9db18e566611 (patch)
tree9fc2fbec68f299015da25897724b250940ea1848 /addons/google_calendar/controllers
parent85907951c52b40c6dfdaeffa68ce43ccc8bfe0f5 (diff)
[REF] google_calendar: Refactor synchronisation
This commit refactors calendar synchronization between Odoo and Google after the main calendar application refactoring. This refactoring takes advantage of two new features from the Google API: - New way of synchronizing resources efficiently[1] Incremental sync is performed repeatedly and updates Odoo with all the changes that happened ever since the previous sync. Each time, Odoo provides the previous sync token it obtained from Google and stores the new sync token from the response. - Event metadata[2] Ability to set hidden key-value pairs with an event, called extended properties. These extended properties are used to store the related odoo event id and the Odoo owner id (see known limitations) Known limitations ================= - Let A and B be two new users (no tokens available). A creates an event in Odoo and invites B. A is the owner of the event (user_id). Now B authenticates to his Google Calendar account and synchronizes his calendar. We cannot send the event to A's calendar since we don't have any access to his Google Calendar. Hence the event his sent to B's calendar. This leads to data de-synchronisation: The owner is A in Odoo but B in Google. The "real" owner (user A) is stored in the Google event's metadata to be able to reconcile the owner for following synchronizations. - Let A and B be two users of Odoo and Google Calendar. And let the Google Calendar of B be private (e.g. if A creates an event in Google Calendar and invites B, B won't see the event in his calendar). If A creates an event in Odoo and invites B. The event is synced to Google Calendar of A. Now B can see the event in Odoo but he can't see it in his Google Calendar. Task 2126717 PR #42031 PR Enterprise odoo/enterprise#8006 [1] https://developers.google.com/calendar/v3/sync [2] https://developers.google.com/calendar/extended-properties
Diffstat (limited to 'addons/google_calendar/controllers')
-rw-r--r--addons/google_calendar/controllers/main.py34
1 files changed, 11 insertions, 23 deletions
diff --git a/addons/google_calendar/controllers/main.py b/addons/google_calendar/controllers/main.py
index fb40a631a03..465b0fa48b0 100644
--- a/addons/google_calendar/controllers/main.py
+++ b/addons/google_calendar/controllers/main.py
@@ -3,6 +3,7 @@
from odoo import http
from odoo.http import request
+from addons.google_calendar.utils.google_calendar import GoogleCalendarService
class GoogleCalendarController(http.Controller):
@@ -14,16 +15,14 @@ class GoogleCalendarController(http.Controller):
The dictionary may contains an url, to allow Odoo Client to redirect user on this URL for authorization for example
"""
if model == 'calendar.event':
- GoogleService = request.env['google.service']
- GoogleCal = request.env['google.calendar']
+ GoogleCal = GoogleCalendarService(request.env['google.service'])
# Checking that admin have already configured Google API for google synchronization !
- context = kw.get('local_context', {})
- client_id = GoogleService.with_context(context).get_client_id('calendar')
+ client_id = request.env['ir.config_parameter'].sudo().get_param('google_calendar_client_id')
if not client_id or client_id == '':
action_id = ''
- if GoogleCal.can_authorize_google():
+ if GoogleCal._can_authorize_google(request.env.user):
action_id = request.env.ref('base_setup.action_general_configuration').id
return {
"status": "need_config_from_admin",
@@ -32,28 +31,17 @@ class GoogleCalendarController(http.Controller):
}
# Checking that user have already accepted Odoo to access his calendar !
- if GoogleCal.need_authorize():
- url = GoogleCal.with_context(context).authorize_google_uri(from_url=kw.get('fromurl'))
+ if not GoogleCal.is_authorized(request.env.user):
+ url = GoogleCal._google_authentication_url(from_url=kw.get('fromurl'))
return {
"status": "need_auth",
"url": url
}
-
# If App authorized, and user access accepted, We launch the synchronization
- return GoogleCal.with_context(context).synchronize_events()
+ need_refresh = request.env.user.sudo()._sync_google_calendar(GoogleCal)
+ return {
+ "status": "need_refresh" if need_refresh else "no_new_event_from_google",
+ "url": ''
+ }
return {"status": "success"}
-
- @http.route('/google_calendar/remove_references', type='json', auth='user')
- def remove_references(self, model, **kw):
- """ This route/function is called when we want to remove all the references between one calendar Odoo and one Google Calendar """
- status = "NOP"
- if model == 'calendar.event':
- GoogleCal = request.env['google.calendar']
- # Checking that user have already accepted Odoo to access his calendar !
- context = kw.get('local_context', {})
- if GoogleCal.with_context(context).remove_references():
- status = "OK"
- else:
- status = "KO"
- return {"status": status}