Skip to content

Commit c235c1f

Browse files
committed
[REF] report_qweb_signer: make signing functions reusable
1 parent ca02b2d commit c235c1f

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

report_qweb_signer/models/ir_actions_report.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ def pdf_sign(self, pdf, certificate):
211211
self._signer_endesive(params, p12, pdf, pdfsigned, passwd)
212212
return pdfsigned
213213

214-
def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
215-
report = self._get_report(report_ref)
216-
certificate = self._certificate_get(report, res_ids)
214+
def _read_attached_signed_content(self, report, res_ids, certificate):
215+
signed_content, ext = False, False
217216
if certificate and certificate.attachment:
218217
signed_content = self._attach_signed_read(res_ids, certificate)
219218
if signed_content:
@@ -223,29 +222,43 @@ def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
223222
res_ids,
224223
)
225224
return signed_content, "pdf"
225+
return signed_content, ext
226+
227+
def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
228+
report = self._get_report(report_ref)
229+
certificate = self._certificate_get(report, res_ids)
230+
signed_content, ext = self._read_attached_signed_content(
231+
report, res_ids, certificate
232+
)
233+
if signed_content:
234+
return signed_content, "pdf"
226235
content, ext = super()._render_qweb_pdf(report_ref, res_ids=res_ids, data=data)
227236
if certificate:
228-
# Creating temporary origin PDF
229-
pdf_fd, pdf = tempfile.mkstemp(suffix=".pdf", prefix="report.tmp.")
230-
with closing(os.fdopen(pdf_fd, "wb")) as pf:
231-
pf.write(content)
232-
_logger.debug(
233-
"Signing PDF document '%s' for IDs %s with certificate '%s'",
234-
self.report_name,
235-
res_ids,
236-
certificate.name,
237-
)
238-
signed = self.pdf_sign(pdf, certificate)
239-
# Read signed PDF
240-
if os.path.exists(signed):
241-
with open(signed, "rb") as pf:
242-
content = pf.read()
243-
# Manual cleanup of the temporary files
244-
for fname in (pdf, signed):
245-
try:
246-
os.unlink(fname)
247-
except OSError:
248-
_logger.error("Error when trying to remove file %s", fname)
249-
if certificate.attachment:
250-
self._attach_signed_write(res_ids, certificate, content)
237+
content = self._sign_pdf_and_attach(res_ids, certificate, content)
251238
return content, ext
239+
240+
def _sign_pdf_and_attach(self, res_ids, certificate, content):
241+
# Creating temporary origin PDF
242+
pdf_fd, pdf = tempfile.mkstemp(suffix=".pdf", prefix="report.tmp.")
243+
with closing(os.fdopen(pdf_fd, "wb")) as pf:
244+
pf.write(content)
245+
_logger.debug(
246+
"Signing PDF document '%s' for IDs %s with certificate '%s'",
247+
self.report_name,
248+
res_ids,
249+
certificate.name,
250+
)
251+
signed = self.pdf_sign(pdf, certificate)
252+
# Read signed PDF
253+
if os.path.exists(signed):
254+
with open(signed, "rb") as pf:
255+
content = pf.read()
256+
# Manual cleanup of the temporary files
257+
for fname in (pdf, signed):
258+
try:
259+
os.unlink(fname)
260+
except OSError:
261+
_logger.error("Error when trying to remove file %s", fname)
262+
if certificate.attachment:
263+
self._attach_signed_write(res_ids, certificate, content)
264+
return content

0 commit comments

Comments
 (0)