From 2d0343bcc32c2ebf28d7246f5fbbd85e06e8e705 Mon Sep 17 00:00:00 2001 From: Yas-Elak Date: Fri, 12 Mar 2021 10:06:54 +0000 Subject: [PATCH] [FIX] google_calendar : check user_id before calculating ownership MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: If we get a 'false' for the real_owner_id from the extendedProperties, we will get an error ValueError: invalid literal for int() with base 10: 'false' Fix: We make sure to set a user for the real_owner_id var if it return a 'false' before closes odoo/odoo#67840 X-original-commit: 735614a127499843bc7bd701794870234e743a88 Signed-off-by: Lucas Lefèvre (lul) --- addons/google_calendar/models/calendar.py | 2 +- .../tests/test_sync_google2odoo.py | 51 +++++++++++++++++++ .../tests/test_sync_odoo2google.py | 12 +++++ addons/google_calendar/utils/google_event.py | 10 +++- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/addons/google_calendar/models/calendar.py b/addons/google_calendar/models/calendar.py index 11364eee474..17ebd7dd838 100644 --- a/addons/google_calendar/models/calendar.py +++ b/addons/google_calendar/models/calendar.py @@ -202,7 +202,7 @@ class Meeting(models.Model): } if self.privacy: values['visibility'] = self.privacy - if self.user_id != self.env.user: + if self.user_id and self.user_id != self.env.user: values['extendedProperties']['shared']['%s_owner_id' % self.env.cr.dbname] = self.user_id.id if not self.active: diff --git a/addons/google_calendar/tests/test_sync_google2odoo.py b/addons/google_calendar/tests/test_sync_google2odoo.py index 4e2d1a7d809..01e53a625fd 100644 --- a/addons/google_calendar/tests/test_sync_google2odoo.py +++ b/addons/google_calendar/tests/test_sync_google2odoo.py @@ -54,6 +54,57 @@ class TestSyncGoogle2Odoo(TransactionCase): self.assertEqual(event.partner_ids, event.attendee_ids.partner_id) self.assertEqual('needsAction', admin_attendee.state) + def test_invalid_owner_property(self): + values = { + 'id': 'oj44nep1ldf8a3ll02uip0c9aa', + 'description': 'Small mini desc', + 'organizer': {'email': 'odoocalendarref@gmail.com', 'self': True}, + 'summary': 'Pricing new update', + 'visibility': 'public', + 'attendees': [], + 'reminders': {'useDefault': True}, + 'start': { + 'dateTime': '2020-01-13T16:55:00+01:00', + 'timeZone': 'Europe/Brussels' + }, + 'extendedProperties': { + 'shared': {'%s_owner_id' % self.env.cr.dbname: "invalid owner id"} + }, + 'end': { + 'dateTime': '2020-01-13T19:55:00+01:00', + 'timeZone': 'Europe/Brussels' + }, + } + self.env['calendar.event']._sync_google2odoo(GoogleEvent([values])) + event = self.env['calendar.event'].search([('google_id', '=', values.get('id'))]) + self.assertEqual(event.user_id, self.env.user) + + def test_valid_owner_property(self): + user = new_test_user(self.env, login='calendar-user') + values = { + 'id': 'oj44nep1ldf8a3ll02uip0c9aa', + 'description': 'Small mini desc', + 'organizer': {'email': 'odoocalendarref@gmail.com', 'self': True}, + 'summary': 'Pricing new update', + 'visibility': 'public', + 'attendees': [], + 'reminders': {'useDefault': True}, + 'start': { + 'dateTime': '2020-01-13T16:55:00+01:00', + 'timeZone': 'Europe/Brussels' + }, + 'extendedProperties': { + 'shared': {'%s_owner_id' % self.env.cr.dbname: str(user.id)} + }, + 'end': { + 'dateTime': '2020-01-13T19:55:00+01:00', + 'timeZone': 'Europe/Brussels' + }, + } + self.env['calendar.event']._sync_google2odoo(GoogleEvent([values])) + event = self.env['calendar.event'].search([('google_id', '=', values.get('id'))]) + self.assertEqual(event.user_id, user) + def test_cancelled(self): google_id = 'oj44nep1ldf8a3ll02uip0c9aa' event = self.env['calendar.event'].create({ diff --git a/addons/google_calendar/tests/test_sync_odoo2google.py b/addons/google_calendar/tests/test_sync_odoo2google.py index 7028d797470..b6a42fd14c2 100644 --- a/addons/google_calendar/tests/test_sync_odoo2google.py +++ b/addons/google_calendar/tests/test_sync_odoo2google.py @@ -84,6 +84,18 @@ class TestSyncOdoo2Google(TransactionCase): 'extendedProperties': {'shared': {'%s_odoo_id' % self.env.cr.dbname: event.id}} }) + def test_event_without_user(self): + event = self.env['calendar.event'].create({ + 'name': "Event", + 'start': datetime(2020, 1, 15, 8, 0), + 'stop': datetime(2020, 1, 15, 18, 0), + 'user_id': False, + 'privacy': 'private', + 'need_sync': False, + }) + values = event._google_values() + self.assertFalse('%s_owner_id' % self.env.cr.dbname in values.get('extendedProperties', {}).get('shared', {})) + @patch_api def test_event_allday_creation(self): event = self.env['calendar.event'].create({ diff --git a/addons/google_calendar/utils/google_event.py b/addons/google_calendar/utils/google_event.py index 9ebbd5ed7b2..8c11af3ce41 100644 --- a/addons/google_calendar/utils/google_event.py +++ b/addons/google_calendar/utils/google_event.py @@ -125,11 +125,17 @@ class GoogleEvent(abc.Set): # UserA creates an event in Odoo (he is the owner) but userB syncs first. # There is no way to insert the event into userA's calendar since we don't have # any authentication access. The event is therefore inserted into userB's calendar - # (he is the orginizer in Google). The "real" owner (in Odoo) is stored as an + # (he is the organizer in Google). The "real" owner (in Odoo) is stored as an # extended property. There is currently no support to "transfert" ownership when # userA syncs his calendar the first time. real_owner_id = self.extendedProperties and self.extendedProperties.get('shared', {}).get('%s_owner_id' % env.cr.dbname) - real_owner = real_owner_id and env['res.users'].browse(int(real_owner_id)) + try: + # If we create an event without user_id, the event properties will be 'false' + # and python will interpret this a a NoneType, that's why we have the 'except TypeError' + real_owner_id = int(real_owner_id) + except (ValueError, TypeError): + real_owner_id = False + real_owner = real_owner_id and env['res.users'].browse(real_owner_id) or env['res.users'] if real_owner_id and real_owner.exists(): return real_owner elif self.organizer and self.organizer.get('self'):