Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] util.remove_view : remove redundant t-calls #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kmod-odoo
Copy link

While removing the view, removing the content having t-call in other views which  are calling to have the content of it.

As there are many specific fixes available for this [16776,15322,14413,13404,13325,12205,12335 etc..], it's better to handle  it in remove_view.

Before fix:

  • t-call with the same xml_id will remain in other views that are using it. So during access of that view, the system is raising an error "view not found."
<t name="Payment" t-name="website_sale.payment">
    <t t-call="website_sale.cart_summary"/>
</t>

After fix:

  • t-call will be removed, so no error will be raised.
<t name="Payment" t-name="website_sale.payment">
</t>

Traceback:

Error while render the template
ValueError: View 'website_sale.cart_summary' in website 1 not found
Template: website_sale.payment
Path: /t/t/div/div[1]/div/div[4]/div[1]/t
Node: <t t-call="website_sale.address_on_payment"/>

@robodoo
Copy link
Contributor

robodoo commented Jul 19, 2024

Pull request status dashboard

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from f3943de to 13fa9cb Compare July 19, 2024 12:47
@kmod-odoo kmod-odoo requested review from a team and jjmaksoud July 19, 2024 13:20
@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch 2 times, most recently from 761da84 to 67feba3 Compare July 24, 2024 13:38
Copy link
Contributor

@diagnoza diagnoza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address pre-commit

src/util/records.py Show resolved Hide resolved
src/util/records.py Outdated Show resolved Hide resolved
src/util/records.py Outdated Show resolved Hide resolved
Comment on lines 131 to 150
if module and module not in standard_modules or not module:
_logger.info(
"The view %s with ID: %s has been updated, removed block with t-call for depricated view %s",
('"' + module + '.' + name + '"' if module else ""),
vid,
xml_id,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. We generally don't touch custom code, it's up to the client to adapt their views. Imho you should limit your query fetching the views to standard_modules (but including studio views).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We update all views when fields are removed, it may make sense to remove t-calls for views we are removing. But then this means that we would probably want to rename t-calls when we rename an xmlid of a view. @KangOl wdyt?

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from 67feba3 to 3e4f381 Compare July 24, 2024 14:08
SELECT iv.id,imd.module,imd.name
FROM ir_ui_view iv
LEFT JOIN ir_model_data imd
ON iv.id = imd.res_id AND imd.model = 'ir.ui.view'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ON iv.id = imd.res_id AND imd.model = 'ir.ui.view'
ON iv.id = imd.res_id
AND imd.model = 'ir.ui.view'

FROM ir_ui_view iv
LEFT JOIN ir_model_data imd
ON iv.id = imd.res_id AND imd.model = 'ir.ui.view'
WHERE {} ~ 't-call="{}"'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WHERE {} ~ 't-call="{}"'
WHERE {} ~ '\yt-call=(["'']){}\1"'

)
for vid, module, name in cr.fetchall():
with edit_view(cr, view_id=vid) as arch:
node = arch.find('.//t[@t-call="' + xml_id + '"]')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
node = arch.find('.//t[@t-call="' + xml_id + '"]')
node = arch.find(".//t[@t-call='{}']".format(xml_id))

standard_modules = set(modules.get_modules()) - {"studio_customization"}
if module and module not in standard_modules or not module:
_logger.info(
"The view %s with ID: %s has been updated, removed block with t-call for depricated view %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The view %s with ID: %s has been updated, removed block with t-call for depricated view %s",
"The view %s with ID: %s has been updated, removed block with t-call for deprecated view %s",

if node is not None:
node.getparent().remove(node)
standard_modules = set(modules.get_modules()) - {"studio_customization"}
if module and module not in standard_modules or not module:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified as

Suggested change
if module and module not in standard_modules or not module:
if not module or module not in standard_modules:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @KangOl, suggested changes done could you please have a look? thanks :)

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from 3e4f381 to 77b0f22 Compare July 25, 2024 12:15
@kmod-odoo
Copy link
Author

Thank you for your valuable input @diagnoza, @aj-fuentes and @KangOl.
Changes have been done. Could you please confirm the changes?

