OCA / server-tools

Tools for Odoo Administrators to improve some technical features on Odoo.
https://odoo-community.org/psc-teams/tools-30
GNU Affero General Public License v3.0
677 stars 1.46k forks source link

[14.0][base_view_inheritance_extension] python_dict (context extension) seems broken #2202

Open rvalyi opened 2 years ago

rvalyi commented 2 years ago

Module: base_view_inheritance_extension

Describe the bug

the python_dict extension seems escaping/quoting context dict values when it should not.

To Reproduce

In a module depending on base_view_inheritance_extension, I was loading a view like:

    <record id="invoice_form" model="ir.ui.view">
        <field name="name">l10n_br_account.invoice.form</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form" />
        <field name="arch" type="xml">
            <xpath expr="//field[@name='invoice_line_ids']" position="attributes">
                <attribute name="context" operation="python_dict" key="some_key">some_value</attribute>
            </xpath>
        </field>
   </record>

And then when displaying the account.move invoice view, when trying to add a new invoice line, I had this error:

  File "/odoo/src/odoo/http.py", line 639, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/odoo/src/odoo/http.py", line 315, in _handle_exception
    raise exception.with_traceback(None) from new_cause
KeyError: "context.get('default_move_type')"

Using the Chrome network inspector, I could see that in the context default_move_type was not evaled client side as expected anymore, instead it was passed to the server as a string "context.get('default_move_type')" creating this stack trace on the server side.

The original context from the account module is:

                                <field name="invoice_line_ids"
                                       widget="section_and_note_one2many"
                                       mode="tree,kanban"
                                       context="{'default_move_type': context.get('default_move_type'), 'journal_id': journal_id, 'default_partner_id': commercial_partner_id, 'default_currency_id': currency_id or company_currency_id}">

(https://github.com/odoo/odoo/blob/14.0/addons/account/views/account_move_views.xml#L727)

Further, by overriding account.move fields_view_get, I could print the relevant view part after base_view_inheritance_extension did its job:

                        <notebook>
                            <page id="invoice_tab" string="Invoice Lines" attrs="{'invisible': [('move_type', '=', 'entry')]}" modifiers="{&quot;invisible&quot;: [[&quot;move_type&quot;, &quot;=&quot;, &quot;entry&quot;]]}">
                                <field name="invoice_line_ids" widget="section_and_note_one2many" mode="tree,kanban" context="{'default_move_type': &quot;context.get('default_move_type')&quot;, 'journal_id': journal_id, 'default_partner_id': commercial_partner_id, 'default_currency_id': currency_id or company_currency_id, 'default_fiscal_operation_id': fiscal_operation_id, 'default_document_type_id': document_type_id, 'default_fiscal_operation_type': fiscal_operation_type}" on_change="1" modifiers="{&quot;readonly&quot;: [[&quot;state&quot;, &quot;not in&quot;, [&quot;draft&quot;]]]}">
                                    </field>

                                <!-- Totals (only invoices / receipts) -->

Pay attention to the context:

 context="{'default_move_type': &quot;context.get('default_move_type')&quot;, 'journal_id': journal_id, 'default_partner_id': commercial_partner_id, 'default_currency_id': currency_id or company_currency_id, 'default_fiscal_operation_id': fiscal_operation_id, 'default_document_type_id': document_type_id, 'default_fiscal_operation_type': fiscal_operation_type}" 

You can see that some key/value pairs are properly preserved like: 'default_currency_id': currency_id or company_currency_id,

However, the value for 'default_move_type' is screwed: 'default_move_type': &quot;context.get('default_move_type')&quot; It seems it's related to the fact that the value evals the context.

As such it will be treated as a string on the client side, and not properly evaled as expected.

Expected behavior In the XML of the view, the context should look like: context="{'default_move_type': context.get('default_move_type'), 'journal_id': journal_id, ... 'some_key': some_value}"

Additional context I think this is related to this part of the code but I'm not too sure what should be done instead: https://github.com/OCA/server-tools/blob/14.0/base_view_inheritance_extension/models/ir_ui_view.py#L117

Context: I was stuck when migrating l10n_br_account to v14 because the invoice_line_ids context in the acocunt module got extra dict keys/values and we were simply replacing the context in l10n_br_account. Then I wanted to do it cleanly using python_dict from base_view_inheritance_extension as suggested in https://github.com/odoo/odoo/pull/26607 but I got stuck with this bug. As for now I will go back to a brutal context replacement, but if somebody has a patch to test for base_view_inheritance_extension I would be glad to test.

cc @pedrobaeza @Yajo @gdgellatly @hbrunn @sergio-teruel @renatonlima @marcelsavegnago @sebastienbeau @alexis-via @clementmbr @hparfr

hbrunn commented 2 years ago

this should be fixed in https://github.com/OCA/server-tools/pull/2216

rvalyi commented 2 years ago

@ivantodorovich @hbrunn thank you I will test as soon as I can (probably in a week though)