2016-03-16 129 views
1

使用NETSVC服務添加在電子郵件模板附件進行自定義模塊這顯示了錯誤無法在郵件添加附件

(result, format) = service.create(AttributeError: 'unicode' object has no attribute 'create'

這是蟒蛇odoo

def custom_send_email (self, cr, uid, ids, context=None): 
    email_obj = self.pool.get('mail.template') 

    template_id = self.pool.get('mail.template').search(cr, uid, 
     [('name', '=', 'Sales Order - Send by Email')], context=context)[0] 

    email = email_obj.browse(cr, uid, template_id) 

    attachment_obj = self.pool.get('ir.attachment') 
    ir_actions_report = self.pool.get('ir.actions.report.xml') 

    matching_reports = ir_actions_report.search(
     cr, uid, [('name', '=', 'Overtime Report')]) 

    print "Matching report " 
    print matching_reports 

    if matching_reports: 
     report = ir_actions_report.browse(cr, uid, matching_reports[0]) 
     report_service = 'report.' + report.report_name 
     service = netsvc.LocalService(report_service) 
     (result, format) = service.create(
      cr, uid, [1], {'model': self._name, 'start_date': datetime.now(), 'end_date': datetime.now()}, context=context) 


     if not report.attachment: 
      result = base64.b64encode(result) 
      file_name = "Overtime Report " + datetime.strftime(datetime.now().date(), "%Y-%m-%d") + ".pdf" 
      attachment_id = attachment_obj.create(cr, uid, 
                { 
                 'name': file_name, 
                 'datas': result, 
                 'datas_fname': file_name, 
                 'type': 'binary' 
                }, context=context) 

     email_obj.write(cr, uid, template_id, 
         { 
          'email_from': "${(object.user_id.email or '[email protected]')|safe}", 
          'email_to': "[email protected]", 
          'subject': "Over Time", 
          'body_html': "Over Time", 
          'email_recipients': "[email protected]", 
          'attachment_ids': [(6, 0, [attachment_id])], 
         }) 

     email_obj.send_mail(cr, uid, template_id, False, True, context=context) 

    return True 
+0

什麼python版本 – mnagel

+0

'report_service ='report。' + report.report_name'聞起來很糟糕。可能是因爲它,下面一行返回一個(unicode)字符串,而不是它應該的任何對象? – SiHa

回答

1

代碼在最新版本odoo netsvc.LocalService現已棄用,有一種新的方法可以做同樣的事情,即openerp.report.render_report。所以試試這個代碼。

report = ir_actions_report.browse(self.env.cr, self.env.uid, matching_reports[0]) 
report_service = 'report.' + report.report_name 
data, format = openerp.report.render_report(self.env.cr,self.env.uid, data_ids, report.report_name, {},{})