[IMP] add the custom_values argument on the command line of the

mailgateway, very usefull if we want to specify some specific values
for one model

bzr revid: stephane@openerp.com-20100930153003-5tcrdshi4jcuigzh
This commit is contained in:
Stephane Wirtel
2010-09-30 17:30:03 +02:00
parent f6c48fdfa9
commit 0f268b64b4
2 changed files with 20 additions and 6 deletions
+6 -1
View File
@@ -327,7 +327,7 @@ class mailgate_tool(osv.osv_memory):
msg['to'] = email_error
tools.misc._email_send(smtp_from, self.to_email(email_error), msg, openobject_id=res.id)
def process_email(self, cr, uid, model, message, attach=True, context=None):
def process_email(self, cr, uid, model, message, custom_values=None, attach=True, context=None):
"""This function Processes email and create record for given OpenERP model
@param self: The object pointer
@param cr: the current row, from the database cursor,
@@ -346,6 +346,9 @@ class mailgate_tool(osv.osv_memory):
if not context:
context = {}
if custom_values is None or not isinstance(custom_values, dict):
custom_values = {}
model_pool = self.pool.get(model)
res_id = False
@@ -354,6 +357,8 @@ class mailgate_tool(osv.osv_memory):
att_ids = []
if hasattr(model_pool, 'message_new'):
res_id = model_pool.message_new(cr, uid, msg, context)
if custom_values:
model_pool.write(cr, uid, [res_id], custom_values, context=context)
else:
data = {
'name': msg.get('subject'),
@@ -28,7 +28,7 @@ import xmlrpclib
import email
class rpc_proxy(object):
def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'):
def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='openerp'):
self.rpc = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/%s' % (host, port, path), allow_none=True)
self.user_id = uid
self.passwd = passwd
@@ -49,11 +49,13 @@ class email_parser(object):
self.email_default = email_default
def parse(self, message):
def parse(self, message, custom_values=None):
if custom_values is None:
custom_values = {}
try:
# 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('email.server.tools', 'process_email', self.model, xmlrpclib.Binary(message))
res_id = self.rpc('email.server.tools', 'process_email', self.model, xmlrpclib.Binary(message), custom_values)
except Exception, e:
logger = logging.getLogger('mail-gateway')
logger.warning('Failed to process incoming email. Source of the failed mail is available at debug level.', exc_info=True)
@@ -70,9 +72,10 @@ if __name__ == '__main__':
parser.add_option("-p", "--password", dest="password", help="Password of the user in OpenERP", default='admin')
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: terp)", default='terp')
parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: openerp)", default='openerp')
parser.add_option("--host", dest="host", help="Hostname of the OpenERP Server", default="localhost")
parser.add_option("--port", dest="port", help="Port of the OpenERP Server", default="8069")
parser.add_option("--custom-values", dest="custom_values", help="Add Custom Values to the object", default=None)
(options, args) = parser.parse_args()
@@ -82,6 +85,12 @@ if __name__ == '__main__':
msg_txt = sys.stdin.read()
parser.parse(msg_txt)
custom_values = {}
try:
custom_values = dict(eval(options.custom_values))
except:
pass
parser.parse(msg_txt, custom_values)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: