在odoo v8中导入数据后如何附加pdf报告以自动发送邮件?

时间:2016-01-22 06:42:52

标签: python odoo odoo-8 email-attachments qweb

当我导入数据时,我需要附上pdf报告,该报告会自动发送声明身体部分。如果我发送手动邮件,请附上报告。

class import_data(osv.osv):

    _name = "import.data"
    _columns = {
        'csv_file' : fields.binary('CSV FILE', required="1"),
        'type':fields.selection([('NSE','NSE'),('BSE','BSE'),('Unlisted','Unlisted')],'Exchange Type',required="1")
        }

def send_mail_user(self,cr,uid,id,data,context = None):

    res_user_obj = self.pool.get('res.users')
    user = res_user_obj.browse(cr,uid, [uid], context)[0]
    mail_mail = self.pool.get('mail.mail')
    this_context = context
    if data.excess < 0.0:
        subject  =  'Share Valuation Report for'  "%s" % (data.name.name) + ' | Deposite security values'
    else:
        subject  =  'Share Valuation Report for'  "%s" % (data.name.name) + ' '         

    if user.login:
        body = '''
                    Dear Sir/Madam,

                    <p>Kindly find the share valuation report. The minimum security ratio has been maintained as on <b>''' "%s" % data.write_date +'''</b>.

                        

                    <p><table>
                            <tr>
                                <td>Current Loan :
                                    <b>''' "%s" % data.loan +'''</b>                    
                                </td>

                                <td>Date :
                                    <b>''' "%s" % data.write_date +'''</b>
                                </td>
                            </tr>
                            <tr>
                                <td>Requisite Security Ratio :
                                    <b>''' "%s" % data.rs_ratio +'''</b>                    
                                </td>

                                <td>Share Type :
                                    <b>''' "%s" % data.type +'''</b>
                                </td>
                            </tr>
                            <tr>
                                <td>Requisite Security Value :
                                    <b>''' "%s" % data.rs_value +'''</b>                    
                                </td>

                                <td>Excesss/(Shortfall) :
                                    <b>''' "%s" % data.excess +'''</b>
                                </td>
                            </tr>
                            <tr>
                                <td>Total Share :
                                    <b>''' "%s" % data.total_share +'''</b>                    
                                </td>

                                <td>Number of Share :
                                    <b>''' "%s" % data.no_share +'''</b>
                                </td>
                            </tr>

                        </table></p>

                    <p>If you have any query/ clairification please contact your Relationship Manager </p>
                    <p>Regards, </p> 
                    <p>''' "%s" % user.name +''' </p>
                    <p>''' "%s" % user.email +''' </p>
                    <p>''' "%s" % user.phone +''' </p>
                    <p>*** This is an automatically generated email, please do not reply ***</p> 
                    ''' 
        mail_values = {
            'email_from': user.email,
            'email_to': data.name.email,
            'email_cc': user.email,
            'subject': subject,
            'body_html': body,
            'state': 'outgoing',
            'type': 'email',
            }
        mail_id = mail_mail.create(cr, uid, mail_values, context=this_context)    
        a=mail_mail.send(cr, uid, [mail_id],  auto_commit=True, context=this_context)
    return True        

def import_data(self,cr,uid,ids,context = None):

        form = self.read(cr, uid, ids, [])
        if not form[0].get('csv_file'):
            raise osv.except_osv(_('Warning!'), _('Please Import CSV!'))
        fdata = form and base64.decodestring(form[0]['csv_file']) or False

        input = cStringIO.StringIO(fdata)
        input.seek(0)
        try:
            data = list(csv.reader(input, quotechar='"' or '"', delimiter=','))
        except:
             raise osv.except_osv(_('Error!'), _('please imported CSV data file is not properly set for the delimiter  !'))
        mix_log=""
        if data and form[0].get('type') == 'NSE':
            self.create_nse(cr,uid,ids,data,context=None)
        if data and form[0].get('type') == 'BSE':
            self.create_bse(cr,uid,ids,data,context=None)    

        share_mon_obj= self.pool.get('share.monitors')
        his_obj=self.pool.get('share.monitors.history')
        share_mon_ids= share_mon_obj.search(cr,uid,[])
        vals={}
        if share_mon_ids:
            for share_id in share_mon_ids:
                if share_id:
                    c_data = share_mon_obj.browse(cr,uid,share_id)
                    if not c_data.share_line:
                        raise osv.except_osv(_('Error!'), _('please create the company and share details!'))

                    for c_line in c_data.share_line:
                        val={
                            'company_names' : c_line.names and c_line.names.id or False,
                            'isin': c_line.isin_no,
                            'shares' : c_line.share,
                            'price_quote': c_line.quote_price,
                            'value_gross': c_line.gross_value,
                            'ratio_rs': c_line.rs_ratio,
                            'ratio_as': c_line.as_ratio,
                            'write_dates' : c_line.write_date, 
                            'monitor_ids': c_line.monitor_id and c_line.monitor_id.id or False,
                            'share_type':c_line.share_type or False,
                        }
                        for  csv_line in data:
                            if form[0].get('type') == 'NSE':
                                if c_line.isin_no == csv_line[12] and c_line.share_type == 'NSE':
                                    vals={
                                        'quote_price':csv_line[5] or 0.00,
                                    }
                                    his_create_id=his_obj.create(cr,uid,val)
                                    a=self.pool.get('share.monitors.info').write(cr,uid,c_line.id,vals)

                            if form[0].get('type') == 'BSE':
                                if c_line.isin_no == csv_line[0] and c_line.share_type == 'BSE':
                                    vals={
                                        'quote_price':csv_line[7] or 0.00,
                                    }
                                    his_create_id=his_obj.create(cr,uid,val)
                                    a=self.pool.get('share.monitors.info').write(cr,uid,c_line.id,vals)   

                    self.send_mail_user(cr, uid,ids,c_data,context=context)

        return True

1 个答案:

答案 0 :(得分:1)

因此,您希望发送带附件的邮件,首先您要创建带附件报告的模板。

例如: 在这里,我创建了一个模板:

<data noupdate="1">
        <!--Email template -->
        <record id="email_template_edi_purchase" model="email.template">
            <field name="name">RFQ - Send by Email</field>
            <field name="email_from">${(object.validator.email or '')|safe}</field>
            <field name="subject">${object.company_id.name|safe} Order (Ref ${object.name or 'n/a' })</field>
            <field name="partner_to">${object.partner_id.id}</field>
            <field name="model_id" ref="purchase.model_purchase_order"/>
            <field name="auto_delete" eval="True"/>
            <field name="report_template" ref="report_purchase_quotation"/>
            <field name="report_name">RFQ_${(object.name or '').replace('/','_')}</field>
            <field name="lang">${object.partner_id.lang}</field>
            <field name="body_html"><![CDATA[
<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">

    <p>Hello ${object.partner_id.name},</p>
]]></field>
        </record>

因此,在此模板中,我已将此报告附加到此模板中。

此后,您要定义发送邮件的功能。 我在下面定义一个函数:

def send_purchase_mail(self, cr, uid, ids,active_id, context=None):
        mtp =self.pool.get('email.template')
        mail_template_id = self.pool.get('ir.model.data').get_object_reference(cr, uid,
                                                    'purchase', 'email_template_edi_purchase')
        if mail_template_id:
            mtp.send_mail(cr, uid, mail_template_id[1], active_id,force_send=True, context=context)
        return True

首先在函数中我得到了template_id然后我用template_id和active_record_id调用send_mail函数之后做了什么。