Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data to email template from python code Odoo 10

I need to pass objects to email_template from python code:

*.py

def _alert_product_expiry(self):
    alert_data = self.search([('alert_date','=',fields.Date.today())])
    if alert_data:
        template_id = self.env.ref('alert_email_template')// Need to pass `alert_data` to template
        send = template_id.send_mail(self.id, force_send=True)

template_body

  <field name="body_html">
        <![CDATA[
       //Retrieve that object here and perform for loop. I don't know how retrieve it.
]]>

  </field>

How can i do it?

like image 476
KbiR Avatar asked Aug 03 '17 11:08

KbiR


1 Answers

Email Template

      <record id="email_weekly_status_sales_manager" model="email.template">
        <field name="name">Tickets - Weekly Status</field>
        <field name="email_from">${user.company_id.email}</field>
        <field name="subject">Weekly Status of Tickets</field>
        <field name="email_to">${object.manager_mail_id}</field>
        <field name="model_id" ref="jb_crm_claim.model_crm_claim"/>
        <field name="auto_delete" eval="False"/>
        <field name="body_html"><![CDATA[
            <p>Hi,</p>Tickets which crossed deadlines and in Progress <br><br>

            <table width="771" cellspacing="1" cellpadding="4" border="1" height="73">
            <tbody>
            <tr>
            <th>Ticket Number</th>
                <th>&nbsp;Customer name</th>
                <th>Vendor name</th>
                <th>Responsible</th>
                <th>Status</th>

            </tr>
            % if object.get_record_ids():
                % for values in object.get_record_ids()
                <tr>
                        <td>${values['ticket_number']}</td>
                        <td>${values['partner_name']}<br></td>
                        <td>${values['vendor_name']}</td>
                        <td>${values['user_name']}<br></td>
                        <td>${values['state']}</td>
                </tr>
                % endfor
                % endif
                </tbody></table><br>
                      <p>Thank you</p>
        ]]></field>
    </record>

Then write a python function to send the values to the template. In the template get_record_ids() will call the function and pass the data to the templte.

 def get_record_ids(self):
        ticket_ids = self.search(['|', ('state', '=', 'open'), ('state', '=', 'pending')])
        records=[]
        for ticket_id in ticket_ids:
            tickets={}
            if ticket_id:
               tickets['ticket_number'] =  ticket_id.ticket_number
                tickets['partner_name'] = ticket_id.partner_id.name
                tickets['vendor_name'] = ticket_idr.vendor_id.name
                tickets['user_name'] = ticket_id.user_id.name
                tickets['state'] = ticket_id.state
                records.append(tickets)
        return records

This python code will send the data to the template.

like image 108
sfx Avatar answered Sep 21 '22 17:09

sfx