mail_eniroment misc improvements

* Move mail_environment in root folder
* Use absolute imports for openerp and new Model classes
* Use cr argument instead of cursor, propagate context
* Update fields, remove deprecated 'method' argument, change 'states'
  otherwise the fields are not readonly (seems that the 'states' of the
  original field is kept.
* Activate the installable flag
* Indentation of the view with 2 spaces
* Remove only the attrs attribute instead of redefining the whole field
* pep8
* crm is not a dependency for mail_environment
* Fix typo in manifest
This commit is contained in:
Guewen Baconnier 2014-10-08 11:30:14 +02:00 committed by Marcos Oitaben
parent c39ccff7f1
commit 9953a5ad54
4 changed files with 169 additions and 142 deletions

View File

@ -1 +1,2 @@
# -*- coding: utf-8 -*-
from . import env_mail from . import env_mail

View File

@ -26,15 +26,17 @@
'description': """ 'description': """
Extend mail and fetch mail with server environment module. Extend mail and fetch mail with server environment module.
In config files, sections outgoint_mail and incoming_mails are default values for all Outgoing Mail Servers and Fetchmail Servers. In config files, sections outgoing_mail and incoming_mails are default values
For each server, you can (re)define values with a section named "outgoing_mail.resource_name" where resource_name is the name of your server. for all Outgoing Mail Servers and Fetchmail Servers.
For each server, you can (re)define values with a section named
"outgoing_mail.resource_name" where resource_name is the name of your server.
Exemple of config file : Exemple of config file :
[outgoing_mail] [outgoing_mail]
smtp_host = smtp.myserver.com smtp_host = smtp.myserver.com
smtp_port = 587 smtp_port = 587
smtp_user = smtp_user =
smtp_pass = smtp_pass =
smtp_encryption = ssl smtp_encryption = ssl
@ -57,11 +59,14 @@ password = openerp
'author': 'Camptocamp', 'author': 'Camptocamp',
'license': 'AGPL-3', 'license': 'AGPL-3',
'website': 'http://openerp.camptocamp.com', 'website': 'http://openerp.camptocamp.com',
'depends': ['mail', 'fetchmail', 'server_environment', 'server_environment_files', 'crm'], 'depends': ['mail',
'fetchmail',
'server_environment',
'server_environment_files',
],
'init_xml': [], 'init_xml': [],
'update_xml': ['mail_view.xml'], 'update_xml': ['mail_view.xml'],
'demo_xml': [], 'demo_xml': [],
'installable': True, 'installable': True,
'active': False, 'active': False,
} }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -19,21 +19,20 @@
# #
############################################################################## ##############################################################################
from osv import fields from openerp.osv import orm, fields
from osv import osv
from server_environment import serv_config from openerp.addons.server_environment import serv_config
class IrMail(osv.osv): class IrMail(orm.Model):
_inherit = "ir.mail_server" _inherit = "ir.mail_server"
def _get_smtp_conf(self, cursor, uid, ids, name, args, context=None): def _get_smtp_conf(self, cr, uid, ids, name, args, context=None):
""" """
Return configuration Return configuration
""" """
res = {} res = {}
for mail_server in self.browse(cursor, uid, ids): for mail_server in self.browse(cr, uid, ids, context=context):
global_section_name = 'outgoing_mail' global_section_name = 'outgoing_mail'
# default vals # default vals
@ -41,7 +40,8 @@ class IrMail(osv.osv):
if serv_config.has_section(global_section_name): if serv_config.has_section(global_section_name):
config_vals.update((serv_config.items(global_section_name))) config_vals.update((serv_config.items(global_section_name)))
custom_section_name = '.'.join((global_section_name, mail_server.name)) custom_section_name = '.'.join((global_section_name,
mail_server.name))
if serv_config.has_section(custom_section_name): if serv_config.has_section(custom_section_name):
config_vals.update(serv_config.items(custom_section_name)) config_vals.update(serv_config.items(custom_section_name))
@ -52,73 +52,85 @@ class IrMail(osv.osv):
return res return res
_columns = { _columns = {
'smtp_host': fields.function(_get_smtp_conf, 'smtp_host': fields.function(
method=True, _get_smtp_conf,
string='SMTP Server', string='SMTP Server',
type="char", type="char",
multi='outgoing_mail_config', multi='outgoing_mail_config',
size=128), states={'draft': [('readonly', True)]},
'smtp_port': fields.function(_get_smtp_conf, help="Hostname or IP of SMTP server"),
method=True, 'smtp_port': fields.function(
string='SMTP Port', _get_smtp_conf,
type="integer", string='SMTP Port',
multi='outgoing_mail_config', type="integer",
help="SMTP Port. Usually 465 for SSL, and 25 or 587 for other cases.", multi='outgoing_mail_config',
size=5), states={'draft': [('readonly', True)]},
'smtp_user': fields.function(_get_smtp_conf, help="SMTP Port. Usually 465 for SSL, "
method=True, "and 25 or 587 for other cases.",
string='Username', size=5),
type="char", 'smtp_user': fields.function(
multi='outgoing_mail_config', _get_smtp_conf,
help="Optional username for SMTP authentication", string='Username',
size=64), type="char",
'smtp_pass': fields.function(_get_smtp_conf, multi='outgoing_mail_config',
method=True, states={'draft': [('readonly', True)]},
string='Password', help="Optional username for SMTP authentication",
type="char", size=64),
multi='outgoing_mail_config', 'smtp_pass': fields.function(
help="Optional password for SMTP authentication", _get_smtp_conf,
size=64), string='Password',
'smtp_encryption' :fields.function(_get_smtp_conf, type="char",
method=True, multi='outgoing_mail_config',
string='smtp_encryption', states={'draft': [('readonly', True)]},
type="char", help="Optional password for SMTP authentication",
multi='outgoing_mail_config', size=64),
help="Choose the connection encryption scheme:\n" 'smtp_encryption': fields.function(
"- none: SMTP sessions are done in cleartext.\n" _get_smtp_conf,
"- starttls: TLS encryption is requested at start of SMTP session (Recommended)\n" string='smtp_encryption',
"- ssl: SMTP sessions are encrypted with SSL/TLS through a dedicated port (default: 465)", type="selection",
size=64)} multi='outgoing_mail_config',
selection=[('none', 'None'),
IrMail() ('starttls', 'TLS (STARTTLS)'),
('ssl', 'SSL/TLS')],
states={'draft': [('readonly', True)]},
help="Choose the connection encryption scheme:\n"
"- none: SMTP sessions are done in cleartext.\n"
"- starttls: TLS encryption is requested at start "
"of SMTP session (Recommended)\n"
"- ssl: SMTP sessions are encrypted with SSL/TLS "
"through a dedicated port (default: 465)")
}
class FetchmailServer(osv.osv): class FetchmailServer(orm.Model):
"""Incoming POP/IMAP mail server account""" """Incoming POP/IMAP mail server account"""
_inherit = 'fetchmail.server' _inherit = 'fetchmail.server'
def _get_incom_conf(self, cursor, uid, ids, name, args, context=None): def _get_incom_conf(self, cr, uid, ids, name, args, context=None):
""" """
Return configuration Return configuration
""" """
res = {} res = {}
for fetchmail in self.browse(cursor, uid, ids): for fetchmail in self.browse(cr, uid, ids, context=context):
global_section_name = 'incoming_mail' global_section_name = 'incoming_mail'
key_types = {'port': int, key_types = {'port': int,
'is_ssl': lambda a: bool(int(a)), 'is_ssl': lambda a: bool(int(a)),
'attach': lambda a: bool(int(a)), 'attach': lambda a: bool(int(a)),
'original': lambda a: bool(int(a)),} 'original': lambda a: bool(int(a)),
}
# default vals # default vals
config_vals = {'port': 993, config_vals = {'port': 993,
'is_ssl': 0, 'is_ssl': 0,
'attach': 0, 'attach': 0,
'original': 0} 'original': 0,
}
if serv_config.has_section(global_section_name): if serv_config.has_section(global_section_name):
config_vals.update(serv_config.items(global_section_name)) config_vals.update(serv_config.items(global_section_name))
custom_section_name = '.'.join((global_section_name, fetchmail.name)) custom_section_name = '.'.join((global_section_name,
fetchmail.name))
if serv_config.has_section(custom_section_name): if serv_config.has_section(custom_section_name):
config_vals.update(serv_config.items(custom_section_name)) config_vals.update(serv_config.items(custom_section_name))
@ -128,81 +140,93 @@ class FetchmailServer(osv.osv):
res[fetchmail.id] = config_vals res[fetchmail.id] = config_vals
return res return res
def _type_search(self, cr, uid, obj, name, args, context={}): def _type_search(self, cr, uid, obj, name, args, context=None):
result_ids = [] result_ids = []
# read all incomming servers values # read all incoming servers values
all_ids = self.search(cr, uid, [], context=context) all_ids = self.search(cr, uid, [], context=context)
results = self.read(cr, uid, all_ids, ['id','type'], context=context) results = self.read(cr, uid, all_ids, ['id', 'type'], context=context)
args = args[:] args = args[:]
i = 0 i = 0
while i < len(args): while i < len(args):
operator = args[i][1] operator = args[i][1]
if operator == '=': if operator == '=':
for res in results: for res in results:
if (res['type'] == args[i][2]) and (res['id'] not in result_ids): if (res['type'] == args[i][2] and
res['id'] not in result_ids):
result_ids.append(res['id']) result_ids.append(res['id'])
elif operator == 'in': elif operator == 'in':
for search_vals in args[i][2]: for search_vals in args[i][2]:
for res in results: for res in results:
if (res['type'] == search_vals) and (res['id'] not in result_ids): if (res['type'] == search_vals and
result_ids.append(res['id']) res['id'] not in result_ids):
result_ids.append(res['id'])
else: else:
continue continue
i += 1 i += 1
return [('id', 'in', result_ids)] return [('id', 'in', result_ids)]
_columns = { _columns = {
'server': fields.function(_get_incom_conf, 'server': fields.function(
method=True, _get_incom_conf,
string='Server', string='Server',
type="char", type="char",
multi='income_mail_config', multi='income_mail_config',
size=256, help="Hostname or IP of the mail server"), states={'draft': [('readonly', True)]},
'port': fields.function(_get_incom_conf, help="Hostname or IP of the mail server"),
method=True, 'port': fields.function(
string='Port', _get_incom_conf,
type="integer", string='Port',
multi='income_mail_config', type="integer",
help="Hostname or IP of the mail server"), states={'draft': [('readonly', True)]},
'type': fields.function(_get_incom_conf, multi='income_mail_config'),
method=True, 'type': fields.function(
string='Type', _get_incom_conf,
type="char", string='Type',
multi='income_mail_config', type="selection",
fnct_search=_type_search, selection=[('pop', 'POP Server'),
size=64, ('imap', 'IMAP Server'),
help="pop, imap, local"), ('local', 'Local Server'),
'is_ssl': fields.function(_get_incom_conf, ],
method=True, multi='income_mail_config',
string='Is SSL', fnct_search=_type_search,
type="boolean", states={'draft': [('readonly', True)]},
multi='income_mail_config', help="pop, imap, local"),
help='Connections are encrypted with SSL/TLS through' 'is_ssl': fields.function(
' a dedicated port (default: IMAPS=993, POP3S=995)'), _get_incom_conf,
'attach': fields.function(_get_incom_conf, string='Is SSL',
method=True, type="boolean",
string='Keep Attachments', multi='income_mail_config',
type="boolean", states={'draft': [('readonly', True)]},
multi='income_mail_config', help='Connections are encrypted with SSL/TLS through'
help="Whether attachments should be downloaded. " ' a dedicated port (default: IMAPS=993, POP3S=995)'),
"If not enabled, incoming emails will be stripped of any attachments before being processed"), 'attach': fields.function(
'original': fields.function(_get_incom_conf, _get_incom_conf,
method=True, string='Keep Attachments',
string='Keep Original', type="boolean",
type="boolean", multi='income_mail_config',
multi='income_mail_config', states={'draft': [('readonly', True)]},
help="Whether a full original copy of each email should be kept for reference" help="Whether attachments should be downloaded. "
"and attached to each processed message. This will usually double the size of your message database."), "If not enabled, incoming emails will be stripped of any "
'user': fields.function(_get_incom_conf, "attachments before being processed"),
method=True, 'original': fields.function(
string='Username', _get_incom_conf,
type="char", string='Keep Original',
multi='income_mail_config', type="boolean",
size=64), multi='income_mail_config',
'password': fields.function(_get_incom_conf, states={'draft': [('readonly', True)]},
method=True, help="Whether a full original copy of each email should be kept "
string='password', "for reference and attached to each processed message. This "
type="char", "will usually double the size of your message database."),
multi='income_mail_config', 'user': fields.function(
size=64)} _get_incom_conf,
FetchmailServer() string='Username',
type="char",
states={'draft': [('readonly', True)]},
multi='income_mail_config'),
'password': fields.function(
_get_incom_conf,
string='password',
type="char",
states={'draft': [('readonly', True)]},
multi='income_mail_config')
}

