From 0ef8bea2cf8d5e94c5fe252307ffe3e95dd0d690 Mon Sep 17 00:00:00 2001 From: Quentin Mondot Date: Wed, 21 Oct 2026 12:09:36 +0200 Subject: [PATCH 1/2] [FIX] survey_record_generation: link to already created record at creation A NOT required "other_record" field was always set after the loop with a write(), even when the linked record was already created. This late write triggers the recomputation of stored computed fields depending on the relation, overriding the values answered by the user. E.g. with subscription_oca, sale.subscription.line price_unit depends on sale_subscription_id.partner_id/pricelist_id: a free amount typed in a numerical_box question (33) was replaced by the product price (20) when the line was linked to its subscription. Now the relation is given to create() when the linked record already exists, so that the user's values are protected from recomputation. The deferred write is kept for records created later. Assisted-By: Claude Opus 5.5 --- survey_record_generation/__manifest__.py | 2 +- .../models/survey_user_input.py | 46 ++++--- survey_record_generation/tests/__init__.py | 1 + survey_record_generation/tests/fake_models.py | 33 ++++++ .../tests/test_computed_fields.py | 112 ++++++++++++++++++ 5 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 survey_record_generation/tests/fake_models.py create mode 100644 survey_record_generation/tests/test_computed_fields.py diff --git a/survey_record_generation/__manifest__.py b/survey_record_generation/__manifest__.py index 32bee37..af08fc3 100644 --- a/survey_record_generation/__manifest__.py +++ b/survey_record_generation/__manifest__.py @@ -11,7 +11,7 @@ Allow to create record of any model when sending the form : * Associate question with fields * For x2m fields : Associate values to questions """, - "version": "16.0.1.0.3", + "version": "16.0.1.0.4", "license": "AGPL-3", "author": "Elabore", "website": "https://www.elabore.coop", diff --git a/survey_record_generation/models/survey_user_input.py b/survey_record_generation/models/survey_user_input.py index 9dff3f4..8cccae0 100644 --- a/survey_record_generation/models/survey_user_input.py +++ b/survey_record_generation/models/survey_user_input.py @@ -160,8 +160,9 @@ class SurveyUserInput(models.Model): elif field_value.value_origin == "question": value = self.get_value_from_user_answer(field_value, user_input) elif field_value.value_origin == "other_record": - # if the other_record value is a required field, get it or raise - value = self.get_required_value_from_other_record( + # if the other record is already created, get it + # (or raise if it is not created yet and the field is required) + value = self.get_value_from_other_record( model, created_records, field_value ) # otherwise, we update the record later (out of this for loop) @@ -198,35 +199,30 @@ class SurveyUserInput(models.Model): duplicate = self.env[model].search(uniq_domain, limit=1) return duplicate - def get_required_value_from_other_record( + def get_value_from_other_record( self, model: str, created_records: dict[Any, Any], field_value: "SurveyRecordCreationFieldValues", ) -> Any: - model_class = self.env[model] - if model_class._fields[field_value.field_id.name].required: - # check if the other record is already created, - # if yes add it to vals, else raise - if ( - len(created_records) > 0 - and created_records[field_value.other_created_record_id.id] - ): - linked_record = created_records[field_value.other_created_record_id.id] - return self.get_value_for_relational_field(field_value, linked_record) - else: - raise UserError( - _( - "The field %(field)s is mandatory for model %(model)s. " - "In Record Creation tab, drag %(record)s " - "on top of the model %(model)s." - ) - % { - "field": field_value.field_id.display_name, - "model": model, - "record": field_value.other_created_record_id.name, - } + linked_record = created_records.get(field_value.other_created_record_id.id) + if linked_record: + # The other record is already created: link it right away. + return self.get_value_for_relational_field(field_value, linked_record) + if self.env[model]._fields[field_value.field_id.name].required: + raise UserError( + _( + "The field %(field)s is mandatory for model %(model)s. " + "In Record Creation tab, drag %(record)s " + "on top of the model %(model)s." ) + % { + "field": field_value.field_id.display_name, + "model": model, + "record": field_value.other_created_record_id.name, + } + ) + return None def get_value_from_user_answer( self, diff --git a/survey_record_generation/tests/__init__.py b/survey_record_generation/tests/__init__.py index 7982aab..0a3fd85 100644 --- a/survey_record_generation/tests/__init__.py +++ b/survey_record_generation/tests/__init__.py @@ -1,2 +1,3 @@ from . import test_survey_record_creation from . import test_end_migration_16_0_1_0_3 +from . import test_computed_fields diff --git a/survey_record_generation/tests/fake_models.py b/survey_record_generation/tests/fake_models.py new file mode 100644 index 0000000..df6cb99 --- /dev/null +++ b/survey_record_generation/tests/fake_models.py @@ -0,0 +1,33 @@ +from odoo import api, fields, models + + +class SurveyRecordGenerationFakeParent(models.Model): + _name = "survey.record.generation.fake.parent" + _description = "Fake parent record (tests only)" + + name = fields.Char() + price = fields.Float() + + +class SurveyRecordGenerationFakeLine(models.Model): + """Line whose price is computed from its parent, like the price of a + subscription line (subscription_oca). + + Pattern reproduced: the survey creates a parent, then a line linked to + it, with a price answered by the user. The user's price must be kept, + the parent's price only applies when the user gave none. + """ + + _name = "survey.record.generation.fake.line" + _description = "Fake line record (tests only)" + + parent_id = fields.Many2one("survey.record.generation.fake.parent") + price_unit = fields.Float( + compute="_compute_price_unit", store=True, readonly=False + ) + + @api.depends("parent_id.price") + def _compute_price_unit(self): + for line in self: + if line.parent_id: + line.price_unit = line.parent_id.price diff --git a/survey_record_generation/tests/test_computed_fields.py b/survey_record_generation/tests/test_computed_fields.py new file mode 100644 index 0000000..79f14ce --- /dev/null +++ b/survey_record_generation/tests/test_computed_fields.py @@ -0,0 +1,112 @@ +from odoo_test_helper import FakeModelLoader + +from odoo.addons.survey.tests.common import SurveyCase + + +class TestComputedFields(SurveyCase): + """User's answers must not be overridden by computed fields depending on + a link to another generated record. + + Use case: a membership form creating a subscription and a subscription + line whose price is typed by the participant (free amount), while the + line price is computed from the subscription. + """ + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + from .fake_models import ( + SurveyRecordGenerationFakeLine, + SurveyRecordGenerationFakeParent, + ) + + cls.loader.update_registry( + (SurveyRecordGenerationFakeParent, SurveyRecordGenerationFakeLine) + ) + + @classmethod + def tearDownClass(cls): + cls.loader.restore_registry() + super().tearDownClass() + + def setUp(self): + super().setUp() + self.survey = self.env["survey.survey"].create({"title": "Join us"}) + self.question_amount = self._add_question( + page=None, + name="Free amount", + qtype="numerical_box", + survey_id=self.survey.id, + sequence=1, + ) + + # Parent, with a default price of 20 + self.parent_creation = self._add_record_creation( + "Parent", "survey.record.generation.fake.parent", 0 + ) + self._add_field_value( + self.parent_creation, "name", value_origin="fixed", + fixed_value_char="Membership", + ) + self._add_field_value( + self.parent_creation, "price", value_origin="fixed", + fixed_value_float=20.0, + ) + + # Line, linked to the parent, with the price answered by the user + self.line_creation = self._add_record_creation( + "Line", "survey.record.generation.fake.line", 1 + ) + self._add_field_value( + self.line_creation, "parent_id", value_origin="other_record", + other_created_record_id=self.parent_creation.id, + ) + self._add_field_value( + self.line_creation, "price_unit", value_origin="question", + question_id=self.question_amount.id, + ) + + def _add_record_creation(self, name, model, sequence): + return self.env["survey.record.creation"].create( + { + "name": name, + "survey_id": self.survey.id, + "model_id": self.env["ir.model"]._get(model).id, + "sequence": sequence, + } + ) + + def _add_field_value(self, record_creation, field_name, **vals): + field = self.env["ir.model.fields"]._get( + record_creation.model_id.model, field_name + ) + return self.env["survey.record.creation.field.values"].create( + { + "survey_record_creation_id": record_creation.id, + "survey_id": self.survey.id, + "model_id": record_creation.model_id.id, + "field_id": field.id, + **vals, + } + ) + + def _submit(self, amount=None): + answer = self._add_answer( + survey=self.survey, partner=False, email="jean@test.fr" + ) + if amount is not None: + self._add_answer_line( + question=self.question_amount, answer=answer, answer_value=amount + ) + answer._mark_done() + return self.env["survey.record.generation.fake.line"].search( + [("parent_id.name", "=", "Membership")] + ) + + def test_answer_is_not_overridden_by_computed_field(self): + line = self._submit(amount=33) + + self.assertEqual(len(line), 1) + self.assertEqual(line.price_unit, 33.0) -- 2.49.1 From 0a7f7213f4ab1e6399e78f61d427faea173b068b Mon Sep 17 00:00:00 2001 From: Quentin Mondot Date: Wed, 21 Oct 2026 12:22:01 +0200 Subject: [PATCH 2/2] [FIX] survey_record_generation: questions without answer no longer empty fields When a question was not displayed (conditional question) or skipped, its field was still given to create() with a None value. The field was then emptied instead of keeping its default or computed value. Since the link to an already created record is given at creation, this also made sale.subscription.line price_unit equal to 0 when the "free amount" question was not displayed, instead of the product price. Fields without value are now left out of the values, which also allows several conditional questions to fill up the same field (e.g. one list of amounts per periodicity). Assisted-By: Claude Opus 5.5 --- .../models/survey_user_input.py | 7 +- .../tests/test_computed_fields.py | 8 ++ .../tests/test_survey_record_creation.py | 79 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/survey_record_generation/models/survey_user_input.py b/survey_record_generation/models/survey_user_input.py index 8cccae0..68b2192 100644 --- a/survey_record_generation/models/survey_user_input.py +++ b/survey_record_generation/models/survey_user_input.py @@ -61,6 +61,11 @@ class SurveyUserInput(models.Model): ) field_name: str = field_value.field_id.name + if value is None: + # No value (e.g. question not displayed or skipped): + # keep the default/computed value of the field, and + # let another question fill up the same field. + continue vals[field_name] = value existing_record = self.find_existing_record(record_creation, vals) @@ -195,7 +200,7 @@ class SurveyUserInput(models.Model): if unique_fields: uniq_domain = [] for uniq_field in unique_fields: - uniq_domain.append((uniq_field, "=", vals[uniq_field])) + uniq_domain.append((uniq_field, "=", vals.get(uniq_field, False))) duplicate = self.env[model].search(uniq_domain, limit=1) return duplicate diff --git a/survey_record_generation/tests/test_computed_fields.py b/survey_record_generation/tests/test_computed_fields.py index 79f14ce..4ae40b2 100644 --- a/survey_record_generation/tests/test_computed_fields.py +++ b/survey_record_generation/tests/test_computed_fields.py @@ -110,3 +110,11 @@ class TestComputedFields(SurveyCase): self.assertEqual(len(line), 1) self.assertEqual(line.price_unit, 33.0) + + def test_computed_field_applies_when_question_is_not_displayed(self): + # E.g. the "free amount" question is only displayed when the user + # chose "Other amount": otherwise the line keeps the computed price. + line = self._submit() + + self.assertEqual(len(line), 1) + self.assertEqual(line.price_unit, 20.0) diff --git a/survey_record_generation/tests/test_survey_record_creation.py b/survey_record_generation/tests/test_survey_record_creation.py index bcb304e..fa25eed 100644 --- a/survey_record_generation/tests/test_survey_record_creation.py +++ b/survey_record_generation/tests/test_survey_record_creation.py @@ -1096,3 +1096,82 @@ class TestSurveyRecordCreation(SurveyCase): self.assertEqual(self.answer.partner_id, jean) self.assertNotEqual(self.answer.partner_id, jeanne) self.assertEqual(self.answer.email, jean.email) + + def test_question_not_displayed_does_not_override_default_value(self): + # When a question is not displayed to the user (e.g. a conditional + # question), the field must keep its default (or computed) value + # instead of being emptied. + question_type = self._add_question( + page=None, + name="Type", + qtype="simple_choice", + labels=[{"value": "contact"}, {"value": "other"}], + survey_id=self.survey.id, + sequence=2, + answer_values_type="value", + ) + for suggested_answer in question_type.suggested_answer_ids: + suggested_answer.value_char = suggested_answer.value + type_field = self.env["ir.model.fields"]._get("res.partner", "type") + self.env["survey.record.creation.field.values"].create( + { + "survey_record_creation_id": self.survey_record_creation.id, + "survey_id": self.survey.id, + "model_id": self.res_partner_model.id, + "field_id": type_field.id, + "value_origin": "question", + "question_id": question_type.id, + } + ) + + self.answer = self._add_answer( + survey=self.survey, partner=False, email="jean@test.fr" + ) + self._add_answer_line( + question=self.question_name, answer=self.answer, answer_value="Jean" + ) + self.answer._mark_done() + + partner = self.env["res.partner"].search([("name", "=", "Jean")]) + self.assertEqual(partner.type, "contact") + + def test_several_conditional_questions_on_the_same_field(self): + # Several questions can fill up the same field (e.g. one list of + # amounts per periodicity, displayed conditionally): the answered + # question wins, the questions not displayed are ignored. + street_field = self.env["ir.model.fields"]._get("res.partner", "street") + questions = self.env["survey.question"] + for sequence, name in enumerate(["Street (France)", "Street (abroad)"], 2): + question = self._add_question( + page=None, + name=name, + qtype="char_box", + survey_id=self.survey.id, + sequence=sequence, + ) + self.env["survey.record.creation.field.values"].create( + { + "survey_record_creation_id": self.survey_record_creation.id, + "survey_id": self.survey.id, + "model_id": self.res_partner_model.id, + "field_id": street_field.id, + "value_origin": "question", + "question_id": question.id, + } + ) + questions |= question + + self.answer = self._add_answer( + survey=self.survey, partner=False, email="jean@test.fr" + ) + self._add_answer_line( + question=self.question_name, answer=self.answer, answer_value="Jean" + ) + # Only the first question is displayed and answered + self._add_answer_line( + question=questions[0], answer=self.answer, answer_value="1 rue de Paris" + ) + self.answer._mark_done() + + partner = self.env["res.partner"].search([("name", "=", "Jean")]) + self.assertEqual(partner.street, "1 rue de Paris") -- 2.49.1