[FIX] google_calendar : check user_id before calculating ownership

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) <lul@odoo.com>
This commit is contained in:
Yas-Elak
2021-03-15 11:37:07 +00:00
committed by Lucas Lefèvre
parent 949ea5ff7b
commit 2d0343bcc3
4 changed files with 72 additions and 3 deletions
+1 -1
View File
@@ -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:
@@ -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({
@@ -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({
+8 -2
View File
@@ -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'):