Skip to content

Commit 9c82eec

Browse files
committed
[IMP] Allow to override/extend the way we get the fallback template
1 parent 7bda0cb commit 9c82eec

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

report_py3o/models/py3o_report.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ class Py3oReport(models.TransientModel):
8282
required=True
8383
)
8484

85+
@api.multi
86+
def _get_template_from_path(self, tmpl_name):
87+
""""Return the template from the path to root of the module if specied
88+
or an absolute path on your server
89+
"""
90+
if not tmpl_name:
91+
return None
92+
report_xml = self.ir_actions_report_xml_id
93+
flbk_filename = None
94+
if report_xml.module:
95+
# if the default is defined
96+
flbk_filename = pkg_resources.resource_filename(
97+
"odoo.addons.%s" % report_xml.module,
98+
tmpl_name,
99+
)
100+
elif os.path.isabs(tmpl_name):
101+
# It is an absolute path
102+
flbk_filename = os.path.normcase(os.path.normpath(tmpl_name))
103+
if flbk_filename and os.path.exists(flbk_filename):
104+
# and it exists on the fileystem
105+
with open(flbk_filename, 'r') as tmpl:
106+
return tmpl.read()
107+
return None
108+
109+
@api.multi
110+
def _get_template_fallback(self):
111+
"""
112+
Return the template referenced in the report definition
113+
:return:
114+
"""
115+
self.ensure_one()
116+
report_xml = self.ir_actions_report_xml_id
117+
return self._get_template_from_path(report_xml.py3o_template_fallback)
118+
85119
@api.multi
86120
def get_template(self):
87121
"""private helper to fetch the template data either from the database
@@ -105,22 +139,8 @@ def get_template(self):
105139
report_xml.py3o_template_id.py3o_template_data
106140
)
107141

108-
elif report_xml.py3o_template_fallback:
109-
tmpl_name = report_xml.py3o_template_fallback
110-
flbk_filename = None
111-
if report_xml.module:
112-
# if the default is defined
113-
flbk_filename = pkg_resources.resource_filename(
114-
"odoo.addons.%s" % report_xml.module,
115-
tmpl_name,
116-
)
117-
elif os.path.isabs(tmpl_name):
118-
# It is an absolute path
119-
flbk_filename = os.path.normcase(os.path.normpath(tmpl_name))
120-
if flbk_filename and os.path.exists(flbk_filename):
121-
# and it exists on the fileystem
122-
with open(flbk_filename, 'r') as tmpl:
123-
tmpl_data = tmpl.read()
142+
else:
143+
tmpl_data = self._get_template_fallback()
124144

125145
if tmpl_data is None:
126146
# if for any reason the template is not found

0 commit comments

Comments
 (0)