standard_modules = set(modules.get_modules()) - {"studio_customization"}
if not module or module not in standard_modules:
_logger.info(
"The view %s with ID: %s has been updated, removed block with t-call for deprecated view %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The view %s with ID: %s has been updated, removed block with t-call for deprecated view %s",
"The view %s with ID: %s has been updated, removed a t-call to a deprecated view %s",

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @diagnoza , Changes have been made, Could you please review it back @aj-fuentes @KangOl

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch 2 times, most recently from eebe33a to d746716 Compare July 30, 2024 07:26
Copy link
Contributor

@aj-fuentes aj-fuentes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle I'm OK with this. I will check with @KangOl once he's back if we should also do something similar for xmlid renames.

standard_modules = set(modules.get_modules()) - {"studio_customization"}
for vid, module, name in cr.fetchall():
with edit_view(cr, view_id=vid) as arch:
node = arch.find(".//t[@t-call='{}']".format(xml_id))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have more than one t-call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Comment on lines 117 to 124
r"""
SELECT iv.id, imd.module, imd.name
FROM ir_ui_view iv
LEFT JOIN ir_model_data imd
ON iv.id = imd.res_id
AND imd.model = 'ir.ui.view'
WHERE {} ~ '\yt-call=(["'']){}\1'
""".format(arch_col, xml_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r"""
SELECT iv.id, imd.module, imd.name
FROM ir_ui_view iv
LEFT JOIN ir_model_data imd
ON iv.id = imd.res_id
AND imd.model = 'ir.ui.view'
WHERE {} ~ '\yt-call=(["'']){}\1'
""".format(arch_col, xml_id)
format_query(cr, """
SELECT iv.id, imd.module, imd.name
FROM ir_ui_view iv
LEFT JOIN ir_model_data imd
ON iv.id = imd.res_id
AND imd.model = 'ir.ui.view'
WHERE {} ~ %s""", arch_col
),
[r"""\yt-call=(["']){}\1""".format(xml_id)]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @aj-fuentes and @KangOl

Changes have been made. Could you please confirm them?

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch 3 times, most recently from 6036e6b to 20793ea Compare August 5, 2024 11:37
@KangOl
Copy link
Contributor

KangOl commented Aug 5, 2024

Note that t-call will also search for views whose key match.
So we should also search for the key of the delete view (even if it hasn't any xmlid).

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch 2 times, most recently from 5d985e6 to 0f26f72 Compare August 8, 2024 09:32
@kmod-odoo
Copy link
Author

Note that t-call will also search for views whose key match. So we should also search for the key of the delete view (even if it hasn't any xmlid).

Hello @KangOl,
Changes were made. Could you please confirm them?

@KangOl
Copy link
Contributor

KangOl commented Aug 8, 2024

upgradeci retry with always hr

@kmod-odoo
Copy link
Author

upgradeci retry with always hr

Hello @KangOl,
matt is still failing due to accounting and other issues unrelated to this patch. Should I take any further action?

@KangOl
Copy link
Contributor

KangOl commented Aug 12, 2024

Can you please add a test?

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from e9489d4 to 624e52c Compare August 14, 2024 10:18
@kmod-odoo
Copy link
Author

Hello @KangOl,

I have added unit test for remove_view. Could you please review it?

@kmod-odoo
Copy link
Author

Hello @KangOl,
Just a gentle reminder to review this PR

@KangOl
Copy link
Contributor

KangOl commented Aug 21, 2024

The key can be different from the xmlid. You need to handle the both.

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from 624e52c to eb9b3de Compare September 4, 2024 12:13
@kmod-odoo
Copy link
Author

Hello @KangOl,
Changes were made. Could you please confirm them?

@kmod-odoo
Copy link
Author

Hello @KangOl,
Just a gentle reminder to review this PR

@kmod-odoo
Copy link
Author

Gentle reminder @KangOl

@kmod-odoo
Copy link
Author

kmod-odoo commented Oct 10, 2024

Hello @KangOl
Just a friendly reminder to review this PR
cc: @aj-fuentes

@@ -1622,3 +1635,46 @@ def remove_act_window_view_mode(cr, model, view_mode):
""",
[view_mode, model, view_mode, view_mode],
)


def remove_redudant_tcalls(cr, match=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the name, also make this method private, is not something we

Suggested change
def remove_redudant_tcalls(cr, match=None):
def _remove_redundant_tcalls(cr, match):

Why having a default value for match?

Comment on lines 115 to 120
if xml_id != "?" or key:
if xml_id != key:
remove_redudant_tcalls(cr, xml_id)
remove_redudant_tcalls(cr, key)
else:
remove_redudant_tcalls(cr, xml_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be made more direct. Also if xml_id is ? you don't need to remove by xml_id.

Suggested change
if xml_id != "?" or key:
if xml_id != key:
remove_redudant_tcalls(cr, xml_id)
remove_redudant_tcalls(cr, key)
else:
remove_redudant_tcalls(cr, xml_id)
if xml_id != "?":
remove_redundant_tcalls(cr, xml_id)
if key and key != xml_id:
remove_redundant_tcalls(cr, key)

Comment on lines 1676 to 1677
"The view %s with ID: %s has been updated, removed a t-call to a deprecated view %s",
('"{}.{}"'.format(module, name) if module else ""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The view %s with ID: %s has been updated, removed a t-call to a deprecated view %s",
('"{}.{}"'.format(module, name) if module else ""),
"The view %swith ID: %s has been updated, removed t-calls to deprecated %r",
("`{}.{}` ".format(module, name) if module else ""),

Comment on lines 1658 to 1786
SELECT iv.id, imd.module, imd.name
FROM ir_ui_view iv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SELECT iv.id, imd.module, imd.name
FROM ir_ui_view iv
SELECT iv.id,
imd.module,
imd.name
FROM ir_ui_view iv

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from eb9b3de to 6afb5a5 Compare October 14, 2024 12:20
@kmod-odoo
Copy link
Author

Hello @aj-fuentes, suggested changes done could you please have a look? thanks :)

Copy link
Contributor

@aj-fuentes aj-fuentes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments

""",
sql.SQL(arch_col),
),
[r"""\yt-call=(["'']){}\1""".format(match)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are passing the regexp as parameter you don't need to duplicate the single quote.
You need to regexp escape match otherwise . will match any character. Since you anyway do a xmlnode.findall later this won't at least cause wrong upates. But you can decrease the output of the query by escaping the .match

Suggested change
[r"""\yt-call=(["'']){}\1""".format(match)],
[r"""\yt-call=(["']){}\1""".format(re.escape(match))],

Comment on lines 1707 to 1708
This function removes the t-call of the removed view from all types of views
based on xml_id and key.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This function removes the t-call of the removed view from all types of views
based on xml_id and key.
This function removes the t-calls to `match`.

This function removes the t-call of the removed view from all types of views
based on xml_id and key.

:param str match: contains xml_id or key based on argument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param str match: contains xml_id or key based on argument
:param str match: t-calls value to remove, typically it would be a view's xml_id or key

@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from 6afb5a5 to b9f0a16 Compare October 15, 2024 11:17
@kmod-odoo
Copy link
Author

Thank you for your valuable input @aj-fuentes.
Changes have been done. Could you please confirm the changes?

Copy link
Contributor

@aj-fuentes aj-fuentes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

cc: @KangOl

@kmod-odoo
Copy link
Author

Hello @KangOl,
Just a gentle reminder to review this PR

@kmod-odoo
Copy link
Author

@KangOl Waiting for your final review as @aj-fuentes has approved this PR. Thanks!

@KangOl
Copy link
Contributor

KangOl commented Oct 28, 2024

I would also adapt util.rename_xmlid to alter the t-call (we already change the key of the views).

while removing the view, removing the content having t-call in other views which 
are calling to have the content of it.
As there are many specific fixes available for this, it's better to handle 
it in remove_view.

Before fix:
t-call with the same xml_id will remain in other views that are using it.
So during access of that view, the system is raising an error "view not found."
```
<t name="Payment" t-name="website_sale.payment">
    <t t-call="website_sale.cart_summary"/>
</t>
```

After fix:
t-call will be removed, so no error will be raised.
```
<t name="Payment" t-name="website_sale.payment">
</t>
```

Traceback:
```
Error while render the template
ValueError: View 'website_sale.cart_summary' in website 1 not found
Template: website_sale.payment
Path: /t/t/div/div[1]/div/div[4]/div[1]/t
Node: <t t-call="website_sale.address_on_payment"/>
```
Testcases for remove_view and rename_xmlid
@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from b9f0a16 to 4daaa9a Compare December 2, 2024 11:12
@kmod-odoo
Copy link
Author

Hello @KangOl @aj-fuentes
I have adapted the rename_xmlid method to handle t-call, t-value and t-name updates and added tests for it; could you please have a look?
Thanks :)

UPDATE ir_ui_view
SET {arch} = regexp_replace({arch}, %s, %s, 'g')
WHERE {arch} ~ %s
""".format(arch=arch_col),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use format_query instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @KangOl suggested changes done could you please have a look? thanks :)

When renaming a view, we should also update its references in other views where
it is used in t-call, t-value and t-name attributes. This ensures consistency and
prevents broken references in dependent views.
@kmod-odoo kmod-odoo force-pushed the master-improve_remove_view-kmod branch from 4daaa9a to 9b1dbb4 Compare December 3, 2024 06:02
@kmod-odoo
Copy link
Author

Hello @KangOl
Just a gentle reminder to review this PR

@kmod-odoo
Copy link
Author

Hello @KangOl
Just a friendly reminder to review this PR

cc: @aj-fuentes

@kmod-odoo
Copy link
Author

Hello @KangOl
A gentle reminder, Could you please review this PR?

cc: @aj-fuentes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants