Files
odoo_source/addons/google_calendar/wizard/reset_account.py
T
Arnaud Joset 4196ea9dc2 [FIX] google_calendar: prevent traceback when resetting the google tokens from the reset account wizard
Before this commit, the account reset would fail with the following traceback:

```py
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
AttributeError: 'res.users' object has no attribute '_set_auth_tokens'
```

This commit allows to use the wizard.

opw-2705169

closes odoo/odoo#80918

X-original-commit: 9f2aa5c87a9809899cba872e6a0428f86c687b9a
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Signed-off-by: Arnaud Joset <arj@odoo.com>
2021-12-06 16:49:24 +00:00

53 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
from odoo.addons.google_calendar.models.google_sync import google_calendar_token
from odoo.addons.google_calendar.utils.google_calendar import GoogleCalendarService
class ResetGoogleAccount(models.TransientModel):
_name = 'google.calendar.account.reset'
_description = 'Google Calendar Account Reset'
user_id = fields.Many2one('res.users', required=True)
delete_policy = fields.Selection(
[('dont_delete', "Leave them untouched"),
('delete_google', "Delete from the current Google Calendar account"),
('delete_odoo', "Delete from Odoo"),
('delete_both', "Delete from both"),
], string="User's Existing Events", required=True, default='dont_delete',
help="This will only affect events for which the user is the owner")
sync_policy = fields.Selection([
('new', "Synchronize only new events"),
('all', "Synchronize all existing events"),
], string="Next Synchronization", required=True, default='new')
def reset_account(self):
google = GoogleCalendarService(self.env['google.service'])
events = self.env['calendar.event'].search([
('user_id', '=', self.user_id.id),
('google_id', '!=', False)])
if self.delete_policy in ('delete_google', 'delete_both'):
with google_calendar_token(self.user_id) as token:
for event in events:
google.delete(event.google_id, token=token)
if self.delete_policy in ('delete_odoo', 'delete_both'):
events.google_id = False
events.unlink()
if self.sync_policy == 'all':
events.write({
'google_id': False,
'need_sync': True,
})
self.user_id.google_cal_account_id._set_auth_tokens(False, False, 0)
self.user_id.write({
'google_calendar_sync_token': False,
'google_calendar_cal_id': False,
})