From 7183ff6f54f8cbc2a752de6cd465b9164b311aa3 Mon Sep 17 00:00:00 2001 From: Quentin Mondot Date: Thu, 13 Nov 2025 17:44:44 +0100 Subject: [PATCH] [IMP] survey_record_generation : add param ignore_if_mandatory_field_is_missing --- survey_record_generation/i18n/fr.po | 25 +++- .../models/survey_record_creation.py | 6 + .../models/survey_user_input.py | 18 ++- .../tests/test_survey_record_creation.py | 132 ++++++++---------- .../views/survey_survey_views.xml | 1 + 5 files changed, 97 insertions(+), 85 deletions(-) diff --git a/survey_record_generation/i18n/fr.po b/survey_record_generation/i18n/fr.po index 4a9e9fd..574b4e5 100644 --- a/survey_record_generation/i18n/fr.po +++ b/survey_record_generation/i18n/fr.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2025-11-03 16:35+0000\n" -"PO-Revision-Date: 2025-11-03 16:35+0000\n" +"POT-Creation-Date: 2025-11-13 16:41+0000\n" +"PO-Revision-Date: 2025-11-13 16:41+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -190,6 +190,22 @@ msgstr "Aide" msgid "ID" msgstr "" +#. module: survey_record_generation +#: model:ir.model.fields,help:survey_record_generation.field_survey_record_creation__ignore_if_mandatory_field_is_missing +msgid "" +"If a mandatory field is missing when trying to create the record, an error " +"is raised when the survey is submitted. If this option is activated, the " +"error is ignored." +msgstr "" +"Si un champs requis est manquant lors de la création de l'enregistrement, " +"une erreur est levée lors de la soumission du formulaire. " +"En activant cette option, l'erreur sera ignorée." + +#. module: survey_record_generation +#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__ignore_if_mandatory_field_is_missing +msgid "Ignore creation if a mandatory field is missing" +msgstr "Ignorer la création si un champs requis est manquant" + #. module: survey_record_generation #: model:ir.model.fields,field_description:survey_record_generation.field_survey_generated_record____last_update #: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation____last_update @@ -278,8 +294,9 @@ msgid "" "Only the first matched record will be updated.\n" " Also to be noticed, the unicity check feature has priority over updating the existing record." msgstr "" -"Attention, seul le premier enregistrement trouvé sera mis à jour. " -"Aussi, si vous avez des champs avec une contrainte d'unicité, cette contrainte aura la priorité sur la mise à jour des enregistrements." +"Attention, seul le premier enregistrement trouvé sera mis à jour. Aussi, si " +"vous avez des champs avec une contrainte d'unicité, cette contrainte aura la" +" priorité sur la mise à jour des enregistrements." #. module: survey_record_generation #. odoo-python diff --git a/survey_record_generation/models/survey_record_creation.py b/survey_record_generation/models/survey_record_creation.py index f3377c4..95d553f 100644 --- a/survey_record_generation/models/survey_record_creation.py +++ b/survey_record_generation/models/survey_record_creation.py @@ -33,6 +33,12 @@ class SurveyRecordCreation(models.Model): store=True, string="Allowed Fields", ) + ignore_if_mandatory_field_is_missing = fields.Boolean( + string="Ignore creation if a mandatory field is missing", + help="If a mandatory field is missing when trying to create the record, " + "an error is raised when the survey is submitted. " + "If this option is activated, the error is ignored." + ) @api.depends("field_values_ids.field_id") def _compute_allowed_field_ids(self): diff --git a/survey_record_generation/models/survey_user_input.py b/survey_record_generation/models/survey_user_input.py index b53ab18..1a50670 100644 --- a/survey_record_generation/models/survey_user_input.py +++ b/survey_record_generation/models/survey_user_input.py @@ -81,17 +81,15 @@ class SurveyUserInput(models.Model): existing_record.write(vals_with_keys_not_in_record) record = existing_record else: - if ( - model == "res.partner" - and ignore_when_res_partner_mandatory_fields_are_missing - ): - # this part has been developed for Calim specific needs : - # being able to ignore some Contacts creation - # TODO : find a way to make it generic for all models - if not vals.get("lastname") and not vals.get("firstname"): + try: + with self.env.cr.savepoint(): + record = self.env[model].create(vals) + except Exception: + # This a broad exception because it could be IntegrityError, + # EmptyNamesError in case partner_firstname is installed etc... + if record_creation.ignore_if_mandatory_field_is_missing: continue - # Create record - record = self.env[model].create(vals) + raise # Link generated records to user input self.env["survey.generated.record"].create( { diff --git a/survey_record_generation/tests/test_survey_record_creation.py b/survey_record_generation/tests/test_survey_record_creation.py index 81e0ac1..69245c6 100644 --- a/survey_record_generation/tests/test_survey_record_creation.py +++ b/survey_record_generation/tests/test_survey_record_creation.py @@ -150,12 +150,12 @@ class TestSurveyRecordCreation(SurveyCase): answer_values_type="value", ) - self.question_type.suggested_answer_ids[ - 0 - ].value_char = self.question_type.suggested_answer_ids[0].value - self.question_type.suggested_answer_ids[ - 1 - ].value_char = self.question_type.suggested_answer_ids[1].value + self.question_type.suggested_answer_ids[0].value_char = ( + self.question_type.suggested_answer_ids[0].value + ) + self.question_type.suggested_answer_ids[1].value_char = ( + self.question_type.suggested_answer_ids[1].value + ) type_field = self.env["ir.model.fields"].search( [("model", "=", "res.partner"), ("name", "=", "type")] @@ -195,12 +195,12 @@ class TestSurveyRecordCreation(SurveyCase): answer_values_type="record", ) - self.question_title.suggested_answer_ids[ - 0 - ].record_id = f"res.partner.title,{mister_title.id}" - self.question_title.suggested_answer_ids[ - 1 - ].record_id = f"res.partner.title,{madam_title.id}" + self.question_title.suggested_answer_ids[0].record_id = ( + f"res.partner.title,{mister_title.id}" + ) + self.question_title.suggested_answer_ids[1].record_id = ( + f"res.partner.title,{madam_title.id}" + ) title_field = self.env["ir.model.fields"].search( [("model", "=", "res.partner"), ("name", "=", "title")] @@ -243,15 +243,15 @@ class TestSurveyRecordCreation(SurveyCase): answer_values_type="record", ) - self.question_category.suggested_answer_ids[ - 0 - ].record_id = f"res.partner.category,{adult_category.id}" - self.question_category.suggested_answer_ids[ - 1 - ].record_id = f"res.partner.category,{teenager_category.id}" - self.question_category.suggested_answer_ids[ - 2 - ].record_id = f"res.partner.category,{child_category.id}" + self.question_category.suggested_answer_ids[0].record_id = ( + f"res.partner.category,{adult_category.id}" + ) + self.question_category.suggested_answer_ids[1].record_id = ( + f"res.partner.category,{teenager_category.id}" + ) + self.question_category.suggested_answer_ids[2].record_id = ( + f"res.partner.category,{child_category.id}" + ) category_field = self.env["ir.model.fields"].search( [("model", "=", "res.partner"), ("name", "=", "category_id")] @@ -411,44 +411,6 @@ class TestSurveyRecordCreation(SurveyCase): partner = self.env["res.partner"].search([("name", "=", "Jeanne")]) self.assertTrue(partner.name == "Jeanne") - def test_required_fields_are_not_filled_up(self): - # In this test, we check the behavior when a required field is missing - self.question_email = self._add_question( - page=None, - name="Email", - qtype="char_box", - survey_id=self.survey.id, - sequence=1, - ) - - email_field = self.env["ir.model.fields"].search( - [("model", "=", "res.partner"), ("name", "=", "email")] - ) - 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": email_field.id, - "value_origin": "question", - "question_id": self.question_email.id, - } - ) - - self.answer = self._add_answer( - survey=self.survey, partner=False, email="jean@test.fr" - ) - self._add_answer_line( - question=self.question_email, - answer=self.answer, - answer_value="jean@test.fr", - ) - - with self.assertRaises(IntegrityError): - # TODO : propose a better user experience than IntegrityError when - # a mandatory field is missing - self.answer._mark_done() - def test_survey_submitted_twice_by_same_user(self): self.answer = self._add_answer( survey=self.survey, partner=False, email="jean@test.fr" @@ -521,12 +483,12 @@ class TestSurveyRecordCreation(SurveyCase): sequence=1, answer_values_type="record", ) - self.question_title.suggested_answer_ids[ - 0 - ].record_id = f"res.partner.title,{mister_title.id}" - self.question_title.suggested_answer_ids[ - 1 - ].record_id = f"res.partner.title,{madam_title.id}" + self.question_title.suggested_answer_ids[0].record_id = ( + f"res.partner.title,{mister_title.id}" + ) + self.question_title.suggested_answer_ids[1].record_id = ( + f"res.partner.title,{madam_title.id}" + ) self.question_street = self._add_question( page=None, @@ -618,12 +580,12 @@ class TestSurveyRecordCreation(SurveyCase): answer_values_type="value", ) - self.question_employee.suggested_answer_ids[ - 0 - ].value_char = self.question_employee.suggested_answer_ids[0].value - self.question_employee.suggested_answer_ids[ - 1 - ].value_char = self.question_employee.suggested_answer_ids[1].value + self.question_employee.suggested_answer_ids[0].value_char = ( + self.question_employee.suggested_answer_ids[0].value + ) + self.question_employee.suggested_answer_ids[1].value_char = ( + self.question_employee.suggested_answer_ids[1].value + ) employee_field = self.env["ir.model.fields"].search( [("model", "=", "res.partner"), ("name", "=", "employee")] @@ -838,3 +800,31 @@ class TestSurveyRecordCreation(SurveyCase): partner = self.env["res.partner"].search([("name", "=", "Jean")]) self.assertTrue(len(partner) == 1) self.assertTrue(getattr(partner, "Email", None) is None) + + def test_required_fields_are_not_filled_up(self): + # In this test, we check the behavior when a required field (name) is missing + self.answer = self._add_answer( + survey=self.survey, partner=False, email="jean@test.fr" + ) + + with self.assertRaises(IntegrityError): + # TODO : propose a better user experience than IntegrityError when + # a mandatory field is missing + self.answer._mark_done() + + def test_ignore_if_mandatory_field_is_missing(self): + # In this test, we check the behavior of ignore_if_mandatory_field_is_missing + + self.survey_record_creation.write( + {"ignore_if_mandatory_field_is_missing": True} + ) + + self.answer = self._add_answer( + survey=self.survey, partner=False, email="jean@test.fr" + ) + + self.answer._mark_done() + + # No partner has been created, and no IntegrityError has been raised + partner = self.env["res.partner"].search([("name", "=", "Jean")]) + self.assertEqual(len(partner), 0) diff --git a/survey_record_generation/views/survey_survey_views.xml b/survey_record_generation/views/survey_survey_views.xml index fcb93dd..8507d76 100644 --- a/survey_record_generation/views/survey_survey_views.xml +++ b/survey_record_generation/views/survey_survey_views.xml @@ -18,6 +18,7 @@ +