Skip to content

Commit 7576867

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

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

report_py3o/models/py3o_report.py

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

8484
@api.multi
85-
def get_template(self):
85+
def _get_template_from_path(self, tmpl_name):
86+
""""Return the template from the path to root of the module if specied
87+
or an absolute path on your server
88+
"""
89+
if not tmpl_name:
90+
return None
91+
report_xml = self.ir_actions_report_xml_id
92+
flbk_filename = None
93+
if report_xml.module:
94+
# if the default is defined
95+
flbk_filename = pkg_resources.resource_filename(
96+
"odoo.addons.%s" % report_xml.module,
97+
tmpl_name,
98+
)
99+
elif os.path.isabs(tmpl_name):
100+
# It is an absolute path
101+
flbk_filename = os.path.normcase(os.path.normpath(tmpl_name))
102+
if flbk_filename and os.path.exists(flbk_filename):
103+
# and it exists on the fileystem
104+
with open(flbk_filename, 'r') as tmpl:
105+
return tmpl.read()
106+
return None
107+
108+
@api.multi
109+
def _get_template_fallback(self, model_instance):
110+
"""
111+
Return the template referenced in the report definition
112+
:return:
113+
"""
114+
self.ensure_one()
115+
report_xml = self.ir_actions_report_xml_id
116+
return self._get_template_from_path(report_xml.py3o_template_fallback)
117+
118+
@api.multi
119+
def get_template(self, model_instance):
86120
"""private helper to fetch the template data either from the database
87121
or from the default template file provided by the implementer.
88122
@@ -96,30 +130,14 @@ def get_template(self):
96130
openerp.exceptions.DeferredException
97131
"""
98132
self.ensure_one()
99-
tmpl_data = None
100133
report_xml = self.ir_actions_report_xml_id
101134
if report_xml.py3o_template_id and report_xml.py3o_template_id.id:
102135
# if a user gave a report template
103136
tmpl_data = b64decode(
104137
report_xml.py3o_template_id.py3o_template_data
105138
)
106-
107-
elif report_xml.py3o_template_fallback:
108-
tmpl_name = report_xml.py3o_template_fallback
109-
flbk_filename = None
110-
if report_xml.module:
111-
# if the default is defined
112-
flbk_filename = pkg_resources.resource_filename(
113-
"openerp.addons.%s" % report_xml.module,
114-
tmpl_name,
115-
)
116-
elif os.path.isabs(tmpl_name):
117-
# It is an absolute path
118-
flbk_filename = os.path.normcase(os.path.normpath(tmpl_name))
119-
if flbk_filename and os.path.exists(flbk_filename):
120-
# and it exists on the fileystem
121-
with open(flbk_filename, 'r') as tmpl:
122-
tmpl_data = tmpl.read()
139+
else:
140+
tmpl_data = self._get_template_fallback(model_instance)
123141

124142
if tmpl_data is None:
125143
# if for any reason the template is not found
@@ -196,7 +214,7 @@ def _create_single_report(self, model_instance, data, save_in_attachment):
196214
filetype = report_xml.py3o_filetype
197215
result_fd, result_path = tempfile.mkstemp(
198216
suffix='.' + filetype, prefix='p3o.report.tmp.')
199-
tmpl_data = self.get_template()
217+
tmpl_data = self.get_template(model_instance)
200218

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

0 commit comments

Comments
 (0)