Skip to content

Commit 631c757

Browse files
bosdbosd
authored andcommitted
[IMP] report_pdf_form: Various improvements
fix(report_pdf_form): move binding to PDF form record This commit moves the binding configuration from the ir.actions.report record to the actual report.pdf.form record, which is the proper location for the binding since the module inherits from ir.actions.report using _inherits. This ensures the demo report appears in the contact's print menu.
1 parent 26879bd commit 631c757

6 files changed

Lines changed: 57 additions & 31 deletions

File tree

report_pdf_form/README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ Creating PDF Form Fields
4444
To create PDFs with form fields for use with this module, you can use
4545
various tools:
4646

47-
- **docfly.com**: An online tool for adding form fields to PDFs
48-
- **LibreOffice Draw**: Part of the LibreOffice suite, can add form
49-
fields to PDFs
50-
- **Scribus**: A desktop publishing application that can create PDFs
51-
with form fields
47+
- **docfly.com**: An online tool for adding form fields to PDFs
48+
- **LibreOffice Draw**: Part of the LibreOffice suite, can add form
49+
fields to PDFs
50+
- **Scribus**: A desktop publishing application that can create PDFs
51+
with form fields
5252

5353
.. IMPORTANT::
5454
This is an alpha version, the data model and design can change at any time without warning.
@@ -87,7 +87,7 @@ Authors
8787
Contributors
8888
------------
8989

90-
- Akim Juillerat [email protected]
90+
- Akim Juillerat [email protected]
9191

9292
Maintainers
9393
-----------

report_pdf_form/demo/report_pdf_form_demo.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
<field name="report_name">demo_partner_report</field>
2020
<field name="report_type">qweb-pdf</field>
2121
<field name="print_report_name">object.name</field>
22-
<field name="binding_model_id" ref="base.model_res_partner" />
23-
<field name="binding_type">report</field>
2422
</record>
2523

2624
<!-- Demo PDF form configuration -->
@@ -29,6 +27,8 @@
2927
<field name="report_id" ref="demo_partner_report" />
3028
<field name="pdf_attachment_id" ref="demo_pdf_attachment" />
3129
<field name="model_id" ref="base.model_res_partner" />
30+
<field name="binding_model_id" ref="base.model_res_partner" />
31+
<field name="binding_type">report</field>
3232
</record>
3333

3434
<!-- Demo field mappings -->

report_pdf_form/models/report_pdf_form.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ def create(self, vals_list):
5151
required=True,
5252
)
5353
model_id = fields.Many2one("ir.model", ondelete="cascade", required=True)
54+
55+
@api.onchange("report_id")
56+
def _onchange_report_id(self):
57+
"""Auto-set model_id based on report_id's model."""
58+
if self.report_id and self.report_id.model:
59+
model = self.env["ir.model"].search(
60+
[("model", "=", self.report_id.model)], limit=1
61+
)
62+
if model:
63+
self.model_id = model.id
64+
5465
# TODO:
5566
field_mapping_ids = fields.One2many(
5667
"report.pdf.form.field",
@@ -74,8 +85,8 @@ def action_preview_pdf(self):
7485
sample_record = self.env[model_name].search([], limit=1)
7586
if not sample_record:
7687
message = self.env._(
77-
"No records found for model {model}. Cannot generate preview."
78-
).format(model=model_name)
88+
"No records found for model %(model_name)s. Cannot generate preview."
89+
) % {"model_name": model_name}
7990
raise UserError(message)
8091

8192
# Generate the PDF using the same logic as the report
@@ -150,7 +161,7 @@ def action_preview_pdf(self):
150161
"target": "new",
151162
}
152163
except Exception as e:
153-
message = self.env._("Could not generate PDF preview: {error}").format(
154-
error=str(e)
155-
)
164+
message = self.env._("Could not generate PDF preview: %(error)s") % {
165+
"error": str(e)
166+
}
156167
raise UserError(message) from e

report_pdf_form/models/report_pdf_form_field.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ def _validate_dotted_path(self):
5656
return True # Not a dotted path or empty, considered valid
5757

