Skip to content

Commit

Permalink
Merge pull request #370 from Tecnativa/imp-convert_field_bootstrap_4to5
Browse files Browse the repository at this point in the history
[IMP] BS4->BS5: performance
  • Loading branch information
pedrobaeza authored Jun 10, 2024
2 parents 6a87c06 + 40741d9 commit 97bd387
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 deletions openupgradelib/openupgrade_160.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .openupgrade_tools import (
convert_html_fragment,
convert_html_replacement_class_shortcut as _r,
not_html_empty_where_clause,
replace_html_replacement_attr_shortcut as _attr_replace,
replace_html_replacement_class_rp_by_inline_shortcut as _class_rp_by_inline,
)
Expand Down Expand Up @@ -344,10 +345,9 @@ def convert_field_bootstrap_4to5(
field_name,
domain,
)
records = env[model_name].search(domain or [])
return _convert_field_bootstrap_4to5_sql(
env.cr,
records._table,
env[model_name]._table,
field_name,
)

Expand All @@ -360,8 +360,10 @@ def _convert_field_bootstrap_4to5_orm(env, model_name, field_name, domain=None):
:param str field_name: Field to convert in that model.
:param domain list: Domain to restrict conversion.
"""
domain = domain or [(field_name, "!=", False), (field_name, "!=", "<p><br></p>")]
records = env[model_name].search(domain)
domain = domain or []
query = env[model_name]._search(domain)
query.add_where(not_html_empty_where_clause(field_name))
records = env[model_name].browse(query)
update_field_multilang(
records,
field_name,
Expand Down
24 changes: 24 additions & 0 deletions openupgradelib/openupgrade_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,27 @@ def invalidate_cache(env, flush=True):
env.invalidate_all()
else:
raise Exception("Not supported Odoo version for this method.")


def not_html_empty_where_clause(column):
"""Add this where clause to your query to optimize when to deal with an html record
value or not. Follows the same rules as odoo.tools.is_html_empty
:param string column:
:return string:
The where clause for the html column ready to select only values with content.
"""
return f"""
NOT (
{column} IS NULL
OR {column} = ''
OR NOT (
regexp_replace(
{column},
'\\<\\s*\\/?(?:p|div|span|br|b|i|font)(?:(?=\\s+\\w*)[^/>]*|\\s*)/?\\s*\\>',
'',
'g'
) ~ '\\S'
)
)"""

0 comments on commit 97bd387

Please sign in to comment.