3

我在论坛上找到了这个 python 代码作为与我的问题相关的答案。我不太了解python,所以有人可以告诉我为什么这不起作用吗?

(一些背景信息:我有一个网络表单,它会自动通过电子邮件发送到 openERP,然后自动创建潜在客户。但是,当潜在客户创建时,电话和姓名等信息不会从电子邮件中读取并分类到相应的字段中以领先者的形式。)

# You can use the following variables:
#  - self: ORM model of the record on which the action is triggered
#  - object: browse_record of the record on which the action is triggered if there is one, otherwise None
#  - pool: ORM model pool (i.e. self.pool)
#  - time: Python time module
#  - cr: database cursor
#  - uid: current user id
#  - context: current context
# If you plan to return an action, assign: action = {...}

def parse_description(description):
  '''
   there is parse function
   It is example for parsing messages like this:

   Name: John
   Phone: +100500
  '''
  fields=['Name','Phone']
  _dict={}
  description=description.lower()
  for line in description.split('\n'):
    for field in fields:
        if field in line:
            split_line=line.split(':')
            if len(split_line)>1:
                pre_dict[field]=line.split(':')[1]
  return  dict

lead=self.browse(cr,uid,context['active_id'],context=context)
description=lead['description']
_dict=parse_description(description)
self.write(cr,uid,context['active_id'],{
                        'partner_name':_dict.get('name'),
                        'contact_name':_dict.get('name'),
                        'phone':_dict.get(u'phone'),
                        'mobile':_dict.get(u'phone')})

更新:

我在取邮件时得到了这些回溯

2014-07-01 13:39:40,188 4992 INFO v8_demo openerp.addons.mail.mail_thread: Routing 
mail from Atul Jain <jain.atul43@gmail.com> to jain.atul10@hotmail.com with 
Message-Id <CAG=2G76_SRthL3ybGGyx2Lai5H=RMNxUOjRRR=+5-ODrcgtEZw@mail.gmail.com>:
fallback to model:crm.lead, thread_id:False, custom_values:None, uid:1
2014-07-01 13:39:40,445 4992 ERROR v8_demo openerp.addons.fetchmail.fetchmail: 
Failed to fetch mail from imap server Gmail.
Traceback (most recent call last):
File "/home/atul/openerp-8/openerp/addons/fetchmail/fetchmail.py", line 206, in 
fetch_mail
action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids'
:[res_id], 'active_model': context.get("thread_model", server.object_id.model)})
File "/home/atul/openerp-8/openerp/addons/base/ir/ir_actions.py", line 967, in run
res = func(cr, uid, action, eval_context=eval_context, context=run_context)
File "/home/atul/openerp-8/openerp/addons/base/ir/ir_actions.py", line 805,  
in run_action_code_multi
eval(action.code.strip(), eval_context, mode="exec", nocopy=True)  # nocopy allows 
to return 'action'
File "/home/atul/openerp-8/openerp/tools/safe_eval.py", line 254, in safe_eval
return eval(c, globals_dict, locals_dict)
File "", line 14, in <module>
File "", line 4, in parse_description
ValueError: "'bool' object has no attribute 'lower'" while evaluating
u"def parse_description(description):
fields=['name','phone']
_dict={}
description=description.lower()
for line in description.split('\\n'):
for field in fields: 
if field in line:
split_line=line.split(':')
if len(split_line)>1:
_dict[field]=split_line[1]
return _dict
lead=self.browse(cr,uid,context['active_id'],context=context)\ndescription=lead['description']
_dict=parse_description(description)

 self.write(cr,uid,context['active_id'],{                'partner_name':_dict.get('name'),                             'contact_name':_dict.get('name'),        
'phone':_dict.get(u'phone'),
'mobile':_dict.get(u'phone')})"

请帮助我理解问题。

4

1 回答 1

2

我已经修复了 parse_description 函数:

def parse_description(description):
  '''
   there is parse function
   It is example for parsing messages like this:

   Name: John
   Phone: +100500
  '''
  fields=['name','phone']
  _dict={}
  description=description.lower()
  for line in description.split('\n'):
    for field in fields:
        if field in line:
            split_line=line.split(':')
            if len(split_line)>1:
                _dict[field]=split_line[1]
  return _dict
  1. 我将这些fields值更改为小写,因为对描述的所有操作都是 on description.lower()
  2. 在线上pre_dict[field]=line.split(':')[1],您正在拆分线以获得结果。这已经完成了:split_line=line.split(':')所以你可以pre_dictpre_dict[field]=split_line[1]
  3. 在同一行上,您正在使用一个变量,pre_dict,它以前没有被引用过。我认为你的意思是使用_dict所以这条线应该是_dict[field]=split_line[1]
  4. 函数返回dict的是类型,而不是变量。您可能希望它返回包含字段数据的字典,因此它应该返回_dict;否则你总会得到结果<type 'dict'>

至于剩下的代码,我没有足够的上下文来理解发生了什么或出了什么问题。至少该parse_description功能现在应该可以工作。

于 2013-07-24T16:10:52.453 回答