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>
47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import base64
|
|
|
|
from odoo import fields, models, api
|
|
|
|
|
|
class IrMailServer(models.Model):
|
|
"""Represents an SMTP server, able to send outgoing emails, with SSL and TLS capabilities."""
|
|
|
|
_name = 'ir.mail_server'
|
|
_inherit = ['ir.mail_server', 'google.gmail.mixin']
|
|
|
|
smtp_authentication = fields.Selection(
|
|
selection_add=[('gmail', 'Gmail OAuth Authentication')],
|
|
ondelete={'gmail': 'set default'})
|
|
|
|
@api.onchange('smtp_encryption')
|
|
def _onchange_encryption(self):
|
|
"""Do not change the SMTP configuration if it's a Gmail server
|
|
(e.g. the port which is already set)"""
|
|
if self.smtp_authentication != 'gmail':
|
|
super(IrMailServer, self)._onchange_encryption()
|
|
|
|
@api.onchange('smtp_authentication')
|
|
def _onchange_smtp_authentication(self):
|
|
if self.smtp_authentication == 'gmail':
|
|
self.smtp_host = 'smtp.gmail.com'
|
|
self.smtp_encryption = 'starttls'
|
|
self.smtp_port = 587
|
|
else:
|
|
self.smtp_encryption = 'none'
|
|
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
|
|
|
|
def _smtp_login(self, connection, smtp_user, smtp_password):
|
|
if len(self) == 1 and self.smtp_authentication == 'gmail':
|
|
auth_string = self._generate_oauth2_string(smtp_user, self.google_gmail_refresh_token)
|
|
oauth_param = base64.b64encode(auth_string.encode()).decode()
|
|
connection.ehlo()
|
|
connection.docmd('AUTH', f'XOAUTH2 {oauth_param}')
|
|
else:
|
|
super(IrMailServer, self)._smtp_login(connection, smtp_user, smtp_password)
|