Compare commits
3 Commits
23fbbddbb2
...
16.0
| Author | SHA1 | Date | |
|---|---|---|---|
| f709adbcec | |||
| fd25c2b970 | |||
| 7d638c4e92 |
@@ -6,8 +6,8 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: Odoo Server 16.0\n"
|
"Project-Id-Version: Odoo Server 16.0\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2025-04-15 10:34+0000\n"
|
"POT-Creation-Date: 2025-11-03 16:35+0000\n"
|
||||||
"PO-Revision-Date: 2025-04-15 10:34+0000\n"
|
"PO-Revision-Date: 2025-11-03 16:35+0000\n"
|
||||||
"Last-Translator: \n"
|
"Last-Translator: \n"
|
||||||
"Language-Team: \n"
|
"Language-Team: \n"
|
||||||
"MIME-Version: 1.0\n"
|
"MIME-Version: 1.0\n"
|
||||||
@@ -31,6 +31,11 @@ msgstr ""
|
|||||||
msgid "Active survey input"
|
msgid "Active survey input"
|
||||||
msgstr "Participation active"
|
msgstr "Participation active"
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__allowed_field_ids
|
||||||
|
msgid "Allowed Fields"
|
||||||
|
msgstr "Champs acceptés"
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__allowed_question_ids
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__allowed_question_ids
|
||||||
msgid "Allowed Question"
|
msgid "Allowed Question"
|
||||||
@@ -55,6 +60,16 @@ msgstr "Associer une valeur à la réponse"
|
|||||||
msgid "Attempts Count"
|
msgid "Attempts Count"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model:ir.model.fields,help:survey_record_generation.field_survey_record_creation__field_to_retrieve_existing_records
|
||||||
|
msgid ""
|
||||||
|
"Choose the field you want to use to retrieve the existing record. WARNING: "
|
||||||
|
"We update only the first record found."
|
||||||
|
msgstr ""
|
||||||
|
"Choisissez le champs à partir duquel nous allons chercher l'enregistrement "
|
||||||
|
"existant. Attention : nous mettons à jour seulement le premier "
|
||||||
|
"enregistrement trouvé."
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_generated_record__create_uid
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_generated_record__create_uid
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__create_uid
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__create_uid
|
||||||
@@ -102,6 +117,11 @@ msgstr ""
|
|||||||
msgid "Field"
|
msgid "Field"
|
||||||
msgstr "Champ"
|
msgstr "Champ"
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__field_to_retrieve_existing_records
|
||||||
|
msgid "Field To Retrieve Existing Records"
|
||||||
|
msgstr "Champs pour retrouver l'enregistrement existant"
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__field_type
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__field_type
|
||||||
msgid "Field Type"
|
msgid "Field Type"
|
||||||
@@ -170,11 +190,6 @@ msgstr "Aide"
|
|||||||
msgid "ID"
|
msgid "ID"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#. module: survey_record_generation
|
|
||||||
#: model:ir.model,name:survey_record_generation.model_survey_question
|
|
||||||
msgid "Inherit Survey Question for extra fields"
|
|
||||||
msgstr "Question du sondage"
|
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. 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_generated_record____last_update
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation____last_update
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation____last_update
|
||||||
@@ -257,6 +272,15 @@ msgstr ""
|
|||||||
"Lors de la création d'un enregistrement, si un autre enregistrement existe "
|
"Lors de la création d'un enregistrement, si un autre enregistrement existe "
|
||||||
"avec la même valeur, l'enregistrement ne sera pas créé."
|
"avec la même valeur, l'enregistrement ne sera pas créé."
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model_terms:ir.ui.view,arch_db:survey_record_generation.survey_survey_view_form
|
||||||
|
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."
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#. odoo-python
|
#. odoo-python
|
||||||
#: code:addons/survey_record_generation/models/survey_record_creation_field_values.py:0
|
#: code:addons/survey_record_generation/models/survey_record_creation_field_values.py:0
|
||||||
@@ -329,6 +353,11 @@ msgstr "Sondage"
|
|||||||
msgid "Survey Label"
|
msgid "Survey Label"
|
||||||
msgstr "Étiquette du sondage"
|
msgstr "Étiquette du sondage"
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model:ir.model,name:survey_record_generation.model_survey_question
|
||||||
|
msgid "Survey Question"
|
||||||
|
msgstr "Question du sondage"
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__survey_record_creation_id
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__survey_record_creation_id
|
||||||
msgid "Survey Record Creation"
|
msgid "Survey Record Creation"
|
||||||
@@ -341,8 +370,8 @@ msgstr "Sondage Création d'enregistrement Valeur des champs"
|
|||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model,name:survey_record_generation.model_survey_user_input
|
#: model:ir.model,name:survey_record_generation.model_survey_user_input
|
||||||
msgid "Survey User Input for custom matrix"
|
msgid "Survey User Input"
|
||||||
msgstr "Entrée utilisateur du sondage"
|
msgstr "Saisie utilisateur du sondage"
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_generated_record__survey_record_creation_id
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_generated_record__survey_record_creation_id
|
||||||
@@ -354,20 +383,36 @@ msgstr "Génération d'enregistrement depuis la participation"
|
|||||||
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid ""
|
msgid ""
|
||||||
"The field %s is mandatory. In Record Creation tab, drag "
|
"The field %(field)s is mandatory for model %(model)s. In Record Creation "
|
||||||
"%s at the top of the table"
|
"tab, drag %(record)s on top of the model %(model)s."
|
||||||
|
msgstr ""
|
||||||
|
"Le champs %(field)s est obligatoire pour le modèle %(model)s. Dans l'onglet "
|
||||||
|
"Création d'un enregistrement, placez la ligne %(record)s au dessus de la "
|
||||||
|
"ligne du modèle %(model)s."
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#. odoo-python
|
||||||
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
|
#, python-format
|
||||||
|
msgid ""
|
||||||
|
"The field %s is mandatory. In Record Creation tab, drag %s at the top of the"
|
||||||
|
" table"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Le champ %s est obligatoire. Dans l'onglet Création d'un enregistrement, glissez "
|
|
||||||
"%s sur la première ligne du tableau"
|
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__unicity_check
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__unicity_check
|
||||||
msgid "Unicity constraint"
|
msgid "Unicity constraint"
|
||||||
msgstr "Contrainte d'unicité"
|
msgstr "Contrainte d'unicité"
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation__update_existing_records
|
||||||
|
msgid "Update existing records"
|
||||||
|
msgstr "Mettre à jour les enregistrements existants"
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_question_answer__value_char
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_question_answer__value_char
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__displayed_value
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__displayed_value
|
||||||
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_boolean
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_char
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_char
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_date
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_date
|
||||||
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_datetime
|
#: model:ir.model.fields,field_description:survey_record_generation.field_survey_record_creation_field_values__fixed_value_datetime
|
||||||
@@ -400,6 +445,50 @@ msgstr "Message d'erreur"
|
|||||||
msgid "You should append at least one record in %s"
|
msgid "You should append at least one record in %s"
|
||||||
msgstr "Vous devez au moins ajouter un enregistrement dans %s"
|
msgstr "Vous devez au moins ajouter un enregistrement dans %s"
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#. odoo-python
|
||||||
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
|
#, python-format
|
||||||
|
msgid ""
|
||||||
|
"[Survey record generation] The answer values type '%(type)s' is not "
|
||||||
|
"supported (for question %(question)s). Use 'record' or 'value' instead."
|
||||||
|
msgstr ""
|
||||||
|
"[Survey record generation] La valeur '%(type)s' pour le champs "
|
||||||
|
"'answer_values_type' n'est pas supportée (pour la question %(question)s). "
|
||||||
|
"Veuillez utiliser 'Enregistrement' ou 'Valeur' à la place."
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#. odoo-python
|
||||||
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
|
#, python-format
|
||||||
|
msgid ""
|
||||||
|
"[Survey record generation] The boolean value %s(value)s is not supported "
|
||||||
|
"(for question %(question)s)."
|
||||||
|
msgstr ""
|
||||||
|
"[Survey record generation] La valeur booléenne %s(value)s n'est pas "
|
||||||
|
"supportée (pour la question %(question)s)."
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#. odoo-python
|
||||||
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
|
#, python-format
|
||||||
|
msgid ""
|
||||||
|
"[Survey record generation] The question type %(type)s is not recognized (for"
|
||||||
|
" question %(question)s)."
|
||||||
|
msgstr ""
|
||||||
|
"[Survey record generation] Le type de question %(type)s n'est pas reconnu "
|
||||||
|
"(pour la question %(question)s)."
|
||||||
|
|
||||||
|
#. module: survey_record_generation
|
||||||
|
#. odoo-python
|
||||||
|
#: code:addons/survey_record_generation/models/survey_user_input.py:0
|
||||||
|
#, python-format
|
||||||
|
msgid ""
|
||||||
|
"[Survey record generation] The question type %(type)s is not supported yet."
|
||||||
|
msgstr ""
|
||||||
|
"[Survey record generation] Le type de question %(type)s n'est pas encore "
|
||||||
|
"supporté."
|
||||||
|
|
||||||
#. module: survey_record_generation
|
#. module: survey_record_generation
|
||||||
#. odoo-python
|
#. odoo-python
|
||||||
#: code:addons/survey_record_generation/models/survey_record_creation_field_values.py:0
|
#: code:addons/survey_record_generation/models/survey_record_creation_field_values.py:0
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import ast
|
import ast
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
from odoo import api, fields, models, _, Command
|
from odoo import api, fields, models, _, Command
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
AnswerValuesType: Literal["no", "value", "record"]
|
||||||
|
|
||||||
class SurveyQuestion(models.Model):
|
class SurveyQuestion(models.Model):
|
||||||
_inherit = 'survey.question'
|
_inherit = 'survey.question'
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from odoo import api, fields, models, _
|
from odoo import api, fields, models, _
|
||||||
@@ -18,11 +17,33 @@ class SurveyRecordCreation(models.Model):
|
|||||||
warning_message = fields.Html('Warning message', compute="_compute_warning_message")
|
warning_message = fields.Html('Warning message', compute="_compute_warning_message")
|
||||||
sequence = fields.Integer("sequence")
|
sequence = fields.Integer("sequence")
|
||||||
|
|
||||||
@api.onchange('model_id')
|
update_existing_records = fields.Boolean(
|
||||||
|
string="Update existing records",
|
||||||
|
)
|
||||||
|
field_to_retrieve_existing_records = fields.Many2one(
|
||||||
|
"ir.model.fields",
|
||||||
|
domain="[('id', 'in', allowed_field_ids), ('readonly', '=', False)]",
|
||||||
|
ondelete="cascade",
|
||||||
|
help="Choose the field you want to use to retrieve the existing record. "
|
||||||
|
"WARNING: We update only the first record found.",
|
||||||
|
)
|
||||||
|
allowed_field_ids = fields.Many2many(
|
||||||
|
"ir.model.fields",
|
||||||
|
compute="_compute_allowed_field_ids",
|
||||||
|
store=True,
|
||||||
|
string="Allowed Fields",
|
||||||
|
)
|
||||||
|
|
||||||
|
@api.depends("field_values_ids.field_id")
|
||||||
|
def _compute_allowed_field_ids(self):
|
||||||
|
for record in self:
|
||||||
|
record.allowed_field_ids = record.field_values_ids.mapped("field_id")
|
||||||
|
|
||||||
|
@api.onchange("model_id")
|
||||||
def clear_field_values_ids(self):
|
def clear_field_values_ids(self):
|
||||||
self.field_values_ids = None
|
self.field_values_ids = None
|
||||||
|
|
||||||
@api.depends('model_id','field_values_ids')
|
@api.depends("model_id","field_values_ids")
|
||||||
def _compute_warning_message(self):
|
def _compute_warning_message(self):
|
||||||
for record_creation in self:
|
for record_creation in self:
|
||||||
# check if all mandatory fields set
|
# check if all mandatory fields set
|
||||||
@@ -37,7 +58,3 @@ class SurveyRecordCreation(models.Model):
|
|||||||
record_creation.warning_message = None
|
record_creation.warning_message = None
|
||||||
else:
|
else:
|
||||||
record_creation.warning_message = None
|
record_creation.warning_message = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ from odoo.tools.misc import format_date
|
|||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
type_mapping = {
|
type_mapping = { #field types on the left, question types on the right. TODO : what about booleans ?
|
||||||
"char": ["char_box", "numerical_box", "date", "datetime", "simple_choice", "multiple_choice"],
|
"char": ["char_box", "numerical_box", "date", "datetime", "simple_choice", "multiple_choice"],
|
||||||
"text": ["char_box", "date", "simple_choice"],
|
"text": ["char_box", "date", "simple_choice"],
|
||||||
"html": ["text_box", "numerical_box", "datetime", "simple_choice"],
|
"html": ["text_box", "numerical_box", "datetime", "simple_choice"],
|
||||||
|
|||||||
@@ -1,13 +1,25 @@
|
|||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
from odoo import models, fields, _
|
from typing import TYPE_CHECKING, Any, Literal
|
||||||
|
|
||||||
|
from odoo import _, fields, models
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
from odoo.fields import Command
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .survey_question import AnswerValuesType
|
||||||
|
from .survey_record_creation import SurveyRecordCreation
|
||||||
|
from .survey_record_creation_field_values import SurveyRecordCreationFieldValues
|
||||||
|
|
||||||
|
|
||||||
class SurveyUserInput(models.Model):
|
class SurveyUserInput(models.Model):
|
||||||
_inherit = "survey.user_input"
|
_inherit = "survey.user_input"
|
||||||
|
|
||||||
generated_record_ids = fields.One2many('survey.generated.record', 'user_input_id', 'Generated records')
|
generated_record_ids = fields.One2many(
|
||||||
generated_records_count = fields.Integer("Attempts Count", compute='_compute_generated_records_count')
|
"survey.generated.record", "user_input_id", "Generated records"
|
||||||
|
)
|
||||||
|
generated_records_count = fields.Integer(
|
||||||
|
"Attempts Count", compute="_compute_generated_records_count"
|
||||||
|
)
|
||||||
|
|
||||||
def _compute_generated_records_count(self):
|
def _compute_generated_records_count(self):
|
||||||
for user_input in self:
|
for user_input in self:
|
||||||
@@ -16,112 +28,313 @@ class SurveyUserInput(models.Model):
|
|||||||
def action_redirect_to_generated_records(self):
|
def action_redirect_to_generated_records(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
|
|
||||||
action = self.env['ir.actions.act_window']._for_xml_id('survey_record_generation.survey_generated_record_action')
|
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||||
""" context = dict(self.env.context or {})
|
"survey_record_generation.survey_generated_record_action"
|
||||||
|
)
|
||||||
context['create'] = False
|
|
||||||
context['search_default_survey_id'] = self.survey_id.id
|
|
||||||
context['search_default_group_by_survey'] = False
|
|
||||||
if self.partner_id:
|
|
||||||
context['search_default_partner_id'] = self.partner_id.id
|
|
||||||
elif self.email:
|
|
||||||
context['search_default_email'] = self.email
|
|
||||||
|
|
||||||
action['context'] = context """
|
|
||||||
|
|
||||||
return action
|
return action
|
||||||
|
|
||||||
def _mark_done(self, ignore_when_res_partner_mandatory_fields_are_missing = False):
|
def _mark_done(
|
||||||
|
self, ignore_when_res_partner_mandatory_fields_are_missing: bool = False
|
||||||
|
):
|
||||||
# generate records
|
# generate records
|
||||||
for user_input in self:
|
for user_input in self:
|
||||||
created_records = {}
|
created_records = {}
|
||||||
fields_to_update = []
|
other_record_fields_to_update: list[SurveyRecordCreationFieldValues] = []
|
||||||
|
|
||||||
for record_creation in user_input.survey_id.survey_record_creation_ids.sorted('sequence'):
|
record_creation: SurveyRecordCreation
|
||||||
model = record_creation.model_id.model
|
for (
|
||||||
vals = {}
|
record_creation
|
||||||
ModelClass = self.env[model]
|
) in user_input.survey_id.survey_record_creation_ids.sorted("sequence"):
|
||||||
|
model: str = record_creation.model_id.model
|
||||||
|
vals: dict = {}
|
||||||
|
|
||||||
|
field_value: SurveyRecordCreationFieldValues
|
||||||
for field_value in record_creation.field_values_ids:
|
for field_value in record_creation.field_values_ids:
|
||||||
if field_value.value_origin == 'fixed':
|
value, other_record_fields_to_update = (
|
||||||
vals[field_value.field_id.name] = field_value.get_fixed_value_for_record_creation()
|
self.get_value_based_on_value_origin(
|
||||||
elif field_value.value_origin == 'question':
|
field_value=field_value,
|
||||||
# find user_input_lines of the question
|
user_input=user_input,
|
||||||
user_input_lines = [user_input_line for user_input_line in user_input.user_input_line_ids if user_input_line.question_id == field_value.question_id]
|
created_records=created_records,
|
||||||
|
model=model,
|
||||||
|
other_record_fields_to_update=other_record_fields_to_update,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
field_name: str = field_value.field_id.name
|
||||||
|
|
||||||
if not user_input_lines:
|
vals[field_name] = value
|
||||||
continue
|
|
||||||
|
|
||||||
if field_value.question_id.question_type in ['simple_choice', 'multiple_choice','matrix']:
|
existing_record = self.find_existing_record(record_creation, vals)
|
||||||
if field_value.question_id.answer_values_type == 'record':
|
|
||||||
record_ids = []
|
duplicate = self.find_duplicate_if_there_are_fields_with_unicity_check(
|
||||||
for user_input_line in user_input_lines:
|
model, record_creation, vals
|
||||||
if user_input_line.suggested_answer_id and user_input_line.suggested_answer_id.record_id:
|
)
|
||||||
record_ids.append(user_input_line.suggested_answer_id.record_id.id)
|
|
||||||
if field_value.question_id.question_type == 'simple_choice':
|
|
||||||
if record_ids:
|
|
||||||
vals[field_value.field_id.name] = record_ids[0]
|
|
||||||
else:
|
|
||||||
vals[field_value.field_id.name] = None
|
|
||||||
else:
|
|
||||||
vals[field_value.field_id.name] = record_ids
|
|
||||||
if field_value.question_id.answer_values_type == 'value':
|
|
||||||
if field_value.field_id.ttype == "boolean":
|
|
||||||
boolean_value = user_input_lines[0].suggested_answer_id.value_char in [True, 1, "1", "True", "true", "Oui", "oui"]
|
|
||||||
vals[field_value.field_id.name] = boolean_value
|
|
||||||
else:
|
|
||||||
vals[field_value.field_id.name] = user_input_lines[0].suggested_answer_id.value_char
|
|
||||||
elif user_input_lines[0].answer_type: # if value not filled by user, answer_type not set
|
|
||||||
vals[field_value.field_id.name] = user_input_lines[0][f"value_{user_input_lines[0].answer_type}"]
|
|
||||||
else:
|
|
||||||
vals[field_value.field_id.name] = None
|
|
||||||
elif field_value.value_origin == 'other_record':
|
|
||||||
fields_to_update.append(field_value)
|
|
||||||
# check if the field to update is mandatory
|
|
||||||
if ModelClass._fields[field_value.field_id.name].required:
|
|
||||||
# check if the other record is already created, if yes add it to vals
|
|
||||||
if len(created_records) > 0 and created_records[field_value.other_created_record_id.id]:
|
|
||||||
linked_record = created_records[field_value.other_created_record_id.id]
|
|
||||||
vals[field_value.field_id.name] = linked_record.id
|
|
||||||
else:
|
|
||||||
raise UserError(
|
|
||||||
_("The field %s is mandatory. In Record Creation tab, drag %s at the top of the table")
|
|
||||||
% (field_value.field_id.display_name, field_value.other_created_record_id.name)
|
|
||||||
)
|
|
||||||
# check duplicates
|
|
||||||
uniq_fields = [field_value.field_id.name for field_value in record_creation.field_values_ids.filtered(lambda r:r.unicity_check)]
|
|
||||||
duplicate = None
|
|
||||||
if uniq_fields:
|
|
||||||
uniq_domain = []
|
|
||||||
for uniq_field in uniq_fields:
|
|
||||||
uniq_domain.append((uniq_field,'=',vals[uniq_field]))
|
|
||||||
duplicate = self.env[model].search(uniq_domain, limit=1)
|
|
||||||
|
|
||||||
if duplicate:
|
if duplicate:
|
||||||
record = 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)
|
||||||
|
}
|
||||||
|
record = existing_record.write(vals_with_keys_not_in_record)
|
||||||
else:
|
else:
|
||||||
if model == "res.partner" and ignore_when_res_partner_mandatory_fields_are_missing:
|
if (
|
||||||
# this part has been developed for Calim specific needs : being able to create several Contacts with the same survey
|
model == "res.partner"
|
||||||
# TODO : find a way to make it generic for all models ?
|
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"):
|
if not vals.get("lastname") and not vals.get("firstname"):
|
||||||
continue
|
continue
|
||||||
# Create record
|
# Create record
|
||||||
record = self.env[model].create(vals)
|
record = self.env[model].create(vals)
|
||||||
# Link generated records to user input
|
# Link generated records to user input
|
||||||
self.env['survey.generated.record'].create({
|
self.env["survey.generated.record"].create(
|
||||||
'survey_record_creation_name':record_creation.name,
|
{
|
||||||
'survey_record_creation_id':record_creation.id,
|
"survey_record_creation_name": record_creation.name,
|
||||||
'user_input_id':user_input.id,
|
"survey_record_creation_id": record_creation.id,
|
||||||
"created_record_id":"%s,%s" % (model,record.id)
|
"user_input_id": user_input.id,
|
||||||
})
|
"created_record_id": f"{model},{record.id}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
created_records[record_creation.id] = record
|
created_records[record_creation.id] = record
|
||||||
|
|
||||||
# update linked records
|
# update linked record
|
||||||
for field_to_update in fields_to_update:
|
for field_to_update in other_record_fields_to_update:
|
||||||
record_to_update = created_records.get(field_to_update.survey_record_creation_id.id)
|
record_to_update = created_records.get(
|
||||||
|
field_to_update.survey_record_creation_id.id
|
||||||
|
)
|
||||||
if record_to_update:
|
if record_to_update:
|
||||||
linked_record = created_records[field_to_update.other_created_record_id.id]
|
linked_record = created_records[
|
||||||
record_to_update.write({field_to_update.field_id.name:linked_record.id})
|
field_to_update.other_created_record_id.id
|
||||||
|
]
|
||||||
|
value = self.get_value_for_relational_field(
|
||||||
|
field_to_update, linked_record
|
||||||
|
)
|
||||||
|
record_to_update.write({field_to_update.field_id.name: value})
|
||||||
|
|
||||||
return super()._mark_done()
|
return super()._mark_done()
|
||||||
|
|
||||||
|
def find_existing_record(
|
||||||
|
self, record_creation: "SurveyRecordCreation", vals: dict
|
||||||
|
) -> Any:
|
||||||
|
if record_creation.update_existing_records:
|
||||||
|
model = record_creation.model_id.model
|
||||||
|
search_field = record_creation.field_to_retrieve_existing_records
|
||||||
|
user_answer_value = vals.get(search_field.name)
|
||||||
|
if user_answer_value:
|
||||||
|
return self.env[model].search(
|
||||||
|
[(search_field.name, "=", user_answer_value)], limit=1
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_value_based_on_value_origin(
|
||||||
|
self,
|
||||||
|
field_value: "SurveyRecordCreationFieldValues",
|
||||||
|
user_input: "SurveyUserInput",
|
||||||
|
created_records: dict[Any, Any],
|
||||||
|
model: str,
|
||||||
|
other_record_fields_to_update: list["SurveyRecordCreationFieldValues"],
|
||||||
|
) -> tuple[Any, list["SurveyRecordCreationFieldValues"]]:
|
||||||
|
value: Any = None
|
||||||
|
|
||||||
|
if field_value.value_origin == "fixed":
|
||||||
|
value = field_value.get_fixed_value_for_record_creation()
|
||||||
|
elif field_value.value_origin == "question":
|
||||||
|
value = self.get_value_from_user_answer(field_value, user_input)
|
||||||
|
elif field_value.value_origin == "other_record":
|
||||||
|
# if the other_record value is a required field, get it or raise
|
||||||
|
value = self.get_required_value_from_other_record(
|
||||||
|
model, created_records, field_value
|
||||||
|
)
|
||||||
|
# otherwise, we update the record later (out of this for loop)
|
||||||
|
if not value:
|
||||||
|
other_record_fields_to_update.append(field_value)
|
||||||
|
return value, other_record_fields_to_update
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_value_for_relational_field(
|
||||||
|
field_to_update: "SurveyRecordCreationFieldValues", linked_record
|
||||||
|
) -> Any:
|
||||||
|
field_type = field_to_update.field_id.ttype
|
||||||
|
if field_type == "many2one":
|
||||||
|
return linked_record.id
|
||||||
|
else:
|
||||||
|
# many2many or one2many
|
||||||
|
return [Command.set(linked_record.ids)]
|
||||||
|
|
||||||
|
def find_duplicate_if_there_are_fields_with_unicity_check(
|
||||||
|
self, model: str, record_creation: "SurveyRecordCreation", vals: dict[Any, Any]
|
||||||
|
) -> Any:
|
||||||
|
# check duplicates
|
||||||
|
unique_fields = [
|
||||||
|
field_value.field_id.name
|
||||||
|
for field_value in record_creation.field_values_ids.filtered(
|
||||||
|
lambda r: r.unicity_check
|
||||||
|
)
|
||||||
|
]
|
||||||
|
duplicate = None
|
||||||
|
if unique_fields:
|
||||||
|
uniq_domain = []
|
||||||
|
for uniq_field in unique_fields:
|
||||||
|
uniq_domain.append((uniq_field, "=", vals[uniq_field]))
|
||||||
|
duplicate = self.env[model].search(uniq_domain, limit=1)
|
||||||
|
return duplicate
|
||||||
|
|
||||||
|
def get_required_value_from_other_record(
|
||||||
|
self,
|
||||||
|
model: str,
|
||||||
|
created_records: dict[Any, Any],
|
||||||
|
field_value: "SurveyRecordCreationFieldValues",
|
||||||
|
) -> Any:
|
||||||
|
model_class = self.env[model]
|
||||||
|
if model_class._fields[field_value.field_id.name].required:
|
||||||
|
# check if the other record is already created,
|
||||||
|
# if yes add it to vals, else raise
|
||||||
|
if (
|
||||||
|
len(created_records) > 0
|
||||||
|
and created_records[field_value.other_created_record_id.id]
|
||||||
|
):
|
||||||
|
linked_record = created_records[field_value.other_created_record_id.id]
|
||||||
|
return self.get_value_for_relational_field(field_value, linked_record)
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_value_from_user_answer(
|
||||||
|
self,
|
||||||
|
field_value: "SurveyRecordCreationFieldValues",
|
||||||
|
user_input: "SurveyUserInput",
|
||||||
|
) -> Any:
|
||||||
|
# find user_input_lines (which are user's answers) for the question
|
||||||
|
user_input_lines = [
|
||||||
|
user_input_line
|
||||||
|
for user_input_line in user_input.user_input_line_ids
|
||||||
|
if user_input_line.question_id == field_value.question_id
|
||||||
|
]
|
||||||
|
|
||||||
|
if not user_input_lines:
|
||||||
|
# If the question has not been displayed to the user,
|
||||||
|
# there are no user_input_lines
|
||||||
|
return None
|
||||||
|
if user_input_lines[0].skipped:
|
||||||
|
# The question has been ignored by the user
|
||||||
|
return None
|
||||||
|
|
||||||
|
question_type = field_value.question_id.question_type
|
||||||
|
|
||||||
|
if question_type in [
|
||||||
|
"char_box",
|
||||||
|
"text_box",
|
||||||
|
"numerical_box",
|
||||||
|
"date",
|
||||||
|
"datetime",
|
||||||
|
]:
|
||||||
|
return user_input_lines[0][f"value_{user_input_lines[0].answer_type}"]
|
||||||
|
elif question_type in ["simple_choice", "multiple_choice", "matrix"]:
|
||||||
|
answer_values_type = field_value.question_id.answer_values_type
|
||||||
|
return self.get_value_based_on_answer_values_type(
|
||||||
|
answer_values_type, field_value, question_type, user_input_lines
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise UserError(
|
||||||
|
_(
|
||||||
|
"[Survey record generation] The question type %(type)s is not "
|
||||||
|
"recognized (for question %(question)s)."
|
||||||
|
)
|
||||||
|
% {"type": question_type, "question": field_value.question_id.title}
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_value_based_on_answer_values_type(
|
||||||
|
self,
|
||||||
|
answer_values_type: "AnswerValuesType",
|
||||||
|
field_value: "SurveyRecordCreationFieldValues",
|
||||||
|
question_type: Literal["simple_choice", "multiple_choice", "matrix"],
|
||||||
|
user_input_lines: list[Any],
|
||||||
|
) -> Any:
|
||||||
|
if answer_values_type == "record":
|
||||||
|
answered_record_ids = []
|
||||||
|
for user_input_line in user_input_lines:
|
||||||
|
if (
|
||||||
|
user_input_line.suggested_answer_id
|
||||||
|
and user_input_line.suggested_answer_id.record_id
|
||||||
|
):
|
||||||
|
answered_record_ids.append(
|
||||||
|
user_input_line.suggested_answer_id.record_id.id
|
||||||
|
)
|
||||||
|
if not answered_record_ids:
|
||||||
|
return None
|
||||||
|
if question_type == "simple_choice":
|
||||||
|
return answered_record_ids[0]
|
||||||
|
elif question_type == "multiple_choice":
|
||||||
|
return answered_record_ids
|
||||||
|
else:
|
||||||
|
raise UserError(
|
||||||
|
_(
|
||||||
|
"[Survey record generation] The question type"
|
||||||
|
" %(type)s is not supported yet."
|
||||||
|
)
|
||||||
|
% {"type": question_type}
|
||||||
|
)
|
||||||
|
elif answer_values_type == "value":
|
||||||
|
answer_value_char = user_input_lines[0].suggested_answer_id.value_char
|
||||||
|
if field_value.field_id.ttype != "boolean":
|
||||||
|
return answer_value_char
|
||||||
|
else:
|
||||||
|
return self.get_boolean_value(
|
||||||
|
answer_value_char=answer_value_char,
|
||||||
|
question_title=field_value.question_id.title,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise UserError(
|
||||||
|
_(
|
||||||
|
"[Survey record generation] The answer values type '%(type)s' "
|
||||||
|
"is not supported (for question %(question)s). Use 'record' or "
|
||||||
|
"'value' instead."
|
||||||
|
)
|
||||||
|
% {
|
||||||
|
"type": answer_values_type,
|
||||||
|
"question": field_value.question_id.title,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_boolean_value(answer_value_char: str, question_title: str) -> bool:
|
||||||
|
# Below code is a trick to be able to use "simple_choice" question
|
||||||
|
# with values 'yes' and 'no' and transform it to boolean.
|
||||||
|
if boolean_value := answer_value_char in [
|
||||||
|
"1",
|
||||||
|
"True",
|
||||||
|
"true",
|
||||||
|
"Oui",
|
||||||
|
"oui",
|
||||||
|
"Yes",
|
||||||
|
"yes",
|
||||||
|
]:
|
||||||
|
return boolean_value
|
||||||
|
else:
|
||||||
|
raise UserError(
|
||||||
|
_(
|
||||||
|
"[Survey record generation] The boolean value %s(value)s "
|
||||||
|
"is not supported (for question %(question)s)."
|
||||||
|
)
|
||||||
|
% {
|
||||||
|
"value": answer_value_char,
|
||||||
|
"question": question_title,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
* `Elabore <https://www.elabore.coop>`_
|
* `Elabore <https://www.elabore.coop>`_
|
||||||
|
|
||||||
* Clément Thomas
|
* Clément Thomas
|
||||||
|
* Quentin Mondot
|
||||||
|
|||||||
1
survey_record_generation/tests/__init__.py
Normal file
1
survey_record_generation/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import test_survey_record_creation
|
||||||
840
survey_record_generation/tests/test_survey_record_creation.py
Normal file
840
survey_record_generation/tests/test_survey_record_creation.py
Normal file
@@ -0,0 +1,840 @@
|
|||||||
|
from datetime import date
|
||||||
|
|
||||||
|
from psycopg2 import IntegrityError
|
||||||
|
|
||||||
|
from odoo.addons.survey.tests.common import SurveyCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestSurveyRecordCreation(SurveyCase):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
# We create a survey
|
||||||
|
self.survey = self.env["survey.survey"].create(
|
||||||
|
{
|
||||||
|
"title": "Test Survey",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# With a single question "name"
|
||||||
|
self.question_name = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Name",
|
||||||
|
qtype="char_box",
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.res_partner_model = self.env["ir.model"]._get("res.partner")
|
||||||
|
# The administrator has set up a record creation on res.partner model
|
||||||
|
self.survey_record_creation = self.env["survey.record.creation"].create(
|
||||||
|
{
|
||||||
|
"name": "Contact",
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.name_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "name")]
|
||||||
|
)
|
||||||
|
# And linked the res_partner field "name" to the answer of question_name
|
||||||
|
self.name_survey_record_creation_field_values = 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": self.name_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_name.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_record_is_created(self):
|
||||||
|
# Easy test to become familiar with the subject (check comments of setUp)
|
||||||
|
|
||||||
|
# We simulate that jean@test.fr has answered the survey
|
||||||
|
self.answer = self._add_answer(
|
||||||
|
survey=self.survey, partner=False, email="jean@test.fr"
|
||||||
|
)
|
||||||
|
# He answered "Jean" to the question_name
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_name, answer=self.answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
# And validate the survey
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
# Thus, the res.partner with the name "Jean" has been created
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
|
||||||
|
def test_all_types_of_question(self):
|
||||||
|
# Still todo : "datetime" and "text_box"
|
||||||
|
# Also todo : "simple_choice" with answer_values_type "no"
|
||||||
|
# Also todo : "multiple_choice" with answer_values_type "no" and "value"
|
||||||
|
# Note : matrix question type is never proposed in the allowed_question_ids
|
||||||
|
|
||||||
|
self.answer = self._add_answer(
|
||||||
|
survey=self.survey, partner=False, email="jean@test.fr"
|
||||||
|
)
|
||||||
|
|
||||||
|
### "char_box" type of question, tested with field CHAR "name" ###
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_name, answer=self.answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
|
||||||
|
### "numerical_box" type of question,
|
||||||
|
# tested with FLOAT field "partner_latitude" ###
|
||||||
|
self.question_partner_latitude = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Partner latitude",
|
||||||
|
qtype="numerical_box",
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
partner_latitude_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "partner_latitude")]
|
||||||
|
)
|
||||||
|
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": partner_latitude_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_partner_latitude.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_partner_latitude,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=44.73333,
|
||||||
|
)
|
||||||
|
|
||||||
|
### "date" type of question, tested with DATE field "date" ###
|
||||||
|
self.question_date = self._add_question(
|
||||||
|
page=None, name="Date", qtype="date", survey_id=self.survey.id, sequence=1
|
||||||
|
)
|
||||||
|
|
||||||
|
date_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "date")]
|
||||||
|
)
|
||||||
|
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": date_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_date.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_date, answer=self.answer, answer_value=date.today()
|
||||||
|
)
|
||||||
|
|
||||||
|
### "simple_choice" type of question, tested with SELECTION field "type" ###
|
||||||
|
### Here we also test answer_values_type "value"
|
||||||
|
self.question_type = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Type",
|
||||||
|
qtype="simple_choice",
|
||||||
|
labels=[
|
||||||
|
{"value": "contact"},
|
||||||
|
{"value": "other"},
|
||||||
|
],
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
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
|
||||||
|
|
||||||
|
type_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "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": self.question_type.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_type,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=self.question_type.suggested_answer_ids[0].id,
|
||||||
|
)
|
||||||
|
|
||||||
|
### "simple_choice" type of question, tested with MANY2ONE field "title" ###
|
||||||
|
### Here we also test answer_values_type "record"
|
||||||
|
|
||||||
|
mister_title = self.env["res.partner.title"].create({"name": "Mister"})
|
||||||
|
madam_title = self.env["res.partner.title"].create({"name": "Madam"})
|
||||||
|
self.question_title = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Title",
|
||||||
|
qtype="simple_choice",
|
||||||
|
labels=[
|
||||||
|
{"value": mister_title.display_name},
|
||||||
|
{"value": madam_title.display_name},
|
||||||
|
],
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
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}"
|
||||||
|
|
||||||
|
title_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "title")]
|
||||||
|
)
|
||||||
|
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": title_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_title.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_title,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=self.question_title.suggested_answer_ids[0].id,
|
||||||
|
)
|
||||||
|
|
||||||
|
### "multiple_choice" type of question,
|
||||||
|
# tested with MANY2MANY field "category" ###
|
||||||
|
adult_category = self.env["res.partner.category"].create({"name": "Adult"})
|
||||||
|
teenager_category = self.env["res.partner.category"].create(
|
||||||
|
{"name": "Teenager"}
|
||||||
|
)
|
||||||
|
child_category = self.env["res.partner.category"].create({"name": "Child"})
|
||||||
|
self.question_category = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Category",
|
||||||
|
qtype="multiple_choice",
|
||||||
|
labels=[
|
||||||
|
{"value": adult_category.display_name},
|
||||||
|
{"value": teenager_category.display_name},
|
||||||
|
{"value": child_category.display_name},
|
||||||
|
],
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
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}"
|
||||||
|
|
||||||
|
category_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "category_id")]
|
||||||
|
)
|
||||||
|
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": category_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_category.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_category,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=self.question_category.suggested_answer_ids[0].id,
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_category,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=self.question_category.suggested_answer_ids[1].id,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.answer._mark_done()
|
||||||
|
partner = self.env["res.partner"].search(
|
||||||
|
[
|
||||||
|
("name", "=", "Jean"),
|
||||||
|
("partner_latitude", "=", 44.73333),
|
||||||
|
("date", "=", date.today()),
|
||||||
|
("type", "=", "contact"),
|
||||||
|
("title", "=", mister_title.id),
|
||||||
|
("category_id", "=", adult_category.id),
|
||||||
|
("category_id", "=", teenager_category.id),
|
||||||
|
("category_id", "!=", child_category.id),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
|
||||||
|
def test_records_are_created(self):
|
||||||
|
# we test that several records can be created at the end of the same survey
|
||||||
|
# concurrently, we test the value_origin "other_record" and "fixed"
|
||||||
|
res_partner_bank_model = self.env["ir.model"]._get("res.partner.bank")
|
||||||
|
self.bank_survey_record_creation = self.env["survey.record.creation"].create(
|
||||||
|
{
|
||||||
|
"name": "Bank",
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": res_partner_bank_model.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# Below we test "value_origin": "other_record" with a required field
|
||||||
|
partner_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner.bank"), ("name", "=", "partner_id")]
|
||||||
|
)
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": self.bank_survey_record_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": res_partner_bank_model.id,
|
||||||
|
"field_id": partner_field.id,
|
||||||
|
"value_origin": "other_record",
|
||||||
|
"other_created_record_id": self.survey_record_creation.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# Below we test "value_origin": "fixed"
|
||||||
|
acc_number_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner.bank"), ("name", "=", "acc_number")]
|
||||||
|
)
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": self.bank_survey_record_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": res_partner_bank_model.id,
|
||||||
|
"field_id": acc_number_field.id,
|
||||||
|
"value_origin": "fixed",
|
||||||
|
"fixed_value_char": "FR76 1444 5004 0004 0000 0000 000",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# Below we test "value_origin": "other_record" with a NOT required field
|
||||||
|
bank_ids_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "bank_ids")]
|
||||||
|
)
|
||||||
|
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": bank_ids_field.id,
|
||||||
|
"value_origin": "other_record",
|
||||||
|
"other_created_record_id": self.bank_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"
|
||||||
|
)
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
bank_account = self.env["res.partner.bank"].search(
|
||||||
|
[("partner_id", "=", partner.id)]
|
||||||
|
)
|
||||||
|
self.assertTrue(bank_account.acc_number == "FR76 1444 5004 0004 0000 0000 000")
|
||||||
|
|
||||||
|
def test_records_of_same_model_are_created(self):
|
||||||
|
# When we have 2 survey.record.creation on res.partner,
|
||||||
|
# we check that 2 contacts are created
|
||||||
|
self.second_question_name = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Name of second person",
|
||||||
|
qtype="char_box",
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.second_contact_creation = self.env["survey.record.creation"].create(
|
||||||
|
{
|
||||||
|
"name": "Second contact",
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": self.second_contact_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"field_id": self.name_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.second_question_name.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.first_answer = self._add_answer(
|
||||||
|
survey=self.survey, partner=False, email="jean@test.fr"
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_name,
|
||||||
|
answer=self.first_answer,
|
||||||
|
answer_value="Jean",
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.second_question_name,
|
||||||
|
answer=self.first_answer,
|
||||||
|
answer_value="Jeanne",
|
||||||
|
)
|
||||||
|
self.first_answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
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.assertTrue(partner.name == "Jean")
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
partners = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(len(partners) == 2)
|
||||||
|
|
||||||
|
def test_unicity_check(self):
|
||||||
|
# In this test, we check the behavior of unicity_check
|
||||||
|
self.name_survey_record_creation_field_values.unicity_check = True
|
||||||
|
|
||||||
|
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.assertTrue(partner.name == "Jean")
|
||||||
|
|
||||||
|
self.second_answer = self._add_answer(
|
||||||
|
survey=self.survey, partner=False, email="jean@test.fr"
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_name, answer=self.second_answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
self.second_answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
self.assertTrue(len(partner) == 1)
|
||||||
|
|
||||||
|
def test_some_questions_are_not_answered(self):
|
||||||
|
self.question_email = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Email",
|
||||||
|
qtype="char_box",
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
)
|
||||||
|
mister_title = self.env["res.partner.title"].create({"name": "Mister"})
|
||||||
|
madam_title = self.env["res.partner.title"].create({"name": "Madam"})
|
||||||
|
self.question_title = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Title",
|
||||||
|
qtype="simple_choice",
|
||||||
|
labels=[
|
||||||
|
{"value": mister_title.display_name},
|
||||||
|
{"value": madam_title.display_name},
|
||||||
|
],
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
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_street = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Street",
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
title_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "title")]
|
||||||
|
)
|
||||||
|
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": title_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_title.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
street_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "street")]
|
||||||
|
)
|
||||||
|
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": self.question_street.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=False,
|
||||||
|
skipped=True,
|
||||||
|
answer_type=False,
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_title,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=False,
|
||||||
|
skipped=True,
|
||||||
|
answer_type=False,
|
||||||
|
)
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.name == "Jean")
|
||||||
|
self.assertFalse(partner.email)
|
||||||
|
self.assertFalse(partner.title)
|
||||||
|
self.assertFalse(partner.street)
|
||||||
|
|
||||||
|
def test_boolean_field(self):
|
||||||
|
self.question_employee = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Employee",
|
||||||
|
qtype="simple_choice",
|
||||||
|
labels=[
|
||||||
|
{"value": "yes"},
|
||||||
|
{"value": "no"},
|
||||||
|
],
|
||||||
|
survey_id=self.survey.id,
|
||||||
|
sequence=1,
|
||||||
|
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
|
||||||
|
|
||||||
|
employee_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "res.partner"), ("name", "=", "employee")]
|
||||||
|
)
|
||||||
|
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": employee_field.id,
|
||||||
|
"value_origin": "question",
|
||||||
|
"question_id": self.question_employee.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_employee,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value=self.question_employee.suggested_answer_ids[0].id,
|
||||||
|
)
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(partner.employee)
|
||||||
|
|
||||||
|
def test_update_existing_record(self):
|
||||||
|
# A contact with name 'Jean' already exists.
|
||||||
|
# We'll update the email of this partner (and not create a new one)
|
||||||
|
self.env["res.partner"].create({"name": "Jean"})
|
||||||
|
|
||||||
|
self.question_email = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Email",
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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_name, answer=self.answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_email,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value="jean@test.fr",
|
||||||
|
)
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(len(partner) == 1)
|
||||||
|
self.assertTrue(partner.email == "jean@test.fr")
|
||||||
|
|
||||||
|
def test_update_only_empty_fields_when_updating_records(self):
|
||||||
|
# A contact with name 'Jean' and email 'jean@test.fr' already exists.
|
||||||
|
# We'll update the field 'function' of this partner and won't update the email
|
||||||
|
# because it's already filled up
|
||||||
|
self.env["res.partner"].create(
|
||||||
|
{
|
||||||
|
"name": "Jean",
|
||||||
|
"email": "jean@test.fr",
|
||||||
|
# when the survey is submitted, email should not be updated
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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="ThisEmailShouldNotBeUpdated@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.assertTrue(len(partner) == 1)
|
||||||
|
self.assertTrue(partner.email == "jean@test.fr")
|
||||||
|
self.assertTrue(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
|
||||||
|
# it has priority over updating the existing record
|
||||||
|
self.name_survey_record_creation_field_values.unicity_check = True
|
||||||
|
|
||||||
|
self.env["res.partner"].create({"name": "Jean"})
|
||||||
|
|
||||||
|
self.question_email = self._add_question(
|
||||||
|
page=None,
|
||||||
|
name="Email",
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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_name, answer=self.answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_email,
|
||||||
|
answer=self.answer,
|
||||||
|
answer_value="jean@test.fr",
|
||||||
|
)
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
self.assertTrue(len(partner) == 1)
|
||||||
|
self.assertTrue(getattr(partner, "Email", None) is None)
|
||||||
@@ -18,6 +18,16 @@
|
|||||||
<group>
|
<group>
|
||||||
<field name="name" />
|
<field name="name" />
|
||||||
<field name="model_id" />
|
<field name="model_id" />
|
||||||
|
<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)]}"/>
|
||||||
|
<div colspan="2" style="width:100%;">
|
||||||
|
<div class="alert alert-warning"
|
||||||
|
attrs="{'invisible': [('update_existing_records', '=', False)]}">
|
||||||
|
Only the first matched record will be updated.
|
||||||
|
Also to be noticed, the unicity check feature has priority over updating the existing record.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<field name="field_values_ids">
|
<field name="field_values_ids">
|
||||||
<tree>
|
<tree>
|
||||||
<field name="field_id" />
|
<field name="field_id" />
|
||||||
|
|||||||
Reference in New Issue
Block a user