View File

@ -1,27 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<openerp> <openerp>
<data> <data>
<record model="ir.ui.view" id="inherit_fetchmail"> <record model="ir.ui.view" id="inherit_fetchmail">
<!-- must be unique in this module. --> <field name="name">inherit_fetchmail_for_env_support</field>
<field name="name">inherit_fetchmail_for_env_support</field> <field name="model">fetchmail.server</field>
<field name="model">fetchmail.server</field> <field name="inherit_id" ref="fetchmail.view_email_server_form"/>
<!--parent python entity --> <field name="arch" type="xml">
<field name="inherit_id" ref="fetchmail.view_email_server_form"/> <field name="server" position="attributes">
<!-- modulename.view --> <attribute name="attrs" eval="False"/>
<field name="arch" type="xml"> </field>
<field name="server" attrs="{'required' : [('type', '!=', 'local')]}" position="replace"> <field name="port" position="attributes">
<field name="server" /> <attribute name="attrs" eval="False"/>
</field> </field>
<field name="port" attrs="{'required' : [('type', '!=', 'local')]}" position="replace"> <field name="user" position="attributes">
<field name="port" /> <attribute name="attrs" eval="False"/>
</field> </field>
<field name="user" attrs="{'required' : [('type', '!=', 'local')]}" position="replace"> <field name="password" position="attributes">
<field name="user" /> <attribute name="attrs" eval="False"/>
</field> </field>
<field name="password" attrs="{'required' : [('type', '!=', 'local')]}" position="replace"> </field>
<field name="password" /> </record>
</field> </data>
</field>
</record>
</data>
</openerp> </openerp>