[IMP] survey_record_generation : new option update_existing_fields
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
{
|
{
|
||||||
"name": "Survey record generation",
|
"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': """
|
'description': """
|
||||||
Allow to create record of any model when sending the form :
|
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. "
|
help="Choose the field you want to use to retrieve the existing record. "
|
||||||
"WARNING: We update only the first record found.",
|
"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(
|
allowed_field_ids = fields.Many2many(
|
||||||
"ir.model.fields",
|
"ir.model.fields",
|
||||||
compute="_compute_allowed_field_ids",
|
compute="_compute_allowed_field_ids",
|
||||||
|
|||||||
@@ -71,12 +71,15 @@ class SurveyUserInput(models.Model):
|
|||||||
if duplicate:
|
if duplicate:
|
||||||
record = duplicate
|
record = duplicate
|
||||||
elif existing_record:
|
elif existing_record:
|
||||||
vals_with_keys_not_in_record = {
|
if record_creation.update_existing_fields:
|
||||||
k: v
|
existing_record.write(vals)
|
||||||
for k, v in vals.items()
|
else:
|
||||||
if not getattr(existing_record, k, False)
|
vals_with_keys_not_in_record = {
|
||||||
}
|
k: v
|
||||||
existing_record.write(vals_with_keys_not_in_record)
|
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
|
record = existing_record
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -722,9 +722,91 @@ class TestSurveyRecordCreation(SurveyCase):
|
|||||||
self.answer._mark_done()
|
self.answer._mark_done()
|
||||||
|
|
||||||
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
self.assertTrue(len(partner) == 1)
|
self.assertEqual(len(partner), 1)
|
||||||
self.assertTrue(partner.email == "jean@test.fr")
|
self.assertEqual(partner.email, "jean@test.fr")
|
||||||
self.assertTrue(partner.function == "happiness office manager")
|
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):
|
def test_unicity_check_has_priority_over_update(self):
|
||||||
# In this test, we verify that if a field is set up with unicity_check
|
# In this test, we verify that if a field is set up with unicity_check
|
||||||
|
|||||||
@@ -16,12 +16,19 @@
|
|||||||
</list>
|
</list>
|
||||||
<form>
|
<form>
|
||||||
<group>
|
<group>
|
||||||
<field name="name" />
|
<group colspan="4">
|
||||||
<field name="model_id" />
|
<field name="name" />
|
||||||
<field name="ignore_if_mandatory_field_is_missing" />
|
<field name="model_id" />
|
||||||
<field name="update_existing_records" />
|
<field name="ignore_if_mandatory_field_is_missing" />
|
||||||
<field name="allowed_field_ids" invisible="1"/>
|
<field name="allowed_field_ids" invisible="1"/>
|
||||||
<field name="field_to_retrieve_existing_records" invisible="not update_existing_records"/>
|
</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 colspan="2" style="width:100%;">
|
||||||
<div class="alert alert-warning"
|
<div class="alert alert-warning"
|
||||||
invisible="not update_existing_records">
|
invisible="not update_existing_records">
|
||||||
|
|||||||
Reference in New Issue
Block a user