Compare commits
2 Commits
cd6261b34a
...
f40a2f8451
| Author | SHA1 | Date | |
|---|---|---|---|
| f40a2f8451 | |||
| 500f14a57a |
@@ -11,7 +11,7 @@ Allow to create record of any model when sending the form :
|
|||||||
* Associate question with fields
|
* Associate question with fields
|
||||||
* For x2m fields : Associate values to questions
|
* For x2m fields : Associate values to questions
|
||||||
""",
|
""",
|
||||||
"version": "18.0.1.0.1",
|
"version": "18.0.1.0.2",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"author": "Elabore",
|
"author": "Elabore",
|
||||||
"website": "https://www.elabore.coop",
|
"website": "https://www.elabore.coop",
|
||||||
|
|||||||
@@ -61,6 +61,11 @@ class SurveyUserInput(models.Model):
|
|||||||
)
|
)
|
||||||
field_name: str = field_value.field_id.name
|
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
|
vals[field_name] = value
|
||||||
|
|
||||||
existing_record = self.find_existing_record(record_creation, vals)
|
existing_record = self.find_existing_record(record_creation, vals)
|
||||||
@@ -160,8 +165,9 @@ class SurveyUserInput(models.Model):
|
|||||||
elif field_value.value_origin == "question":
|
elif field_value.value_origin == "question":
|
||||||
value = self.get_value_from_user_answer(field_value, user_input)
|
value = self.get_value_from_user_answer(field_value, user_input)
|
||||||
elif field_value.value_origin == "other_record":
|
elif field_value.value_origin == "other_record":
|
||||||
# if the other_record value is a required field, get it or raise
|
# if the other record is already created, get it
|
||||||
value = self.get_required_value_from_other_record(
|
# (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
|
model, created_records, field_value
|
||||||
)
|
)
|
||||||
# otherwise, we update the record later (out of this for loop)
|
# otherwise, we update the record later (out of this for loop)
|
||||||
@@ -194,39 +200,34 @@ class SurveyUserInput(models.Model):
|
|||||||
if unique_fields:
|
if unique_fields:
|
||||||
uniq_domain = []
|
uniq_domain = []
|
||||||
for uniq_field in unique_fields:
|
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)
|
duplicate = self.env[model].search(uniq_domain, limit=1)
|
||||||
return duplicate
|
return duplicate
|
||||||
|
|
||||||
def get_required_value_from_other_record(
|
def get_value_from_other_record(
|
||||||
self,
|
self,
|
||||||
model: str,
|
model: str,
|
||||||
created_records: dict[Any, Any],
|
created_records: dict[Any, Any],
|
||||||
field_value: "SurveyRecordCreationFieldValues",
|
field_value: "SurveyRecordCreationFieldValues",
|
||||||
) -> Any:
|
) -> Any:
|
||||||
model_class = self.env[model]
|
linked_record = created_records.get(field_value.other_created_record_id.id)
|
||||||
if model_class._fields[field_value.field_id.name].required:
|
if linked_record:
|
||||||
# check if the other record is already created,
|
# The other record is already created: link it right away.
|
||||||
# if yes add it to vals, else raise
|
return self.get_value_for_relational_field(field_value, linked_record)
|
||||||
if (
|
if self.env[model]._fields[field_value.field_id.name].required:
|
||||||
len(created_records) > 0
|
raise UserError(
|
||||||
and created_records[field_value.other_created_record_id.id]
|
_(
|
||||||
):
|
"The field %(field)s is mandatory for model %(model)s. "
|
||||||
linked_record = created_records[field_value.other_created_record_id.id]
|
"In Record Creation tab, drag %(record)s "
|
||||||
return self.get_value_for_relational_field(field_value, linked_record)
|
"on top of the model %(model)s."
|
||||||
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,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
% {
|
||||||
|
"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(
|
def get_value_from_user_answer(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from psycopg2 import IntegrityError
|
from psycopg2 import IntegrityError
|
||||||
|
|
||||||
from odoo.addons.survey.tests.common import SurveyCase
|
from odoo.addons.survey.tests.common import SurveyCase
|
||||||
@@ -1072,3 +1074,143 @@ class TestSurveyRecordCreation(SurveyCase):
|
|||||||
self.assertEqual(self.answer.partner_id, jean)
|
self.assertEqual(self.answer.partner_id, jean)
|
||||||
self.assertNotEqual(self.answer.partner_id, jeanne)
|
self.assertNotEqual(self.answer.partner_id, jeanne)
|
||||||
self.assertEqual(self.answer.email, jean.email)
|
self.assertEqual(self.answer.email, jean.email)
|
||||||
|
|
||||||
|
def test_link_to_already_created_record_is_set_at_creation(self):
|
||||||
|
# When a NOT required "other_record" field points to a record that
|
||||||
|
# is already created (lower sequence), the link must be given in
|
||||||
|
# create() and not written afterwards: a later write would trigger
|
||||||
|
# computed fields depending on this link (e.g. price_unit of a
|
||||||
|
# sale.subscription.line depends on its subscription) and override
|
||||||
|
# the values answered by the user.
|
||||||
|
self.survey_record_creation.sequence = 0
|
||||||
|
child_creation = self.env["survey.record.creation"].create(
|
||||||
|
{
|
||||||
|
"name": "Child contact",
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"sequence": 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": child_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"field_id": self.name_field.id,
|
||||||
|
"value_origin": "fixed",
|
||||||
|
"fixed_value_char": "Child",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
parent_field = self.env["ir.model.fields"]._get("res.partner", "parent_id")
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": child_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"field_id": parent_field.id,
|
||||||
|
"value_origin": "other_record",
|
||||||
|
"other_created_record_id": self.survey_record_creation.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"
|
||||||
|
)
|
||||||
|
|
||||||
|
partner_class = type(self.env["res.partner"])
|
||||||
|
original_write = partner_class.write
|
||||||
|
written_fields = []
|
||||||
|
|
||||||
|
def spy_write(records, vals):
|
||||||
|
written_fields.extend(vals)
|
||||||
|
return original_write(records, vals)
|
||||||
|
|
||||||
|
with patch.object(partner_class, "write", spy_write):
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
jean = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
child = self.env["res.partner"].search([("name", "=", "Child")])
|
||||||
|
self.assertEqual(child.parent_id, jean)
|
||||||
|
self.assertNotIn("parent_id", written_fields)
|
||||||
|
|
||||||
|
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user