2
votes

I need to sort order lines in an odoo xml template; Here is the code:

`<?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 '[email protected]')|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">[email protected], ${(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[

Hello ${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/>

Please reply to this email and fill in the quantities needed at your earliest convenience. Please contact your inside sales representative at 518-452-5995 if you have any questions.

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





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

'

Here is the traceback:

'Traceback (most recent call last):

File "/home/odoo/chris_odoo/odoo/addons/email_template/email_template.py", line 193, in render_template_batch template = mako_template_env.from_string(tools.ustr(template)) File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 880, in from_string return cls.from_code(self, self.compile(source), globals, None) File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 591, in compile self.handle_exception(exc_info, source_hint=source_hint) File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb) File "", line 78, in template TemplateSyntaxError: expected token ',', got 'r'

1

1 Answers

1
votes

The error in this template is due to using general python expression (lambda function in this case). You can't use general python expressions in Jinja2 template, as you would write in python scripts. Jinja2 support limited number of expressions and you can use only those in the template to apply logic. for line in sorted((object.order_line), key=lambda r: r['product_id.name']): is a python expression with lambda function which is not supported in Jinja syntax that's why you are getting this error. you have to convert this expression in Jinja2 supported template, with additional functionalities needed written in the python files.