Skip to content

Commit

Permalink
[IMP] template_translation: export all qweb views in module
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Nov 25, 2024
1 parent e67e239 commit b70e085
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 28 deletions.
18 changes: 14 additions & 4 deletions template_translation/controllers/template_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import logging

from odoo import http
from odoo import _, http
from odoo.exceptions import UserError
from odoo.http import request

_logger = logging.getLogger(__name__) # pylint: disable=invalid-name

REQUIRED_KEYS = ["xmlid", "language"]

STATEMENT_TEMPLATE_GET = """\
SELECT vw.arch_db::jsonb->%(language)s
Expand All @@ -30,20 +32,28 @@ class TemplateGet(http.Controller):
)
def get_template(self, **kwargs):
"""Get template contents from xmlid and language."""
# Create exactly what is shown in #8264 attachment
for key in REQUIRED_KEYS:
if key not in REQUIRED_KEYS:
raise UserError(_("Missing key in %(key)s in request") % {"key": key})
xmlid = kwargs.get("xmlid", "portal.portal_share_template")
language = kwargs.get("language", "en_US")
xmlid_parts = xmlid.split(".")
query_parms = {

Check warning on line 41 in template_translation/controllers/template_get.py

View check run for this annotation

Codecov / codecov/patch

template_translation/controllers/template_get.py#L37-L41

Added lines #L37 - L41 were not covered by tests
"language": language,
"xmlid": xmlid,
"module": xmlid_parts[0],
"name": xmlid_parts[1],
}
database_cursor = request.env.cr
database_cursor.execute(STATEMENT_TEMPLATE_GET, query_parms)
template_content = database_cursor.fetchone()[0]
template_content = database_cursor.fetchone()

Check warning on line 49 in template_translation/controllers/template_get.py

View check run for this annotation

Codecov / codecov/patch

template_translation/controllers/template_get.py#L47-L49

Added lines #L47 - L49 were not covered by tests
if not template_content:
raise UserError(

Check warning on line 51 in template_translation/controllers/template_get.py

View check run for this annotation

Codecov / codecov/patch

template_translation/controllers/template_get.py#L51

Added line #L51 was not covered by tests
_("Can not find content for xmlid %(xmlid)s and language %(language)s")
% query_parms
)
result = {

Check warning on line 55 in template_translation/controllers/template_get.py

View check run for this annotation

Codecov / codecov/patch

template_translation/controllers/template_get.py#L55

Added line #L55 was not covered by tests
"template_content": template_content,
"template_content": template_content[0],
}
_logger.debug(

Check warning on line 58 in template_translation/controllers/template_get.py

View check run for this annotation

Codecov / codecov/patch

template_translation/controllers/template_get.py#L58

Added line #L58 was not covered by tests
"Retrieved template %s in language %s",
Expand Down
6 changes: 4 additions & 2 deletions template_translation/controllers/template_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@


STATEMENT_TEMPLATE_LIST = """\
SELECT name
SELECT md.name
FROM ir_model_data md
JOIN ir_ui_view vw ON md.res_id = vw.id
WHERE md.model = 'ir.ui.view'
AND md.module = %(module)s
AND md.module = %(module)s
AND vw.type = 'qweb'
"""


Expand Down
2 changes: 1 addition & 1 deletion template_translation/scripts/api_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests

# You must initialize logging, otherwise you'll not see debug output.
_logger = logging.getLogger()
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
_logger.propagate = False

Expand Down
25 changes: 16 additions & 9 deletions template_translation/scripts/template_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from api_login import get_args, get_config, get_session


def template_get(args, config, cookies):
def template_get_receive(args, xmlid=None, cookies=None):
"""Retrieve subscription information"""
config = get_config()
cookies = cookies or get_session(args, config)
config_dict = config["template_get"]
endpoint = "%s/" % config_dict["endpoint"]
headers = {"Content-Type": "application/json"}
Expand All @@ -17,22 +19,27 @@ def template_get(args, config, cookies):
json_data = json.dumps(
{
"jsonrpc": "2.0",
"params": {"xmlid": args.xmlid, "language": args.language},
"params": {
"xmlid": xmlid or args.xmlid,
"language": args.language,
},
"id": "template_get",
}
)
response = requests.get(
url=endpoint, headers=headers, data=json_data, cookies=cookies, timeout=15
)
received = response.json()
if "result" in received:
print(received["result"]["template_content"]) # to stdout
else:
print(response.text)
if "result" not in received:
raise Exception(received)
return received


def template_get(args):
received = template_get_receive(args)
print(received["result"]["template_content"]) # to stdout


if __name__ == "__main__":
main_args = get_args()
main_config = get_config()
main_cookies = get_session(main_args, main_config)
template_get(main_args, main_config, main_cookies)
template_get(main_args)
49 changes: 49 additions & 0 deletions template_translation/scripts/template_get_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2024 Therp BV <http://therp.nl>.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
import os

from api_login import get_args, get_config, get_session
from template_get import template_get_receive
from template_list import template_list_receive

_logger = logging.getLogger(__name__)


def template_write(xmlid, template_content, language=None):
"""Write template contents to export directory.
Files will get a name according to the format:
<module>.<name>.<language>.xml
"""
config = get_config()
config_dict = config["template_export"]
export_directory = config_dict["export_directory"]
os.makedirs(export_directory, exist_ok=True)
lang = language or "en_US"
path = os.path.join(export_directory, "%s.%s.xml" % (xmlid, lang))
with open(path, mode="w") as xmlfile: # Overwrite any existing content.
xmlfile.write(template_content)


def template_get_all(args):
"""Export all module texts in language to files in ~/tmp/ directory."""
config = get_config()
cookies = get_session(args, config)
received = template_list_receive(args, cookies=cookies)
template_list = received["result"]["template_list"]
for xmlid in template_list:
# Get template content one by one and write to file.
complete_xmlid = "%s.%s" % (args.module, xmlid)
received = template_get_receive(args, xmlid=complete_xmlid, cookies=cookies)
template_content = received["result"]["template_content"]
if not template_content:
_logger.debug("Did not find content for xmlid %s", complete_xmlid)
continue
template_write(complete_xmlid, template_content, language=args.language)


if __name__ == "__main__":
main_args = get_args()
template_get_all(main_args)
24 changes: 14 additions & 10 deletions template_translation/scripts/template_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from api_login import get_args, get_config, get_session


def template_list(args, config, cookies):
def template_list_receive(args, cookies=None):
"""Retrieve subscription information"""
config = get_config()
cookies = cookies or get_session(args, config)
config_dict = config["template_list"]
endpoint = "%s/" % config_dict["endpoint"]
headers = {"Content-Type": "application/json"}
Expand All @@ -23,16 +25,18 @@ def template_list(args, config, cookies):
url=endpoint, headers=headers, data=json_data, cookies=cookies, timeout=15
)
received = response.json()
if "result" in received:
template_list = received["result"]["template_list"]
for xmlid in template_list:
print("%s.%s" % (args.module, xmlid)) # to stdout
else:
print(response.text)
if "result" not in received:
raise Exception(received)
return received


def template_list(args):
received = template_list_receive(args)
template_list = received["result"]["template_list"]
for xmlid in template_list:
print("%s.%s" % (args.module, xmlid)) # to stdout


if __name__ == "__main__":
main_args = get_args()
main_config = get_config()
main_cookies = get_session(main_args, main_config)
template_list(main_args, main_config, main_cookies)
template_list(main_args)
6 changes: 4 additions & 2 deletions template_translation/scripts/template_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def template_put(args, config, cookies):
response = requests.post(
url=endpoint, headers=headers, data=json_data, cookies=cookies, timeout=15
)
if "result" not in response:
print(response.text)
received = response.json()
if "result" not in received:
raise Exception(received)
return received


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions template_translation/scripts/test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ endpoint = ${login:base_url}/servertools/template-put

[template_list]
endpoint = ${login:base_url}/servertools/template-list

[template_export]
export_directory = /home/openeyedev/tmp/templates

0 comments on commit b70e085

Please sign in to comment.