Skip to content

Commit 91b7898

Browse files
committed
[FIX] sale_pdf_quote_builder: prevent errors when handling non-PDF files
Currently an error occurs when user uploads a non-pdf file on product documents. Steps to replicate: - Install `sale_management` and go to products. - Open any product's form view and click on the `Documents` smart button. - Click new and upload any non-pdf file. - On the field `Sale : Visible at`, select the value `inside quote pdf` and you will get the error. Error: `PdfReadError: EOF marker not found` The error occurs because at the line [1] the code requires a pdf file, and as we have passed a non-pdf file the error occurs. [1] - https://github.com/odoo/odoo/blob/e750244c3125a48e2ca030160b977bb0344609db/addons/sale_pdf_quote_builder/models/sale_pdf_form_field.py#L206 There is already a constraint made for this particular thing [2], but the problem is that the error is due the compute [3] (Because constraints are checked at the time of form saving, and compute runs when a field is changed so even before the constraint is checked the error will be triggered). [2] - https://github.com/odoo/odoo/blob/e750244c3125a48e2ca030160b977bb0344609db/addons/sale_pdf_quote_builder/models/product_document.py#L44-L45 [3] - https://github.com/odoo/odoo/blob/e750244c3125a48e2ca030160b977bb0344609db/addons/sale_pdf_quote_builder/models/product_document.py#L59-L61 This commit resolves this issue by skipping the pdf extraction if the file is not a pdf type, because we already have a constraint [2] that will trigger at save. sentry-6161120972 closes odoo#220678 X-original-commit: ccf8f3c Signed-off-by: Bhavya Ashesh Nanavati (bhna) <bhna@odoo.com>
1 parent 6a54348 commit 91b7898

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

addons/sale_pdf_quote_builder/models/product_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _compute_form_field_ids(self):
5353
# Empty the linked form fields as we want all and only those from the current datas
5454
self.form_field_ids = [Command.clear()]
5555
document_to_parse = self.filtered(
56-
lambda doc: doc.attached_on_sale == 'inside' and doc.datas
56+
lambda doc: doc.attached_on_sale == 'inside' and doc.datas and doc.mimetype and doc.mimetype.endswith('pdf')
5757
)
5858
if document_to_parse:
5959
doc_type = 'product_document'

addons/sale_pdf_quote_builder/tests/test_pdf_quote_builder.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from werkzeug.datastructures import FileStorage
99

10+
from odoo.exceptions import ValidationError
1011
from odoo.fields import Command
1112
from odoo.tests import Form, tagged
1213
from odoo.tools.misc import file_open
@@ -194,6 +195,22 @@ def test_quotation_document_is_removed_on_template_change(self):
194195
self.assertNotIn(self.header, self.sale_order.available_quotation_document_ids)
195196
self.assertEqual(len(self.sale_order.quotation_document_ids), 0)
196197

198+
def test_non_pdf_attachment_inside_quote_form_save(self):
199+
non_pdf_att = self.env['ir.attachment'].create({
200+
'name': 'Not a PDF',
201+
'datas': b64encode(b"hello"),
202+
'mimetype': 'text/plain',
203+
})
204+
205+
product_document = self.product_document
206+
207+
product_document.write({
208+
'ir_attachment_id': non_pdf_att.id,
209+
})
210+
with self.assertRaises(ValidationError):
211+
with Form(product_document) as doc_form:
212+
doc_form.attached_on_sale = 'inside'
213+
197214
def test_onchange_product_removes_previously_selected_documents(self):
198215
""" Check that changing a line that has a selected document unselect said document. """
199216

0 commit comments

Comments
 (0)