Skip to content

Commit 4048c0b

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

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

report_py3o/models/py3o_report.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,41 @@ class Py3oReport(models.TransientModel):
8383
)
8484

8585
@api.multi
86-
def get_template(self):
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, model_instance):
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+
119+
@api.multi
120+
def get_template(self, model_instance):
87121
"""private helper to fetch the template data either from the database
88122
or from the default template file provided by the implementer.
89123
@@ -97,30 +131,15 @@ def get_template(self):
97131
odoo.exceptions.DeferredException
98132
"""
99133
self.ensure_one()
100-
tmpl_data = None
101134
report_xml = self.ir_actions_report_xml_id
102135
if report_xml.py3o_template_id and report_xml.py3o_template_id.id:
103136
# if a user gave a report template
104137
tmpl_data = b64decode(
105138
report_xml.py3o_template_id.py3o_template_data
106139
)
107140

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()
141+
else:
142+
tmpl_data = self._get_template_fallback(model_instance)
124143

125144
if tmpl_data is None:
126145
# if for any reason the template is not found
@@ -197,7 +216,7 @@ def _create_single_report(self, model_instance, data, save_in_attachment):
197216
filetype = report_xml.py3o_filetype
198217
result_fd, result_path = tempfile.mkstemp(
199218
suffix='.' + filetype, prefix='p3o.report.tmp.')
200-
tmpl_data = self.get_template()
219+
tmpl_data = self.get_template(model_instance)
201220

202221
in_stream = StringIO(tmpl_data)
203222
with closing(os.fdopen(result_fd, 'w+')) as out_stream:

0 commit comments

Comments
 (0)