Skip to content

Commit

Permalink
[4606][ADD] template_content_swapper (#26)
Browse files Browse the repository at this point in the history
[4606][ADD] template_content_swapper (#26)
  • Loading branch information
AungKoKoLin1997 authored Jun 28, 2024
1 parent 178453e commit a6cdf29
Show file tree
Hide file tree
Showing 16 changed files with 843 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/template_content_swapper/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
99 changes: 99 additions & 0 deletions template_content_swapper/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
========================
Template Content Swapper
========================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/16.0/template_content_swapper
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-template_content_swapper
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/16.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides a generic functionality to replace QWeb view elements.

Examples:

* Replace 'Salesperson' label with 'Sales Representative' in the quotation print.
* Replace 'Add to Cart' button with 'Add to Basket' in the eCommerce product page.

**Table of contents**

.. contents::
:local:

Configuration
=============

Go to *Settings > Technical > User Interface > Template Content Mappings* to
create/maintain records.

Following fields should be filled in:

* **Report** (optional): Report record that includes the string you'd like to replace.
Setting a report record will automatically update the template field.
* **Template** (required): The main QWeb template (ir.ui.view record) that includes the
string you'd like to replace.
* **Language** (optional): Target language for string replacement. If left blank, the
replacement will be applied to all languages.
* **Content From** (required): An existing string to be replaced.
* **Content To** (optional): A new string to replace the existing string.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20template_content_swapper%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Quartile Limited

Contributors
~~~~~~~~~~~~

* `Quartile <https://www.quartile.co>`_:

* Aung Ko Ko Lin
* Yoshi Tashiro
* Tatsuki Kanda

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/template_content_swapper>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions template_content_swapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
16 changes: 16 additions & 0 deletions template_content_swapper/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Template Content Swapper",
"version": "16.0.1.0.0",
"author": "Quartile Limited, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Tools",
"website": "https://github.com/OCA/server-tools",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"views/template_content_mapping_views.xml",
],
"installable": True,
}
2 changes: 2 additions & 0 deletions template_content_swapper/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ir_ui_view
from . import template_content_mapping
46 changes: 46 additions & 0 deletions template_content_swapper/models/ir_ui_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2024 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import re

from markupsafe import Markup

from odoo import models


class IrUiView(models.Model):
_inherit = "ir.ui.view"

def _render_template(self, template, values=None):
result = super()._render_template(template, values)
result_str = str(result)
lang_code = "en_US"
request = values.get("request")
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)
view = self._get(template).sudo()
content_mappings = (
self.env["template.content.mapping"]
.sudo()
.search(
[
("template_id", "=", view.id),
"|",
("lang", "=", lang_code),
("lang", "=", 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
65 changes: 65 additions & 0 deletions template_content_swapper/models/template_content_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class TemplateContentMapping(models.Model):
_name = "template.content.mapping"
_description = "Template Content Mapping"
_order = "template_id, content_from"

@api.model
def _lang_get(self):
return self.env["res.lang"].get_installed()

name = fields.Char(compute="_compute_name", store=True, readonly=True)
report_id = fields.Many2one("ir.actions.report")
template_id = fields.Many2one(
"ir.ui.view",
domain=[("type", "=", "qweb")],
required=True,
compute="_compute_template_id",
store=True,
readonly=False,
precompute=True,
help="Select the main template of the report / frontend page to be modified.",
)
lang = fields.Selection(
_lang_get,
string="Language",
default=lambda self: self.env.lang,
help="If no language is selected, the mapping will be applied to all "
"languages.",
)
active_lang_count = fields.Integer(compute="_compute_active_lang_count")
content_from = fields.Char(
required=True,
help="Set the content (string) to be replaced. e.g. 'Salesperson'.",
)
content_to = fields.Char(
help="Set your new content (string). e.g. 'Sales Representative'.",
)

@api.depends("content_from", "content_to")
def _compute_name(self):
for record in self:
record.name = False
if record.content_from:
record.name = (
f"{record.content_from or ''} -> {record.content_to or ''}"
)

@api.depends("report_id")
def _compute_template_id(self):
for rec in self:
rec.template_id = False
if rec.report_id:
report_name = rec.report_id.report_name
rec.template_id = self.env["ir.ui.view"]._get(report_name).sudo()

@api.depends("lang")
def _compute_active_lang_count(self):
lang_count = len(self.env["res.lang"].get_installed())
for rec in self:
rec.active_lang_count = lang_count
13 changes: 13 additions & 0 deletions template_content_swapper/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Go to *Settings > Technical > User Interface > Template Content Mappings* to
create/maintain records.

Following fields should be filled in:

* **Report** (optional): Report record that includes the string you'd like to replace.
Setting a report record will automatically update the template field.
* **Template** (required): The main QWeb template (ir.ui.view record) that includes the
string you'd like to replace.
* **Language** (optional): Target language for string replacement. If left blank, the
replacement will be applied to all languages.
* **Content From** (required): An existing string to be replaced.
* **Content To** (optional): A new string to replace the existing string.
5 changes: 5 additions & 0 deletions template_content_swapper/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `Quartile <https://www.quartile.co>`_:

* Aung Ko Ko Lin
* Yoshi Tashiro
* Tatsuki Kanda
6 changes: 6 additions & 0 deletions template_content_swapper/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This module provides a generic functionality to replace QWeb view elements.

Examples:

* Replace 'Salesperson' label with 'Sales Representative' in the quotation print.
* Replace 'Add to Cart' button with 'Add to Basket' in the eCommerce product page.
3 changes: 3 additions & 0 deletions template_content_swapper/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_template_content_mapping_all,template_content_mapping_all,model_template_content_mapping,,1,0,0,0
access_template_content_mapping_system,template_content_mapping_system,model_template_content_mapping,base.group_system,1,1,1,1
Loading

0 comments on commit a6cdf29

Please sign in to comment.