[IMP] survey_record_generation : add param ignore_if_mandatory_field_is_missing
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m38s
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m38s
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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(
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<group>
|
||||
<field name="name" />
|
||||
<field name="model_id" />
|
||||
<field name="ignore_if_mandatory_field_is_missing" />
|
||||
<field name="update_existing_records" />
|
||||
<field name="allowed_field_ids" attrs="{'invisible': True}"/>
|
||||
<field name="field_to_retrieve_existing_records" attrs="{'invisible': [('update_existing_records', '=', False)]}"/>
|
||||
|
||||
Reference in New Issue
Block a user