5858
# Get the model from the parent report form
59+
if (
60+
not self.report_form_id
61+
or not self.report_form_id.model_id
62+
or not self.report_form_id.model_id.model
63+
):
64+
return True # Model not available yet, skip validation during creation
5965
model_name = self.report_form_id.model_id.model
60-
if not model_name:
61-
return False
66+
if not model_name or model_name not in self.env:
67+
return True # Model not set or not in registry, skip validation
6268

6369
# Split the dotted path
6470
path_parts = self.odoo_field_value.split(".")
6571

6672
# Start with the base model
6773
current_model = self.env[model_name]
68-
if not current_model:
69-
return False
7074

7175
# Traverse the path
7276
for i, field_name in enumerate(path_parts):
@@ -78,9 +82,9 @@ def _validate_dotted_path(self):
7882
if field.type not in ["many2one", "one2many", "many2many"]:
7983
return False # Can't traverse further on non-relation field
8084
# Move to the related model
85+
if not field.comodel_name or field.comodel_name not in self.env:
86+
return False # comodel not found
8187
current_model = self.env[field.comodel_name]
82-
if not current_model:
83-
return False
8488

8589
return True
8690

@@ -94,18 +98,19 @@ def action_validate_field(self):
9498
"tag": "display_notification",
9599
"params": {
96100
"title": "Success",
97-
"message": f'The dotted path "{self.odoo_field_value}" is valid.',
101+
"message": self.env._('The dotted path "%(path)s" is valid.')
102+
% {"path": self.odoo_field_value},
98103
"type": "success",
99104
"sticky": False,
100105
},
101106
}
102107
else:
103108
message = self.env._(
104-
'The dotted path "{path}" is invalid for model "{model}".'
105-
).format(
106-
path=self.odoo_field_value,
107-
model=self.report_form_id.model_id.name,
108-
)
109+
'The dotted path "%(path)s" is invalid for model "%(model)s".'
110+
) % {
111+
"path": self.odoo_field_value,
112+
"model": self.report_form_id.model_id.name,
113+
}
109114
return {
110115
"type": "ir.actions.client",
111116
"tag": "display_notification",
@@ -120,11 +125,20 @@ def action_validate_field(self):
120125
@api.constrains("odoo_field_evaluation", "odoo_field_value", "report_form_id")
121126
def _check_dotted_path(self):
122127
for record in self:
128+
# Skip validation if model is not available (during creation/updates)
129+
if (
130+
not record.report_form_id
131+
or not record.report_form_id.model_id
132+
or not record.report_form_id.model_id.model
133+
):
134+
continue # Skip validation when model is not yet available
123135
if not record._validate_dotted_path():
124-
message = self.env._(
125-
"The dotted path '{path}' is not valid for model '{model}'."
126-
).format(
127-
path=record.odoo_field_value,
128-
model=record.report_form_id.model_id.name,
136+
model_name = (
137+
record.report_form_id.model_id.name
138+
if record.report_form_id.model_id
139+
else "Unknown"
129140
)
141+
message = self.env._(
142+
"The dotted path '%(path)s' is not valid for model '%(model)s'."
143+
) % {"path": record.odoo_field_value, "model": model_name}
130144
raise ValidationError(message)

report_pdf_form/tests/test_report_pdf_form.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def setUpClass(cls):
4242
"name": "PDF Form Example",
4343
"report_id": cls.empty_report.id,
4444
"pdf_attachment_id": cls.pdf_attachment.id,
45+
"model_id": cls.env.ref("base.model_res_partner").id,
4546
"field_mapping_ids": [
4647
Command.create(
4748
{

report_pdf_form/views/report_pdf_form.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
type="object"
4040
icon="fa-check-circle"
4141
invisible="odoo_field_evaluation != 'dotted_path'"
42-
help="Validate field mapping"
42+
title="Validate field mapping"
4343
/>
4444
</list>
4545
</field>

0 commit comments

Comments
 (0)