[REF] fetchmail: rename fetchmail.server type field to server_type
Purpose of this commit is to rename type column to something matching the the real business use of the field. That way it is easier to find and grep in the code. It also lessens potential conflicts with type build-in python function. It also lessens conflicts when using the field in JS as type is a build-in attribute. Renaming type column is a long-living issue. We choose to do it at the beginning of the v13 development to catch errors as soon as possible. This commit is linked to task ID 1896245. It is also a subpart of community PR #27599.
This commit is contained in:
committed by
Thibault Delavallée
parent
4f9afa6a64
commit
529c1a2e28
@@ -33,11 +33,11 @@ class FetchmailServer(models.Model):
|
||||
], string='Status', index=True, readonly=True, copy=False, default='draft')
|
||||
server = fields.Char(string='Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft': [('readonly', False)]})
|
||||
port = fields.Integer(readonly=True, states={'draft': [('readonly', False)]})
|
||||
type = fields.Selection([
|
||||
server_type = fields.Selection([
|
||||
('pop', 'POP Server'),
|
||||
('imap', 'IMAP Server'),
|
||||
('local', 'Local Server'),
|
||||
], 'Server Type', index=True, required=True, default='pop')
|
||||
], string='Server Type', index=True, required=True, default='pop', oldname='type')
|
||||
is_ssl = fields.Boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)")
|
||||
attach = fields.Boolean('Keep Attachments', help="Whether attachments should be downloaded. "
|
||||
"If not enabled, incoming emails will be stripped of any attachments before being processed", default=True)
|
||||
@@ -55,12 +55,12 @@ class FetchmailServer(models.Model):
|
||||
configuration = fields.Text('Configuration', readonly=True)
|
||||
script = fields.Char(readonly=True, default='/mail/static/scripts/openerp_mailgate.py')
|
||||
|
||||
@api.onchange('type', 'is_ssl', 'object_id')
|
||||
@api.onchange('server_type', 'is_ssl', 'object_id')
|
||||
def onchange_server_type(self):
|
||||
self.port = 0
|
||||
if self.type == 'pop':
|
||||
if self.server_type == 'pop':
|
||||
self.port = self.is_ssl and 995 or 110
|
||||
elif self.type == 'imap':
|
||||
elif self.server_type == 'imap':
|
||||
self.port = self.is_ssl and 993 or 143
|
||||
else:
|
||||
self.server = ''
|
||||
@@ -106,13 +106,13 @@ class FetchmailServer(models.Model):
|
||||
@api.multi
|
||||
def connect(self):
|
||||
self.ensure_one()
|
||||
if self.type == 'imap':
|
||||
if self.server_type == 'imap':
|
||||
if self.is_ssl:
|
||||
connection = IMAP4_SSL(self.server, int(self.port))
|
||||
else:
|
||||
connection = IMAP4(self.server, int(self.port))
|
||||
connection.login(self.user, self.password)
|
||||
elif self.type == 'pop':
|
||||
elif self.server_type == 'pop':
|
||||
if self.is_ssl:
|
||||
connection = POP3_SSL(self.server, int(self.port))
|
||||
else:
|
||||
@@ -132,14 +132,14 @@ class FetchmailServer(models.Model):
|
||||
connection = server.connect()
|
||||
server.write({'state': 'done'})
|
||||
except Exception as err:
|
||||
_logger.info("Failed to connect to %s server %s.", server.type, server.name, exc_info=True)
|
||||
_logger.info("Failed to connect to %s server %s.", server.server_type, server.name, exc_info=True)
|
||||
raise UserError(_("Connection test failed: %s") % tools.ustr(err))
|
||||
finally:
|
||||
try:
|
||||
if connection:
|
||||
if server.type == 'imap':
|
||||
if server.server_type == 'imap':
|
||||
connection.close()
|
||||
elif server.type == 'pop':
|
||||
elif server.server_type == 'pop':
|
||||
connection.quit()
|
||||
except Exception:
|
||||
# ignored, just a consequence of the previous exception
|
||||
@@ -149,7 +149,7 @@ class FetchmailServer(models.Model):
|
||||
@api.model
|
||||
def _fetch_mails(self):
|
||||
""" Method called by cron to fetch mails from servers """
|
||||
return self.search([('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])]).fetch_mail()
|
||||
return self.search([('state', '=', 'done'), ('server_type', 'in', ['pop', 'imap'])]).fetch_mail()
|
||||
|
||||
@api.multi
|
||||
def fetch_mail(self):
|
||||
@@ -159,13 +159,13 @@ class FetchmailServer(models.Model):
|
||||
}
|
||||
MailThread = self.env['mail.thread']
|
||||
for server in self:
|
||||
_logger.info('start checking for new emails on %s server %s', server.type, server.name)
|
||||
_logger.info('start checking for new emails on %s server %s', server.server_type, server.name)
|
||||
additionnal_context['fetchmail_server_id'] = server.id
|
||||
additionnal_context['server_type'] = server.type
|
||||
additionnal_context['server_type'] = server.server_type
|
||||
count, failed = 0, 0
|
||||
imap_server = None
|
||||
pop_server = None
|
||||
if server.type == 'imap':
|
||||
if server.server_type == 'imap':
|
||||
try:
|
||||
imap_server = server.connect()
|
||||
imap_server.select()
|
||||
@@ -177,19 +177,19 @@ class FetchmailServer(models.Model):
|
||||
try:
|
||||
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, data[0][1], save_original=server.original, strip_attachments=(not server.attach))
|
||||
except Exception:
|
||||
_logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True)
|
||||
_logger.info('Failed to process mail from %s server %s.', server.server_type, server.name, exc_info=True)
|
||||
failed += 1
|
||||
imap_server.store(num, '+FLAGS', '\\Seen')
|
||||
self._cr.commit()
|
||||
count += 1
|
||||
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.type, server.name, (count - failed), failed)
|
||||
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.server_type, server.name, (count - failed), failed)
|
||||
except Exception:
|
||||
_logger.info("General failure when trying to fetch mail from %s server %s.", server.type, server.name, exc_info=True)
|
||||
_logger.info("General failure when trying to fetch mail from %s server %s.", server.server_type, server.name, exc_info=True)
|
||||
finally:
|
||||
if imap_server:
|
||||
imap_server.close()
|
||||
imap_server.logout()
|
||||
elif server.type == 'pop':
|
||||
elif server.server_type == 'pop':
|
||||
try:
|
||||
while True:
|
||||
pop_server = server.connect()
|
||||
@@ -203,15 +203,15 @@ class FetchmailServer(models.Model):
|
||||
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, message, save_original=server.original, strip_attachments=(not server.attach))
|
||||
pop_server.dele(num)
|
||||
except Exception:
|
||||
_logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True)
|
||||
_logger.info('Failed to process mail from %s server %s.', server.server_type, server.name, exc_info=True)
|
||||
failed += 1
|
||||
self.env.cr.commit()
|
||||
if num_messages < MAX_POP_MESSAGES:
|
||||
break
|
||||
pop_server.quit()
|
||||
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", num_messages, server.type, server.name, (num_messages - failed), failed)
|
||||
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", num_messages, server.server_type, server.name, (num_messages - failed), failed)
|
||||
except Exception:
|
||||
_logger.info("General failure when trying to fetch mail from %s server %s.", server.type, server.name, exc_info=True)
|
||||
_logger.info("General failure when trying to fetch mail from %s server %s.", server.server_type, server.name, exc_info=True)
|
||||
finally:
|
||||
if pop_server:
|
||||
pop_server.quit()
|
||||
@@ -225,6 +225,6 @@ class FetchmailServer(models.Model):
|
||||
try:
|
||||
# Enabled/Disable cron based on the number of 'done' server of type pop or imap
|
||||
cron = self.env.ref('fetchmail.ir_cron_mail_gateway_action')
|
||||
cron.toggle(model=self._name, domain=[('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])])
|
||||
cron.toggle(model=self._name, domain=[('state', '=', 'done'), ('server_type', 'in', ['pop', 'imap'])])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<field name="arch" type="xml">
|
||||
<tree decoration-info="state == 'draft'" string="POP/IMAP Servers">
|
||||
<field name="name"/>
|
||||
<field name="type"/>
|
||||
<field name="server_type"/>
|
||||
<field name="is_ssl"/>
|
||||
<field name="object_id"/>
|
||||
<field name="date"/>
|
||||
@@ -22,7 +22,7 @@
|
||||
<field name="model">fetchmail.server</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Incoming Mail Server">
|
||||
<header attrs="{'invisible' : [('type', '=', 'local')]}">
|
||||
<header attrs="{'invisible' : [('server_type', '=', 'local')]}">
|
||||
<button string="Test & Confirm" type="object" name="button_confirm_login" states="draft"/>
|
||||
<button string="Fetch Now" type="object" name="fetch_mail" states="done"/>
|
||||
<button string="Reset Confirmation" type="object" name="set_draft" states="done"/>
|
||||
@@ -31,25 +31,25 @@
|
||||
<sheet>
|
||||
<group col="4">
|
||||
<field name="name"/>
|
||||
<field name="type"/>
|
||||
<field name="server_type"/>
|
||||
<field name="date"/>
|
||||
</group>
|
||||
<notebook>
|
||||
<page string="Server & Login">
|
||||
<group>
|
||||
<group attrs="{'invisible' : [('type', '=', 'local')]}" string="Server Information">
|
||||
<field name="server" colspan="2" attrs="{'required' : [('type', '!=', 'local')]}" />
|
||||
<field name="port" required="1" attrs="{'required' : [('type', '!=', 'local')]}" />
|
||||
<group attrs="{'invisible' : [('server_type', '=', 'local')]}" string="Server Information">
|
||||
<field name="server" colspan="2" attrs="{'required' : [('server_type', '!=', 'local')]}" />
|
||||
<field name="port" required="1" attrs="{'required' : [('server_type', '!=', 'local')]}" />
|
||||
<field name="is_ssl"/>
|
||||
</group>
|
||||
<group attrs="{'invisible' : [('type', '=', 'local')]}" string="Login Information">
|
||||
<field name="user" attrs="{'required' : [('type', '!=', 'local')]}"/>
|
||||
<field name="password" password="True" attrs="{'required' : [('type', '!=', 'local')]}"/>
|
||||
<group attrs="{'invisible' : [('server_type', '=', 'local')]}" string="Login Information">
|
||||
<field name="user" attrs="{'required' : [('server_type', '!=', 'local')]}"/>
|
||||
<field name="password" password="True" attrs="{'required' : [('server_type', '!=', 'local')]}"/>
|
||||
</group>
|
||||
<group string="Actions to Perform on Incoming Mails">
|
||||
<field name="object_id"/>
|
||||
</group>
|
||||
<group attrs="{'invisible' : [('type', '!=', 'local')]}" string="Configuration">
|
||||
<group attrs="{'invisible' : [('server_type', '!=', 'local')]}" string="Configuration">
|
||||
<field name="configuration" colspan="4"/>
|
||||
<field name="script" colspan="4" widget="url"/>
|
||||
</group>
|
||||
@@ -75,8 +75,8 @@
|
||||
<field name="arch" type="xml">
|
||||
<search string="Search Incoming Mail Servers">
|
||||
<field name="name" string="Incoming Mail Server"/>
|
||||
<filter string="IMAP" name="imap" domain="[('type','=','imap')]" help="Server type IMAP."/>
|
||||
<filter string="POP" name="pop" domain="[('type','=','pop')]" help="Server type POP."/>
|
||||
<filter string="IMAP" name="imap" domain="[('server_type','=','imap')]" help="Server type IMAP."/>
|
||||
<filter string="POP" name="pop" domain="[('server_type','=','pop')]" help="Server type POP."/>
|
||||
<separator/>
|
||||
<filter string="SSL" name="ssl" domain="[('is_ssl','=',True)]" help="If SSL required."/>
|
||||
</search>
|
||||
|
||||
Reference in New Issue
Block a user