排序循环的odoo电子邮件模板

时间:2019-03-12 19:39:50

标签: python xml python-2.7 odoo

我需要在odoo xml模板中对订单行进行排序; 这是代码:

`<?xml version="1.0" ?>

    

    <!--Email template -->
    <record id="email_template_route_sale" model="email.template">
        <field name="name">Master Quote - Send by Email</field>
        <field name="email_from">${(object.salesperson_id2.email or object.user_id.email  or 'sales@chriscoffee.com')|safe}</field>
        <field name="subject">Chris' Coffee Service ${object.state in ('draft', 'sent') and 'Quotation' or 'Order'} (Ref ${object.name or 'n/a' })</field>
        <field name="email_to">${(object.partner_id.email or '')|safe}</field>
        <field name="partner_to">${object.partner_id.id}</field>
        <field name="reply_to">catchall@chriscoffee.com, ${(object.salesperson_id2.email)}</field>
        <field name="model_id" ref="sale.model_sale_order"/>
        <field name="auto_delete" eval="False"/>
        <field name="report_name">${(object.name or '').replace('/','_')}_${object.state == 'draft' and 'draft' or ''}</field>
        <field name="lang">${object.partner_id.lang}</field>
        <field name="body_html"><![CDATA[

你好$ {object.partner_id.name},

<p>Here is your ${object.state in ('draft', 'sent') and 'order form' or 'order confirmation'} from ${object.company_id.name}:</p>

<p style="border-left: 1px solid #8e0000; margin-left: 30px;">
   &nbsp;&nbsp;<strong>REFERENCES</strong><br />
   &nbsp;&nbsp;Order number: <strong>${object.name}</strong><br />
   &nbsp;&nbsp;Order date: ${object.date_order}<br />
   % if object.user_id:
   &nbsp;&nbsp;Your contact: <a href="mailto:${object.salesperson_id2.email or ''}?subject=Order%20${object.name}">${object.salesperson_id2.name}</a>
   % endif
</p>
<br/>
<div style="width: 575px;">
    <div style="margin: 0px; padding: 0px; background-color: #8E0000; border-top-left-radius: 5px 5px; border-top-right-radius: 5px 5px; background-repeat: repeat no-repeat;">
       <h3 style="margin: 0px; padding: 2px 14px; font-size: 12px; color: #DDD;">
            <strong style="text-transform:uppercase;">Your items:</strong></h3>  
    </div>
<table width="575px">
  <tr>
    <td width="50%">Account #  ${object.partner_id.ref}</td>
    <td width="50%"></td>
  </tr>
  <tr>
    <td width="50%"><strong style="text-transform:uppercase;">${object.partner_id.display_name}</strong></td>
    <td width="50%"></td>
  </tr>
  <tr>
    <td width="50%">
        <span style="color: #222; margin-bottom: 5px; display: block; ">
        % if object.partner_id.street:
            ${object.partner_id.street}<br/>
        % endif
        % if object.partner_id.street2:
            ${object.partner_id.street2}<br/>
        % endif
        % if object.partner_id.city or object.partner_id.zip:
            ${object.partner_id.city}, ${object.partner_id.state_id.name} ${object.partner_id.zip}<br/>
        % endif
        % if object.partner_id.country_id:
            ${object.partner_id.state_id and ('%s, ' % object.partner_id.state_id.name) or ''} ${object.partner_id.country_id.name or ''}<br/>
        % endif
        </span>
        % if object.partner_id.phone:
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
                Phone:&nbsp; ${object.partner_id.phone}
            </div>
        % endif
        % if object.partner_id.website:
            <div>
                Web :&nbsp;<a href="${object.partner_id.website}">${object.partner_id.website}</a>
            </div>
        % endif
    </td>
    <td width="50%">
        <span style="color: #222; margin-bottom: 5px; display: block; ">
        % if object.partner_id.next_return_date:
            Delivery Date : ${object.partner_id.next_return_date}<br/>
        % endif
        % if object.driver_number.name:
            Driver/Route : ${object.driver_number.name} - ${object.timeslot_parent_id.name}<br/>
        % endif
        % if object.salesperson_id2:
            Inside Salesperson : ${object.salesperson_id2.name}<br/>
        % endif
         </span>
     </td>
  </tr>
</table>
<br/><br/>
<table style="width:575px;">
  <tr style="background-color:#c0c0c0;">
    <th width="10%" style="text-align: center;">QTY</th>
    <th width="70%" style="text-align: center;">Product</th>
    <th width="20%" style="text-align: center;">Case/Unit</th>
  </tr>
**% for line in sorted((object.order_line), key=lambda r: r['product_id.name']):**
  <tr>
    <td width="10%" style="text-align: center;" valign="middle">______  </td>
    <td width="70%">${line.product_id.name}</td>
    <td width="20%" style="text-align: center;">${line.product_uom.name}</td>
  </tr>
%endfor
</table><br/>
    <div style="margin: 0px; padding: 0px; background-color: #8E0000; border-bottom-left-radius: 5px 5px; border-bottom-right-radius: 5px 5px; background-repeat: repeat no-repeat;">
       <h3 style="margin: 0px; padding: 5px 14px; font-size: 12px; color: #DDD;">
    </div>
<br/>

请回复此电子邮件,并尽早填写所需数量。如有任何疑问,请致电518-452-5995与内部销售代表联系。

<p>Thank you for choosing Chris’ Coffee Service!</p>





        ]]></field>
    </record>
</data>

'

这是回溯:

'Traceback (most recent call last):

render_template_batch中第193行的“ /home/odoo/chris_odoo/odoo/addons/email_template/email_template.py”文件     模板= mako_template_env.from_string(tools.ustr(模板))   文件“ /usr/lib/python2.7/dist-packages/jinja2/environment.py”,第880行,位于from_string中     返回cls.from_code(self,self.compile(source),globals,None)   编译文件“ /usr/lib/python2.7/dist-packages/jinja2/environment.py”,行591     self.handle_exception(exc_info,source_hint = source_hint)   handle_exception中的文件“ /usr/lib/python2.7/dist-packages/jinja2/environment.py”,第780行     加价(exc_type,exc_value,tb)   模板中的文件“”,第78行 TemplateSyntaxError:预期令牌',',得到了'r'

1 个答案:

答案 0 :(得分:0)

此模板中的错误是由于使用通用的python表达式(在这种情况下为lambda函数)。您不能像在python脚本中那样在Jinja2模板中使用常规的python表达式。 Jinja2支持有限数量的表达式,您只能使用模板中的表达式来应用逻辑。 for line in sorted((object.order_line), key=lambda r: r['product_id.name']):是具有lambda函数的python表达式,Jinja语法不支持,这就是您收到此错误的原因。您必须在Jinja2支持的模板中转换此表达式,并需要在python文件中编写其他功能。