Files
odoo_source/addons/fetchmail_gmail/models/fetchmail_server.py
T
std-odoo d497dbe919 [IMP] fetchmail_gmail, google_gmail: simplify the mail server form view
Purpose
=======
A field `use_google_gmail_service` has been used in stable to define
a mail server which use Gmail authentication.

But now that the fields `smtp_authentication` exists, we want to use it
to simplify the mail server form view. For the incoming mail server,
the field `server_type` will be used for the same purpose.

Add a new field to have the option to install `google_gmail` in the
main settings page.

Task-2170676

closes odoo/odoo#83413

Related: odoo/upgrade#3199
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2022-02-01 15:32:16 +00:00

46 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class FetchmailServer(models.Model):
_name = 'fetchmail.server'
_inherit = ['fetchmail.server', 'google.gmail.mixin']
server_type = fields.Selection(selection_add=[('gmail', 'Gmail OAuth Authentication')], ondelete={'gmail': 'set default'})
@api.onchange('server_type', 'is_ssl', 'object_id')
def onchange_server_type(self):
"""Set the default configuration for a IMAP Gmail server."""
if self.server_type == 'gmail':
self.server = 'imap.gmail.com'
self.is_ssl = True
self.port = 993
else:
self.google_gmail_authorization_code = False
self.google_gmail_refresh_token = False
self.google_gmail_access_token = False
self.google_gmail_access_token_expiration = False
super(FetchmailServer, self).onchange_server_type()
def _imap_login(self, connection):
"""Authenticate the IMAP connection.
If the mail server is Gmail, we use the OAuth2 authentication protocol.
"""
self.ensure_one()
if self.server_type == 'gmail':
auth_string = self._generate_oauth2_string(self.user, self.google_gmail_refresh_token)
connection.authenticate('XOAUTH2', lambda x: auth_string)
connection.select('INBOX')
else:
super(FetchmailServer, self)._imap_login(connection)
def _get_connection_type(self):
"""Return which connection must be used for this mail server (IMAP or POP).
The Gmail mail server used an IMAP connection.
"""
self.ensure_one()
return 'imap' if self.server_type == 'gmail' else super()._get_connection_type()