-
-
Notifications
You must be signed in to change notification settings - Fork 665
[16.0][IMP] template_content_swapper: add feature to use domain for applying some specific records in report pdfs #1202
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
Open
AungKoKoLin1997
wants to merge
1
commit into
OCA:16.0
Choose a base branch
from
qrtl:5806.0-imp-template_content_swapper
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,119 @@ | ||
| # Copyright 2024 Quartile Limited | ||
| # Copyright 2024 Quartile (https://www.quartile.co) | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| import logging | ||
| import re | ||
|
|
||
| from lxml import html | ||
| from markupsafe import Markup | ||
|
|
||
| from odoo import api, models | ||
| from odoo.tools.profiler import QwebTracker | ||
| from odoo.tools.safe_eval import safe_eval | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| ARTICLE_XPATH = '//div[contains(@class, "article") and @data-oe-model and @data-oe-id]' | ||
|
|
||
|
|
||
| class IrQWeb(models.AbstractModel): | ||
| _inherit = "ir.qweb" | ||
|
|
||
| def _apply_mappings(self, html_str, mappings, model_name=None, res_id=None): | ||
| """Apply mappings to HTML string, optionally filtering by record domain.""" | ||
| for m in mappings: | ||
| if m.domain: | ||
| if m.report_model and m.report_model != model_name: | ||
| continue | ||
| if not self._record_matches_domain(model_name, res_id, m.domain): | ||
| continue | ||
| html_str = html_str.replace(m.content_from, m.content_to or "") | ||
| return html_str | ||
|
|
||
| def _record_matches_domain(self, model_name, res_id, domain_str): | ||
| """Check if record (model_name, res_id) matches the given domain.""" | ||
| try: | ||
| dom = safe_eval(domain_str) | ||
| except Exception: | ||
| _logger.warning( | ||
| "Invalid domain on template.content.mapping for %s,%s: %s", | ||
| model_name, | ||
| res_id, | ||
| domain_str, | ||
| ) | ||
| return False | ||
| return bool(self.env[model_name].search_count([("id", "=", res_id)] + dom)) | ||
|
|
||
| def _apply_mappings_on_articles(self, articles, domain_mappings): | ||
| """Apply domain mappings per article block for multi-record renders.""" | ||
| for article in articles: | ||
| article_html = html.tostring(article, encoding="unicode") | ||
| new_html = self._apply_mappings( | ||
| article_html, | ||
| domain_mappings, | ||
| article.get("data-oe-model"), | ||
| int(article.get("data-oe-id")), | ||
| ) | ||
| if new_html != article_html: | ||
| try: | ||
| article.getparent().replace(article, html.fromstring(new_html)) | ||
| except Exception: | ||
| _logger.exception( | ||
| "Failed to replace article HTML for %s,%s", | ||
| article.get("data-oe-model"), | ||
| article.get("data-oe-id"), | ||
| ) | ||
|
|
||
| @QwebTracker.wrap_render | ||
| @api.model | ||
| def _render(self, template, values=None, **options): | ||
| result = super()._render(template, values=values, **options) | ||
| if not isinstance(template, str): | ||
| return result | ||
| result_str = str(result) | ||
| lang_code = "en_US" | ||
| values = values or {} | ||
| request = values.get("request") | ||
|
AungKoKoLin1997 marked this conversation as resolved.
|
||
| if request: | ||
| # For views | ||
| lang_code = request.env.lang | ||
| else: | ||
| # For reports | ||
| lang_match = re.search(r'data-oe-lang="([^"]+)"', result_str) | ||
| if lang_match: | ||
| lang_code = lang_match.group(1) | ||
| lang_code = lang_match.group(1) if lang_match else "en_US" | ||
| view = self.env["ir.ui.view"]._get(template) | ||
| content_mappings = ( | ||
| mappings = ( | ||
| self.env["template.content.mapping"] | ||
| .sudo() | ||
| .search( | ||
| [ | ||
| ("template_id", "=", view.id), | ||
| "|", | ||
| ("lang", "=", lang_code), | ||
| ("lang", "=", False), | ||
| ] | ||
| ) | ||
| .search([("template_id", "=", view.id), ("lang", "in", [lang_code, False])]) | ||
| ) | ||
| if content_mappings: | ||
| for mapping in content_mappings: | ||
| content_from = mapping.content_from | ||
| content_to = mapping.content_to or "" | ||
| result_str = result_str.replace(content_from, content_to) | ||
| result = Markup(result_str) | ||
| return result | ||
| if not mappings: | ||
| return result | ||
| global_mappings = [m for m in mappings if not m.domain] | ||
| domain_mappings = [m for m in mappings if m.domain] | ||
| result_str = self._apply_mappings(result_str, global_mappings) | ||
| if not domain_mappings: | ||
| return Markup(result_str) | ||
| try: | ||
| root = html.fromstring(result_str) | ||
| except Exception: | ||
| _logger.warning( | ||
| "Failed to parse HTML for template %s, skipping domain-based mappings.", | ||
| template, | ||
| ) | ||
| return Markup(result_str) | ||
| articles = root.xpath(ARTICLE_XPATH) | ||
| if not articles: | ||
| return Markup(result_str) | ||
| if len(articles) == 1: | ||
| # Single record → domain mappings can be applied globally | ||
| article = articles[0] | ||
| result_str = self._apply_mappings( | ||
| result_str, | ||
| domain_mappings, | ||
| article.get("data-oe-model"), | ||
| int(article.get("data-oe-id")), | ||
| ) | ||
| return Markup(result_str) | ||
| self._apply_mappings_on_articles(articles, domain_mappings) | ||
| final_html = html.tostring(root, encoding="unicode") | ||
| return Markup(final_html) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.