[IMP] google_calendar: rename google_cal[endar]_account_id
Rename from google_cal_account_id to google_calendar_account_id, to have the complete 'service' name into the field and allow to use it from a generic way in oauth2callback from google_account e.g. task-2497212 pr-95744 closes odoo/odoo#95744 Related: odoo/upgrade#3673 Signed-off-by: Christophe Simonis <chs@odoo.com>
This commit is contained in:
@@ -13,14 +13,21 @@ class GoogleAuth(http.Controller):
|
||||
def oauth2callback(self, **kw):
|
||||
""" This route/function is called by Google when user Accept/Refuse the consent of Google """
|
||||
state = json.loads(kw['state'])
|
||||
dbname = state.get('d')
|
||||
service = state.get('s')
|
||||
url_return = state.get('f')
|
||||
|
||||
if kw.get('code'):
|
||||
access_token, refresh_token, ttl = request.env['google.service']._get_google_tokens(kw['code'], service)
|
||||
# LUL TODO only defined in google_calendar
|
||||
request.env.user.google_cal_account_id._set_auth_tokens(access_token, refresh_token, ttl)
|
||||
base_url = request.env.user.get_base_url()
|
||||
access_token, refresh_token, ttl = request.env['google.service']._get_google_tokens(
|
||||
kw['code'],
|
||||
service,
|
||||
redirect_uri=f'{base_url}/google_account/authentication'
|
||||
)
|
||||
service_field = f'google_{service}_account_id'
|
||||
if service_field in request.env.user:
|
||||
getattr(request.env.user, service_field)._set_auth_tokens(access_token, refresh_token, ttl)
|
||||
else:
|
||||
raise Warning('No callback field for service <%s>' % service)
|
||||
return request.redirect(url_return)
|
||||
elif kw.get('error'):
|
||||
return request.redirect("%s%s%s" % (url_return, "?error=", kw['error']))
|
||||
|
||||
@@ -17,7 +17,7 @@ class GoogleCredentials(models.Model):
|
||||
_name = 'google.calendar.credentials'
|
||||
_description = 'Google Calendar Account Data'
|
||||
|
||||
user_ids = fields.One2many('res.users', 'google_cal_account_id', required=True)
|
||||
user_ids = fields.One2many('res.users', 'google_calendar_account_id', required=True)
|
||||
calendar_rtoken = fields.Char('Refresh Token', copy=False)
|
||||
calendar_token = fields.Char('User token', copy=False)
|
||||
calendar_token_validity = fields.Datetime('Token Validity', copy=False)
|
||||
|
||||
@@ -14,32 +14,32 @@ _logger = logging.getLogger(__name__)
|
||||
class User(models.Model):
|
||||
_inherit = 'res.users'
|
||||
|
||||
google_cal_account_id = fields.Many2one('google.calendar.credentials')
|
||||
google_calendar_rtoken = fields.Char(related='google_cal_account_id.calendar_rtoken', groups="base.group_system")
|
||||
google_calendar_token = fields.Char(related='google_cal_account_id.calendar_token')
|
||||
google_calendar_token_validity = fields.Datetime(related='google_cal_account_id.calendar_token_validity')
|
||||
google_calendar_sync_token = fields.Char(related='google_cal_account_id.calendar_sync_token')
|
||||
google_calendar_cal_id = fields.Char(related='google_cal_account_id.calendar_cal_id')
|
||||
google_synchronization_stopped = fields.Boolean(related='google_cal_account_id.synchronization_stopped', readonly=False)
|
||||
google_calendar_account_id = fields.Many2one('google.calendar.credentials')
|
||||
google_calendar_rtoken = fields.Char(related='google_calendar_account_id.calendar_rtoken', groups="base.group_system")
|
||||
google_calendar_token = fields.Char(related='google_calendar_account_id.calendar_token')
|
||||
google_calendar_token_validity = fields.Datetime(related='google_calendar_account_id.calendar_token_validity')
|
||||
google_calendar_sync_token = fields.Char(related='google_calendar_account_id.calendar_sync_token')
|
||||
google_calendar_cal_id = fields.Char(related='google_calendar_account_id.calendar_cal_id')
|
||||
google_synchronization_stopped = fields.Boolean(related='google_calendar_account_id.synchronization_stopped', readonly=False)
|
||||
|
||||
_sql_constraints = [
|
||||
('google_token_uniq', 'unique (google_cal_account_id)', "The user has already a google account"),
|
||||
('google_token_uniq', 'unique (google_calendar_account_id)', "The user has already a google account"),
|
||||
]
|
||||
|
||||
|
||||
@property
|
||||
def SELF_READABLE_FIELDS(self):
|
||||
return super().SELF_READABLE_FIELDS + ['google_synchronization_stopped', 'google_cal_account_id']
|
||||
return super().SELF_READABLE_FIELDS + ['google_synchronization_stopped', 'google_calendar_account_id']
|
||||
|
||||
@property
|
||||
def SELF_WRITEABLE_FIELDS(self):
|
||||
return super().SELF_WRITEABLE_FIELDS + ['google_synchronization_stopped', 'google_cal_account_id']
|
||||
return super().SELF_WRITEABLE_FIELDS + ['google_synchronization_stopped', 'google_calendar_account_id']
|
||||
|
||||
def _get_google_calendar_token(self):
|
||||
self.ensure_one()
|
||||
if self.google_cal_account_id.calendar_rtoken and not self.google_cal_account_id._is_google_calendar_valid():
|
||||
self.sudo().google_cal_account_id._refresh_google_calendar_token()
|
||||
return self.google_cal_account_id.calendar_token
|
||||
if self.google_calendar_account_id.calendar_rtoken and not self.google_calendar_account_id._is_google_calendar_valid():
|
||||
self.sudo().google_calendar_account_id._refresh_google_calendar_token()
|
||||
return self.google_calendar_account_id.calendar_token
|
||||
|
||||
def _sync_google_calendar(self, calendar_service: GoogleCalendarService):
|
||||
self.ensure_one()
|
||||
@@ -56,11 +56,11 @@ class User(models.Model):
|
||||
full_sync = not bool(self.google_calendar_sync_token)
|
||||
with google_calendar_token(self) as token:
|
||||
try:
|
||||
events, next_sync_token, default_reminders = calendar_service.get_events(self.google_cal_account_id.calendar_sync_token, token=token)
|
||||
events, next_sync_token, default_reminders = calendar_service.get_events(self.google_calendar_account_id.calendar_sync_token, token=token)
|
||||
except InvalidSyncToken:
|
||||
events, next_sync_token, default_reminders = calendar_service.get_events(token=token)
|
||||
full_sync = True
|
||||
self.google_cal_account_id.calendar_sync_token = next_sync_token
|
||||
self.google_calendar_account_id.calendar_sync_token = next_sync_token
|
||||
|
||||
# Google -> Odoo
|
||||
events.clear_type_ambiguity(self.env)
|
||||
@@ -99,8 +99,8 @@ class User(models.Model):
|
||||
|
||||
def restart_google_synchronization(self):
|
||||
self.ensure_one()
|
||||
if not self.google_cal_account_id:
|
||||
self.google_cal_account_id = self.env['google.calendar.credentials'].sudo().create([{'user_ids': [Command.set(self.ids)]}])
|
||||
if not self.google_calendar_account_id:
|
||||
self.google_calendar_account_id = self.env['google.calendar.credentials'].sudo().create([{'user_ids': [Command.set(self.ids)]}])
|
||||
self.google_synchronization_stopped = False
|
||||
self.env['calendar.recurrence']._restart_google_sync()
|
||||
self.env['calendar.event']._restart_google_sync()
|
||||
|
||||
@@ -20,7 +20,7 @@ class TestTokenAccess(TransactionCase):
|
||||
'name': f'{u}',
|
||||
'login': f'{u}',
|
||||
'email': f'{u}@odoo.com',
|
||||
'google_cal_account_id': credentials.id,
|
||||
'google_calendar_account_id': credentials.id,
|
||||
})
|
||||
cls.users += [user]
|
||||
|
||||
@@ -33,14 +33,14 @@ class TestTokenAccess(TransactionCase):
|
||||
|
||||
def test_normal_user_should_be_able_to_reset_his_own_token(self):
|
||||
user = self.users[0]
|
||||
old_validity = user.google_cal_account_id.calendar_token_validity
|
||||
old_validity = user.google_calendar_account_id.calendar_token_validity
|
||||
|
||||
user.with_user(user).google_cal_account_id._set_auth_tokens('my_new_token', 'my_new_rtoken', 3600)
|
||||
user.with_user(user).google_calendar_account_id._set_auth_tokens('my_new_token', 'my_new_rtoken', 3600)
|
||||
|
||||
self.assertEqual(user.google_cal_account_id.calendar_rtoken, 'my_new_rtoken')
|
||||
self.assertEqual(user.google_cal_account_id.calendar_token, 'my_new_token')
|
||||
self.assertEqual(user.google_calendar_account_id.calendar_rtoken, 'my_new_rtoken')
|
||||
self.assertEqual(user.google_calendar_account_id.calendar_token, 'my_new_token')
|
||||
self.assertNotEqual(
|
||||
user.google_cal_account_id.calendar_token_validity,
|
||||
user.google_calendar_account_id.calendar_token_validity,
|
||||
old_validity
|
||||
)
|
||||
|
||||
@@ -48,19 +48,19 @@ class TestTokenAccess(TransactionCase):
|
||||
user1, user2 = self.users
|
||||
|
||||
with self.assertRaises(AccessError):
|
||||
user2.with_user(user1).google_cal_account_id._set_auth_tokens(False, False, 0)
|
||||
user2.with_user(user1).google_calendar_account_id._set_auth_tokens(False, False, 0)
|
||||
|
||||
def test_system_user_should_be_able_to_reset_any_tokens(self):
|
||||
user = self.users[0]
|
||||
old_validity = user.google_cal_account_id.calendar_token_validity
|
||||
old_validity = user.google_calendar_account_id.calendar_token_validity
|
||||
|
||||
user.with_user(self.system_user).google_cal_account_id._set_auth_tokens(
|
||||
user.with_user(self.system_user).google_calendar_account_id._set_auth_tokens(
|
||||
'my_new_token', 'my_new_rtoken', 3600
|
||||
)
|
||||
|
||||
self.assertEqual(user.google_cal_account_id.calendar_rtoken, 'my_new_rtoken')
|
||||
self.assertEqual(user.google_cal_account_id.calendar_token, 'my_new_token')
|
||||
self.assertEqual(user.google_calendar_account_id.calendar_rtoken, 'my_new_rtoken')
|
||||
self.assertEqual(user.google_calendar_account_id.calendar_token, 'my_new_token')
|
||||
self.assertNotEqual(
|
||||
user.google_cal_account_id.calendar_token_validity,
|
||||
user.google_calendar_account_id.calendar_token_validity,
|
||||
old_validity
|
||||
)
|
||||
|
||||
@@ -45,7 +45,7 @@ class ResetGoogleAccount(models.TransientModel):
|
||||
'need_sync': True,
|
||||
})
|
||||
|
||||
self.user_id.google_cal_account_id._set_auth_tokens(False, False, 0)
|
||||
self.user_id.google_calendar_account_id._set_auth_tokens(False, False, 0)
|
||||
self.user_id.write({
|
||||
'google_calendar_sync_token': False,
|
||||
'google_calendar_cal_id': False,
|
||||
|
||||
Reference in New Issue
Block a user