[MERGE] lp:~openerp-dev/openobject-addons/trunk-mail-alias-jam-mail-script-tpa + Fix for the logger

bzr revid: jam@tinyerp.com-20120629052651-6y6455o9uwwrr7vs
This commit is contained in:
Jigar Amin - OpenERP 2012-06-29 10:56:51 +05:30
commit 50a31d0f32
2 changed files with 8 additions and 54 deletions

View File

@ -506,16 +506,12 @@ class mail_thread(osv.osv):
mail_message_pool = self.pool.get('mail.message')
if isinstance(message, xmlrpclib.Binary):
message = str(message.data)
# Parse Message
# Warning: message_from_string doesn't always work correctly on unicode,
# we must use utf-8 strings here :-(
if isinstance(message, unicode):
message = message.encode('utf-8')
msg_txt = email.message_from_string(message)
msg = mail_message_pool.parse_message(msg_txt)
alias_name = msg.get('to')
alias_ids = alias_pool.search(cr, uid, [('alias_name','=',alias_name)])
#if alias found then call message_process method.
if alias_ids:
alias_id = alias_pool.browse(cr, uid, alias_ids[0], context)
@ -524,14 +520,10 @@ class mail_thread(osv.osv):
custom_values = alias_id.alias_defaults or {},
thread_id = alias_id.alias_force_thread_id or False,
context=context)
#if mail_alias not found give Exception
else:
#_logger.warning("This mailbox does not exist so mail gate will reject this mail.")
from_email = user_pool.browse(cr, uid, uid, context).user_email
sub = "Mail Rejection" + msg.get('subject')
message = "Respective mailbox does not exist so your mail have been rejected" + msg.get('body')
mail_compose_pool.send_mail(cr, uid, {'email_from': from_email,'email_to': msg.get('from'),'subject': sub, 'body_text': message}, context)
#if Mail box for the intended Mail Alias then give logger warning
_logger.warning("No Mail Alias Foound for the name '%s'"%(alias_name))
#raise
return True
#------------------------------------------------------

View File

@ -94,32 +94,19 @@ class RPCProxy(object):
return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request, **kwargs)
class EmailParser(object):
def __init__(self, uid, password, model, email_default, dbname, host, port):
def __init__(self, uid, password, dbname, host, port):
self.rpc = RPCProxy(uid, password, host=host, port=port, dbname=dbname)
try:
self.model_id = int(model)
self.model = str(model)
except:
self.model_id = self.rpc('ir.model', 'search', [('model', '=', model)])[0]
self.model = str(model)
self.email_default = email_default
def parse(self, message, custom_values=None, save_original=None):
def parse(self, message):
# pass message as bytes because we don't know its encoding until we parse its headers
# and hence can't convert it to utf-8 for transport
res_id = self.rpc('mail.thread',
'message_process',
self.model,
xmlrpclib.Binary(message),
custom_values or {},
save_original or False)
res_id = self.rpc('mail.thread', 'message_catchall', xmlrpclib.Binary(message))
def configure_parser():
parser = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.1')
group = optparse.OptionGroup(parser, "Note",
"This program parse a mail from standard input and communicate "
"with the OpenERP server for case management in the CRM module.")
"This program parse a mail from standard input and communicate ")
parser.add_option_group(group)
parser.add_option("-u", "--user", dest="userid",
help="ID of the user in OpenERP",
@ -127,12 +114,6 @@ def configure_parser():
parser.add_option("-p", "--password", dest="password",
help="Password of the user in OpenERP",
default=config.OPENERP_DEFAULT_PASSWORD)
parser.add_option("-o", "--model", dest="model",
help="Name or ID of crm model",
default="crm.lead")
parser.add_option("-m", "--default", dest="default",
help="Default eMail in case of any trouble.",
default=None)
parser.add_option("-d", "--dbname", dest="dbname",
help="Database name (default: %default)",
default=config.OPENERP_DEFAULT_DATABASE)
@ -142,13 +123,6 @@ def configure_parser():
parser.add_option("--port", dest="port",
help="Port of the OpenERP Server",
default=config.OPENERP_PORT)
parser.add_option("--custom-values", dest="custom_values",
help="Add Custom Values to the object",
default=None)
parser.add_option("-s", dest="save_original",
action="store_true",
help="Attach a copy of original email to the message entry",
default=False)
return parser
@ -162,24 +136,12 @@ def main():
email_parser = EmailParser(options.userid,
options.password,
options.model,
options.default,
dbname=options.dbname,
host=options.host,
port=options.port)
msg_txt = sys.stdin.read()
custom_values = {}
try:
custom_values = dict(eval(options.custom_values or {} ))
except:
import traceback
traceback.print_exc()
try:
email_parser.parse(msg_txt, custom_values, options.save_original or False)
email_parser.parse(msg_txt)
except Exception:
msg = '\n'.join([
'parameters',