Sharjeel Ali Shaukat
Sharjeel Ali Shaukat

Reputation: 558

Not able to add attachments in email

Using netsvc service to add attachments in email templates for custom module this shows the error

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

This is code for python 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

Upvotes: 3

Views: 510

Answers (1)

Hammad Qureshi
Hammad Qureshi

Reputation: 1136

In latest version of odoo netsvc.LocalService is now deprecated, there is a new way to do the same thing is openerp.report.render_report . So try this code instead.

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, {},{})

Upvotes: 1

Related Questions