[REVIEW+IMP] emails: improve view of smtp.server and test connection process

bzr revid: hmo@tinyerp.com-20110214091158-q8u3fb35sfce076l
This commit is contained in:
Harry (OpenERP) 2011-02-14 14:41:58 +05:30
parent a011e2579f
commit 824c7f56c9
2 changed files with 24 additions and 60 deletions

View File

@ -109,7 +109,7 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
size=120,
required=False),
'smtptls':fields.boolean('TLS'),
'smtpssl':fields.boolean('SSL/TLS (only in python 2.6)'),
'smtpssl':fields.boolean('SSL/TLS'),
'default': fields.boolean('Default', help="Only one account can be default at a time"),
}
@ -147,68 +147,29 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
def name_get(self, cr, uid, ids, context=None):
return [(a["id"], "%s (%s)" % (a['email_id'], a['name'])) for a in self.read(cr, uid, ids, ['name', 'email_id'], context=context)]
def get_outgoing_server(self, cursor, user, id, context=None):
"""
Returns the Out Going Connection (SMTP) object
@attention: DO NOT USE except_osv IN THIS METHOD
@param cursor: Database Cursor
@param user: ID of current user
@param ids: ID/list of ids of current object for
which connection is required
First ID will be chosen from lists
@param context: Context
@return: SMTP server object or Exception
def test_smtp_connection(self, cr, uid, ids, context=None):
"""
#Type cast ids to integer
if type(id) == list:
id = id[0]
smtp_data = self.browse(cursor, user, id, context=context)
if smtp_data:
if smtp_data.smtpserver and smtp_data.smtpport:
try:
if smtp_data.smtpssl:
serv = smtplib.SMTP_SSL(smtp_data.smtpserver, smtp_data.smtpport)
else:
serv = smtplib.SMTP(smtp_data.smtpserver, smtp_data.smtpport)
if smtp_data.smtptls:
serv.ehlo()
serv.starttls()
serv.ehlo()
except Exception, error:
raise error
try:
if serv.has_extn('AUTH') or smtp_data.smtpuname or smtp_data.smtppass:
serv.login(str(smtp_data.smtpuname), str(smtp_data.smtppass))
except Exception, error:
raise error
return serv
raise Exception(_("SMTP SERVER or PORT not specified"))
raise Exception(_("Core connection for the given ID does not exist"))
def check_outgoing_connection(self, cursor, user, ids, context=None):
"""
checks SMTP credentials and confirms if outgoing connection works
(Attached to button)
@param cursor: Database Cursor
@param user: ID of current user
@param ids: list of ids of current object for
which connection is required
@param context: Context
Test SMTP connection works
"""
try:
for id in ids:
self.get_outgoing_server(cursor, user, id, context)
raise osv.except_osv(_("SMTP Test Connection Was Successful"), '')
except osv.except_osv, success_message:
raise success_message
for smtp_server in self.browse(cr, uid, ids, context=context):
smtp = tools.connect_smtp_server(smtp_server.smtpserver, smtp_server.smtpport, user_name=smtp_server.smtpuname,
user_password=smtp_server.smtppass, ssl=smtp_server.smtpssl, tls=smtp_server.smtptls)
try:
smtp.quit()
except Exception:
# ignored, just a consequence of the previous exception
pass
except Exception, error:
raise osv.except_osv(
_("Out going connection test failed"),
_("SMTP Connection: Test failed"),
_("Reason: %s") % error
)
raise osv.except_osv(_("SMTP Connection: Test Successfully!"), '')
# def do_approval(self, cr, uid, ids, context=None):
# #TODO: Check if user has rights
# self.write(cr, uid, ids, {'state':'approved'}, context=context)

View File

@ -135,21 +135,21 @@
<field name="model">email.smtp_server</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Email Smtp Server Configuration">
<group colspan="2">
<form string="Smtp Server Configuration">
<group colspan="4">
<field name="name"/>
<field name="default"/>
</group>
<notebook colspan="4">
<page string="Outgoing">
<page string="Configuration">
<separator string="Server Information" colspan="4"/>
<group colspan="4" col="4">
<group colspan="4" col="8">
<field name="smtpserver"/>
<button name="check_outgoing_connection" type="object" string="Test Outgoing Connection" icon="gtk-network" colspan="2"/>
<field name="smtpport"/>
<field name="smtpssl"/>
<field name="smtptls"/>
</group>
</group>
<separator string="User Information" colspan="4"/>
<group col="6" colspan="4">
<field name="email_id"/>
@ -157,6 +157,9 @@
<field name="smtppass" password="True"/>
</group>
<separator string="" colspan="4"/>
<label string="" colspan="2"/>
<button name="test_smtp_connection" type="object" string="Test Connection" icon="gtk-network" colspan="2"/>
</page>
</notebook>