[IMP] survey_record_generation : new option update_existing_fields
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
{
|
||||
"name": "Survey record generation",
|
||||
'summary': 'Allow to create record of any model when sending the form',
|
||||
'summary': 'Allow to create or update record of any model when sending the form',
|
||||
'description': """
|
||||
Allow to create record of any model when sending the form :
|
||||
----------------------------------------------------
|
||||
|
||||
@@ -27,6 +27,11 @@ class SurveyRecordCreation(models.Model):
|
||||
help="Choose the field you want to use to retrieve the existing record. "
|
||||
"WARNING: We update only the first record found.",
|
||||
)
|
||||
update_existing_fields = fields.Boolean(
|
||||
string="Update existing fields",
|
||||
help="The default behavior is to not update the existing fields. "
|
||||
"If checked, the existing fields will be updated. ",
|
||||
)
|
||||
allowed_field_ids = fields.Many2many(
|
||||
"ir.model.fields",
|
||||
compute="_compute_allowed_field_ids",
|
||||
|
||||
@@ -71,12 +71,15 @@ class SurveyUserInput(models.Model):
|
||||
if duplicate:
|
||||
record = duplicate
|
||||
elif existing_record:
|
||||
vals_with_keys_not_in_record = {
|
||||
k: v
|
||||
for k, v in vals.items()
|
||||
if not getattr(existing_record, k, False)
|
||||
}
|
||||
existing_record.write(vals_with_keys_not_in_record)
|
||||
if record_creation.update_existing_fields:
|
||||
existing_record.write(vals)
|
||||
else:
|
||||
vals_with_keys_not_in_record = {
|
||||
k: v
|
||||
for k, v in vals.items()
|
||||
if not getattr(existing_record, k, False)
|
||||
}
|
||||
existing_record.write(vals_with_keys_not_in_record)
|
||||
record = existing_record
|
||||
else:
|
||||
try:
|
||||
|
||||
@@ -722,9 +722,91 @@ class TestSurveyRecordCreation(SurveyCase):
|
||||
self.answer._mark_done()
|
||||
|
||||
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
self.assertTrue(len(partner) == 1)
|
||||
self.assertTrue(partner.email == "jean@test.fr")
|
||||
self.assertTrue(partner.function == "happiness office manager")
|
||||
self.assertEqual(len(partner), 1)
|
||||
self.assertEqual(partner.email, "jean@test.fr")
|
||||
self.assertEqual(partner.function, "happiness office manager")
|
||||
|
||||
def test_update_all_fields_when_updating_records(self):
|
||||
# A contact with name 'Jean' and email 'jean@test.fr' already exists.
|
||||
# We'll update the fields 'function' AND 'email' of this partner
|
||||
# because the option 'update_existing_fields' is True
|
||||
self.env["res.partner"].create(
|
||||
{
|
||||
"name": "Jean",
|
||||
"email": "jean@test.fr",
|
||||
}
|
||||
)
|
||||
self.question_email = self._add_question(
|
||||
page=None,
|
||||
name="Email",
|
||||
qtype="char_box",
|
||||
survey_id=self.survey.id,
|
||||
sequence=1,
|
||||
)
|
||||
self.question_function = self._add_question(
|
||||
page=None,
|
||||
name="Function",
|
||||
qtype="char_box",
|
||||
survey_id=self.survey.id,
|
||||
sequence=1,
|
||||
)
|
||||
|
||||
self.survey_record_creation.write(
|
||||
{
|
||||
"update_existing_records": True,
|
||||
"field_to_retrieve_existing_records": self.name_field.id,
|
||||
"update_existing_fields": True,
|
||||
}
|
||||
)
|
||||
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,
|
||||
}
|
||||
)
|
||||
function_field = self.env["ir.model.fields"].search(
|
||||
[("model", "=", "res.partner"), ("name", "=", "function")]
|
||||
)
|
||||
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": function_field.id,
|
||||
"value_origin": "question",
|
||||
"question_id": self.question_function.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._add_answer_line(
|
||||
question=self.question_email,
|
||||
answer=self.answer,
|
||||
answer_value="IAmTheNewEmailReplacingTheOldOne@test.fr",
|
||||
)
|
||||
self._add_answer_line(
|
||||
question=self.question_function,
|
||||
answer=self.answer,
|
||||
answer_value="happiness office manager",
|
||||
)
|
||||
self.answer._mark_done()
|
||||
|
||||
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
self.assertEqual(len(partner), 1)
|
||||
self.assertEqual(partner.email, "IAmTheNewEmailReplacingTheOldOne@test.fr")
|
||||
self.assertEqual(partner.function, "happiness office manager")
|
||||
|
||||
def test_unicity_check_has_priority_over_update(self):
|
||||
# In this test, we verify that if a field is set up with unicity_check
|
||||
|
||||
@@ -16,12 +16,19 @@
|
||||
</list>
|
||||
<form>
|
||||
<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" invisible="1"/>
|
||||
<field name="field_to_retrieve_existing_records" invisible="not update_existing_records"/>
|
||||
<group colspan="4">
|
||||
<field name="name" />
|
||||
<field name="model_id" />
|
||||
<field name="ignore_if_mandatory_field_is_missing" />
|
||||
<field name="allowed_field_ids" invisible="1"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="update_existing_records" />
|
||||
</group>
|
||||
<group invisible="not update_existing_records">
|
||||
<field name="field_to_retrieve_existing_records"/>
|
||||
<field name="update_existing_fields"/>
|
||||
</group>
|
||||
<div colspan="2" style="width:100%;">
|
||||
<div class="alert alert-warning"
|
||||
invisible="not update_existing_records">
|
||||
|
||||
Reference in New Issue
Block a user