Compare commits
5 Commits
1528e0b7ad
...
18.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 0020f35399 | |||
| bfe59096db | |||
| 9d55588f18 | |||
| 2835e2d2be | |||
| 0948703b4e |
23
survey_dropdown_simple_choice/README.md
Normal file
23
survey_dropdown_simple_choice/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# survey_dropdown_simple_choice
|
||||
|
||||
Ajoute une option « Afficher en liste déroulante » aux questions
|
||||
« Choix multiple : une seule réponse » des sondages : les réponses sont
|
||||
proposées dans une liste déroulante au lieu de boutons radio.
|
||||
|
||||
Pratique pour les longues listes (pays, départements…).
|
||||
|
||||
## Utilisation
|
||||
|
||||
Dans le sondage, éditez la question → onglet **Options** → cochez
|
||||
**« Afficher en liste déroulante »**.
|
||||
|
||||
L'option « Autre » (commentaire) et les questions conditionnelles
|
||||
fonctionnent comme d'habitude.
|
||||
|
||||
## Limites
|
||||
|
||||
- Si une réponse proposée contient une image, la question reste affichée
|
||||
en boutons radio.
|
||||
- À l'impression et dans la revue des réponses (« Consulter vos réponses »),
|
||||
seule la réponse choisie est affichée, ainsi que la bonne réponse pour
|
||||
un sondage noté.
|
||||
1
survey_dropdown_simple_choice/__init__.py
Normal file
1
survey_dropdown_simple_choice/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import models
|
||||
27
survey_dropdown_simple_choice/__manifest__.py
Normal file
27
survey_dropdown_simple_choice/__manifest__.py
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "Survey Dropdown Simple Choice",
|
||||
"summary": "Display some single choice survey questions as a dropdown list",
|
||||
"description": "Adds a boolean on survey.question to display a "
|
||||
"\"Multiple choice: only one answer\" question as a dropdown list "
|
||||
"instead of radio buttons, with support for conditional questions "
|
||||
"and \"Other\" comments.",
|
||||
"author": "Elabore",
|
||||
"website": "https://www.elabore.coop",
|
||||
"category": "Marketing/Surveys",
|
||||
"license": "AGPL-3",
|
||||
"version": "18.0.1.0.0",
|
||||
"depends": ["survey"],
|
||||
"data": [
|
||||
"views/survey_question_views.xml",
|
||||
"views/survey_templates.xml",
|
||||
],
|
||||
"assets": {
|
||||
"survey.survey_assets": [
|
||||
"survey_dropdown_simple_choice/static/src/scss/survey_dropdown.scss",
|
||||
"survey_dropdown_simple_choice/static/src/js/survey_form_dropdown.js",
|
||||
],
|
||||
},
|
||||
"installable": True,
|
||||
"application": False,
|
||||
"auto_install": True,
|
||||
}
|
||||
41
survey_dropdown_simple_choice/i18n/fr.po
Normal file
41
survey_dropdown_simple_choice/i18n/fr.po
Normal file
@@ -0,0 +1,41 @@
|
||||
# Translation of Odoo Server.
|
||||
# This file contains the translation of the following modules:
|
||||
# * survey_dropdown_simple_choice
|
||||
#
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: Odoo Server 18.0\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-10-20 17:08+0000\n"
|
||||
"PO-Revision-Date: 2026-10-20 17:08+0000\n"
|
||||
"Last-Translator: \n"
|
||||
"Language-Team: \n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: \n"
|
||||
"Plural-Forms: \n"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,field_description:survey_dropdown_simple_choice.field_survey_question__display_dropdown
|
||||
msgid "Display as dropdown list"
|
||||
msgstr "Afficher en liste déroulante"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,help:survey_dropdown_simple_choice.field_survey_question__display_dropdown
|
||||
msgid ""
|
||||
"Display this single choice question as a dropdown list instead of radio "
|
||||
"buttons. Ignored if the suggested answers contain images."
|
||||
msgstr ""
|
||||
"Affiche cette question à choix unique sous la forme d'une liste déroulante "
|
||||
"au lieu de boutons radio. Ignoré si les réponses proposées contiennent des "
|
||||
"images."
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,field_description:survey_dropdown_simple_choice.field_survey_question__smart_search
|
||||
msgid "Smart Search"
|
||||
msgstr "Recherche intelligente"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model,name:survey_dropdown_simple_choice.model_survey_question
|
||||
msgid "Survey Question"
|
||||
msgstr "Question du sondage"
|
||||
1
survey_dropdown_simple_choice/models/__init__.py
Normal file
1
survey_dropdown_simple_choice/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import survey_question
|
||||
11
survey_dropdown_simple_choice/models/survey_question.py
Normal file
11
survey_dropdown_simple_choice/models/survey_question.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class SurveyQuestion(models.Model):
|
||||
_inherit = 'survey.question'
|
||||
|
||||
display_dropdown = fields.Boolean(
|
||||
string="Display as dropdown list",
|
||||
help="Display this single choice question as a dropdown list instead "
|
||||
"of radio buttons. Ignored if the suggested answers contain images.",
|
||||
)
|
||||
@@ -0,0 +1,72 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import SurveyFormWidget from "@survey/js/survey_form";
|
||||
|
||||
// The dropdown only drives the native radios (hidden by CSS): choosing an
|
||||
// option clicks the matching radio, so validation, conditional questions,
|
||||
// "Other" comment, auto-submit and AJAX submit are all handled natively.
|
||||
SurveyFormWidget.include({
|
||||
events: Object.assign({}, SurveyFormWidget.prototype.events, {
|
||||
'change select.o_survey_dropdown_choice': '_onDropdownChoiceChange',
|
||||
'change [data-survey-dropdown="edit"] .o_survey_form_choice_item': '_onDropdownRadioChange',
|
||||
}),
|
||||
|
||||
/**
|
||||
* A focused dropdown is handled like a text input: its keys (letters,
|
||||
* arrows, enter) must not trigger the survey shortcuts.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_onKeyDown: function (event) {
|
||||
if (event.target.matches?.('select.o_survey_dropdown_choice')
|
||||
&& !((event.ctrlKey || event.metaKey) && event.key === 'Enter')) {
|
||||
return;
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* Correct answers are colored on the native radios: show them (only the
|
||||
* selected and correct ones, see "review" mode) instead of the dropdown.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_showQuestionAnswer: function (correctAnswers, questionId) {
|
||||
this._super.apply(this, arguments);
|
||||
const group = this.el.querySelector(
|
||||
`.js_question-wrapper[id="${questionId}"] [data-survey-dropdown="edit"]`
|
||||
);
|
||||
if (group) {
|
||||
group.dataset.surveyDropdown = 'review';
|
||||
group.querySelector('.o_survey_dropdown_select').classList.add('d-none');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Click the radio of the chosen option, or the checked one to untick it
|
||||
* when the empty option is chosen (native untick behavior).
|
||||
*/
|
||||
_onDropdownChoiceChange: function (ev) {
|
||||
if (this.readonly) {
|
||||
return;
|
||||
}
|
||||
const select = ev.currentTarget;
|
||||
const group = select.closest('.o_survey_form_choice');
|
||||
const radio = select.value
|
||||
? group.querySelector(`.o_survey_form_choice_item[value="${select.value}"]`)
|
||||
: group.querySelector('.o_survey_form_choice_item:checked');
|
||||
if (radio && !(select.value && radio.checked)) {
|
||||
radio.click();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Keep the dropdown in sync when a radio changes by other means
|
||||
* (A-Z keyboard shortcuts).
|
||||
*/
|
||||
_onDropdownRadioChange: function (ev) {
|
||||
const group = ev.currentTarget.closest('.o_survey_form_choice');
|
||||
const checked = group.querySelector('.o_survey_form_choice_item:checked');
|
||||
group.querySelector('select.o_survey_dropdown_choice').value = checked ? checked.value : '';
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
.o_survey_form {
|
||||
select.o_survey_dropdown_choice {
|
||||
min-height: 44px;
|
||||
|
||||
option {
|
||||
color: initial;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Native radios still carry the answer but are replaced by the dropdown
|
||||
.o_survey_form_choice[data-survey-dropdown="edit"] > div:has(> .o_survey_choice_btn) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
// Review / print / correction: only show the selected and correct answers
|
||||
.o_survey_form_choice[data-survey-dropdown="review"]
|
||||
> div:has(> .o_survey_choice_btn):not(:has(> .o_survey_selected)):not(:has(> .bg-success)) {
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="survey_question_form_dropdown" model="ir.ui.view">
|
||||
<field name="name">survey.question.form.dropdown</field>
|
||||
<field name="model">survey.question</field>
|
||||
<field name="inherit_id" ref="survey.survey_question_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='comments_allowed']" position="before">
|
||||
<field name="display_dropdown" invisible="question_type != 'simple_choice'"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
33
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
33
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<!-- Add a dropdown on top of the native radios when the display_dropdown
|
||||
flag is set. The radios are kept (hidden by CSS) and still carry the
|
||||
answer: the dropdown only clicks them (see survey_form_dropdown.js).
|
||||
In readonly mode (review / print), the radios are shown but only the
|
||||
selected and correct answers are kept visible (data-survey-dropdown
|
||||
"review"). The native rendering is kept when a suggestion carries an
|
||||
image. -->
|
||||
<template id="question_simple_choice" inherit_id="survey.question_simple_choice" name="Question: simple choice (dropdown)">
|
||||
<xpath expr="//t[@t-set='comment_line']" position="after">
|
||||
<t t-set="dropdown_enabled" t-value="question.display_dropdown and not any(a.value_image for a in question.suggested_answer_ids)"/>
|
||||
<t t-set="use_dropdown" t-value="dropdown_enabled and not survey_form_readonly"/>
|
||||
</xpath>
|
||||
<xpath expr="//div[hasclass('o_survey_form_choice')]" position="attributes">
|
||||
<attribute name="t-att-data-survey-dropdown">('review' if survey_form_readonly else 'edit') if dropdown_enabled else None</attribute>
|
||||
</xpath>
|
||||
<xpath expr="//t[@t-set='has_correct_answer']" position="after">
|
||||
<div t-if="use_dropdown" class="col-sm-12 o_survey_dropdown_select">
|
||||
<select class="form-select o_survey_dropdown_choice" t-att-aria-label="question.title">
|
||||
<option value=""/>
|
||||
<option t-foreach="question.suggested_answer_ids" t-as="label"
|
||||
t-att-value="label.id"
|
||||
t-att-selected="'selected' if answer_line.suggested_answer_id == label else None"
|
||||
t-out="label.value"/>
|
||||
<option t-if="question.comments_allowed and question.comment_count_as_answer" value="-1"
|
||||
t-att-selected="'selected' if comment_line else None"
|
||||
t-out="question.comments_message or default_comments_message"/>
|
||||
</select>
|
||||
</div>
|
||||
</xpath>
|
||||
</template>
|
||||
</odoo>
|
||||
@@ -11,7 +11,7 @@ Allow to create record of any model when sending the form :
|
||||
* Associate question with fields
|
||||
* For x2m fields : Associate values to questions
|
||||
""",
|
||||
"version": "18.0.1.0.0",
|
||||
"version": "18.0.1.0.2",
|
||||
"license": "AGPL-3",
|
||||
"author": "Elabore",
|
||||
"website": "https://www.elabore.coop",
|
||||
|
||||
159
survey_record_generation/migrations/18.0.1.0.1/end-migration.py
Normal file
159
survey_record_generation/migrations/18.0.1.0.1/end-migration.py
Normal file
@@ -0,0 +1,159 @@
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
"""Relink survey.user_input.partner_id/email to the contact that actually
|
||||
matches each participant's own answers.
|
||||
|
||||
Before this version, survey.user_input.partner_id/email were only updated
|
||||
when _mark_done() created a *new* res.partner. If the answer already had a
|
||||
partner_id/email (e.g. inherited from the Odoo user who was logged in when
|
||||
the /survey/start link was opened, or from some other unidentified cause),
|
||||
_mark_done() silently kept that value even when the survey_record_creation
|
||||
config found or created a different, correct contact from the participant's
|
||||
own answers. This backfill re-runs that resolution for every already-done
|
||||
answer and fixes partner_id/email accordingly.
|
||||
|
||||
It never re-triggers the other survey.user_input._mark_done() overrides
|
||||
(crm lead / event registration / notification modules, ...): it only calls
|
||||
the res.partner-matching helpers directly, so it can't create duplicate
|
||||
leads, registrations, etc. for historical submissions.
|
||||
|
||||
A res.partner is expected to already exist for every done submission (it
|
||||
was necessarily found or created the first time _mark_done() ran), so this
|
||||
backfill never creates one. It first looks for the survey.generated.record
|
||||
row _mark_done() logged when it created that partner, and falls back to
|
||||
re-deriving the match through find_existing_record()/
|
||||
find_duplicate_if_there_are_fields_with_unicity_check() when there is no
|
||||
such row (existing partner matched instead of created) or it points to a
|
||||
partner that has since been deleted (e.g. merged into another contact). If
|
||||
neither approach finds anything, the record is left untouched and logged
|
||||
for manual review instead.
|
||||
"""
|
||||
import logging
|
||||
|
||||
from odoo import SUPERUSER_ID, api
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def migrate(cr, version):
|
||||
env = api.Environment(cr, SUPERUSER_ID, {})
|
||||
user_input_model = env["survey.user_input"]
|
||||
|
||||
surveys_with_partner_creation = env["survey.record.creation"].search(
|
||||
[("model_id.model", "=", "res.partner")]
|
||||
).survey_id
|
||||
|
||||
user_inputs = user_input_model.search(
|
||||
[
|
||||
("survey_id", "in", surveys_with_partner_creation.ids),
|
||||
("state", "=", "done"),
|
||||
]
|
||||
)
|
||||
|
||||
_logger.info(
|
||||
"survey_record_generation: relinking partner_id/email on %d done "
|
||||
"survey.user_input records",
|
||||
len(user_inputs),
|
||||
)
|
||||
|
||||
fixed_count = 0
|
||||
not_found_count = 0
|
||||
|
||||
for user_input in user_inputs:
|
||||
record_creations = user_input.survey_id.survey_record_creation_ids.filtered(
|
||||
lambda rc: rc.model_id.model == "res.partner"
|
||||
).sorted("sequence")
|
||||
|
||||
record_creation = record_creations[:1]
|
||||
if not record_creation:
|
||||
continue
|
||||
|
||||
# 1) Prefer the res.partner this very submission created, if any
|
||||
# (the first one, by id, in the rare case there is more than one):
|
||||
# it's the exact record _mark_done() produced for this answer, no
|
||||
# guessing involved. This is also the only option for surveys whose
|
||||
# record creation has neither update_existing_records nor any
|
||||
# unicity_check field configured, since find_existing_record()/
|
||||
# find_duplicate...() can then never find anything (nothing to
|
||||
# search on).
|
||||
record = False
|
||||
for generated in user_input.generated_record_ids.sorted("id"):
|
||||
if (
|
||||
generated.survey_record_creation_id == record_creation
|
||||
and generated.created_record_id
|
||||
and generated.created_record_id._name == "res.partner"
|
||||
and generated.created_record_id.exists()
|
||||
):
|
||||
# The referenced partner may have since been deleted (e.g.
|
||||
# merged into another contact): in that case it's not usable
|
||||
# and we fall through to the search-based lookup below.
|
||||
record = generated.created_record_id
|
||||
break
|
||||
|
||||
# 2) Otherwise, this submission matched an already-existing partner
|
||||
# instead of creating one (find_existing_record()/find_duplicate...()
|
||||
# branch of _mark_done()): re-derive it the same way.
|
||||
if not record:
|
||||
# Only compute the fields find_existing_record()/find_duplicate...()
|
||||
# actually read (the search field, and any unicity_check field),
|
||||
# not every field of the record creation: other fields (e.g. a
|
||||
# "record" reference to a model defined in a module that depends
|
||||
# on this one) may not be loadable yet at this point of the
|
||||
# upgrade, and are useless here anyway since this backfill never
|
||||
# writes to res.partner.
|
||||
needed_field_names = set()
|
||||
if (
|
||||
record_creation.update_existing_records
|
||||
and record_creation.field_to_retrieve_existing_records
|
||||
):
|
||||
needed_field_names.add(
|
||||
record_creation.field_to_retrieve_existing_records.name
|
||||
)
|
||||
unicity_field_values = record_creation.field_values_ids.filtered(
|
||||
lambda field_value: field_value.unicity_check
|
||||
)
|
||||
needed_field_names.update(unicity_field_values.mapped("field_id.name"))
|
||||
|
||||
vals = {}
|
||||
for field_value in record_creation.field_values_ids:
|
||||
if field_value.field_id.name not in needed_field_names:
|
||||
continue
|
||||
value, __ = user_input_model.get_value_based_on_value_origin(
|
||||
field_value=field_value,
|
||||
user_input=user_input,
|
||||
created_records={},
|
||||
model="res.partner",
|
||||
other_record_fields_to_update=[],
|
||||
)
|
||||
vals[field_value.field_id.name] = value
|
||||
|
||||
existing_record = user_input_model.find_existing_record(
|
||||
record_creation, vals
|
||||
)
|
||||
duplicate = (
|
||||
user_input_model.find_duplicate_if_there_are_fields_with_unicity_check(
|
||||
"res.partner", record_creation, vals
|
||||
)
|
||||
)
|
||||
record = duplicate or existing_record
|
||||
|
||||
if not record:
|
||||
_logger.warning(
|
||||
"survey_record_generation: no existing res.partner found for "
|
||||
"survey.user_input %s while backfilling partner_id/email, "
|
||||
"leaving it untouched",
|
||||
user_input.id,
|
||||
)
|
||||
not_found_count += 1
|
||||
continue
|
||||
|
||||
if user_input.partner_id != record or user_input.email != record.email:
|
||||
user_input.partner_id = record.id
|
||||
user_input.email = record.email
|
||||
fixed_count += 1
|
||||
|
||||
_logger.info(
|
||||
"survey_record_generation: fixed %d survey.user_input records "
|
||||
"(%d without a matching res.partner)",
|
||||
fixed_count,
|
||||
not_found_count,
|
||||
)
|
||||
@@ -39,6 +39,7 @@ class SurveyUserInput(models.Model):
|
||||
for user_input in self:
|
||||
created_records = {}
|
||||
other_record_fields_to_update: list[SurveyRecordCreationFieldValues] = []
|
||||
partner_linked_in_this_run = False
|
||||
|
||||
record_creation: SurveyRecordCreation
|
||||
for (
|
||||
@@ -60,6 +61,11 @@ class SurveyUserInput(models.Model):
|
||||
)
|
||||
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
|
||||
|
||||
existing_record = self.find_existing_record(record_creation, vals)
|
||||
@@ -85,8 +91,6 @@ class SurveyUserInput(models.Model):
|
||||
try:
|
||||
with self.env.cr.savepoint():
|
||||
record = self.env[model].create(vals)
|
||||
if model == "res.partner" and not self.partner_id:
|
||||
self.partner_id = record.id
|
||||
except Exception:
|
||||
# This a broad exception because it could be IntegrityError,
|
||||
# EmptyNamesError in case partner_firstname is installed etc...
|
||||
@@ -103,6 +107,17 @@ class SurveyUserInput(models.Model):
|
||||
}
|
||||
)
|
||||
|
||||
if model == "res.partner" and not partner_linked_in_this_run:
|
||||
# Always reflect the partner actually matched/created from
|
||||
# this participant's own answers, even if partner_id/email
|
||||
# were already set on the answer (e.g. inherited from the
|
||||
# logged-in user when the survey link was opened). Only the
|
||||
# first res.partner record creation of this run wins, in
|
||||
# case several are configured on the same survey.
|
||||
user_input.partner_id = record.id
|
||||
user_input.email = record.email
|
||||
partner_linked_in_this_run = True
|
||||
|
||||
created_records[record_creation.id] = record
|
||||
|
||||
# update linked record
|
||||
@@ -150,8 +165,9 @@ class SurveyUserInput(models.Model):
|
||||
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(
|
||||
# if the other record is already created, get it
|
||||
# (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
|
||||
)
|
||||
# otherwise, we update the record later (out of this for loop)
|
||||
@@ -184,39 +200,34 @@ class SurveyUserInput(models.Model):
|
||||
if unique_fields:
|
||||
uniq_domain = []
|
||||
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)
|
||||
return duplicate
|
||||
|
||||
def get_required_value_from_other_record(
|
||||
def get_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,
|
||||
}
|
||||
linked_record = created_records.get(field_value.other_created_record_id.id)
|
||||
if linked_record:
|
||||
# The other record is already created: link it right away.
|
||||
return self.get_value_for_relational_field(field_value, linked_record)
|
||||
if self.env[model]._fields[field_value.field_id.name].required:
|
||||
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,
|
||||
}
|
||||
)
|
||||
return None
|
||||
|
||||
def get_value_from_user_answer(
|
||||
self,
|
||||
@@ -315,28 +326,44 @@ class SurveyUserInput(models.Model):
|
||||
}
|
||||
)
|
||||
|
||||
@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,
|
||||
}
|
||||
|
||||
def _get_boolean_true_values(self):
|
||||
"""Tokens interpreted as true, normalized to lowercase.
|
||||
|
||||
Instance method so that a third-party module or a localization can
|
||||
extend the list without rewriting get_boolean_value.
|
||||
"""
|
||||
return {"1", "true", "vrai", "yes", "y", "oui", "o", "on", "x"}
|
||||
|
||||
def _get_boolean_false_values(self):
|
||||
"""Tokens interpreted as false, normalized to lowercase."""
|
||||
return {"0", "false", "faux", "no", "n", "non", "off", ""}
|
||||
|
||||
def get_boolean_value(self, answer_value_char: str, question_title: str) -> bool:
|
||||
"""Convert the technical value of an answer into a boolean.
|
||||
|
||||
Allows a boolean field to be filled from a "simple_choice" question
|
||||
whose suggested answers carry yes/no values.
|
||||
"""
|
||||
if not answer_value_char:
|
||||
# Empty answer: an unset boolean is false, this is not a
|
||||
# configuration error.
|
||||
return False
|
||||
|
||||
token = str(answer_value_char).strip().casefold()
|
||||
|
||||
if token in self._get_boolean_true_values():
|
||||
return True
|
||||
if token in self._get_boolean_false_values():
|
||||
return False
|
||||
|
||||
raise UserError(
|
||||
_(
|
||||
"[Survey record generation] The boolean value %(value)s is not "
|
||||
"supported (for question %(question)s)"
|
||||
)
|
||||
% {
|
||||
"value": answer_value_char,
|
||||
"question": question_title,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
from . import test_survey_record_creation
|
||||
from . import test_end_migration_18_0_1_0_1
|
||||
from . import test_computed_fields
|
||||
|
||||
33
survey_record_generation/tests/fake_models.py
Normal file
33
survey_record_generation/tests/fake_models.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class SurveyRecordGenerationFakeParent(models.Model):
|
||||
_name = "survey.record.generation.fake.parent"
|
||||
_description = "Fake parent record (tests only)"
|
||||
|
||||
name = fields.Char()
|
||||
price = fields.Float()
|
||||
|
||||
|
||||
class SurveyRecordGenerationFakeLine(models.Model):
|
||||
"""Line whose price is computed from its parent, like the price of a
|
||||
subscription line (subscription_oca).
|
||||
|
||||
Pattern reproduced: the survey creates a parent, then a line linked to
|
||||
it, with a price answered by the user. The user's price must be kept,
|
||||
the parent's price only applies when the user gave none.
|
||||
"""
|
||||
|
||||
_name = "survey.record.generation.fake.line"
|
||||
_description = "Fake line record (tests only)"
|
||||
|
||||
parent_id = fields.Many2one("survey.record.generation.fake.parent")
|
||||
price_unit = fields.Float(
|
||||
compute="_compute_price_unit", store=True, readonly=False
|
||||
)
|
||||
|
||||
@api.depends("parent_id.price")
|
||||
def _compute_price_unit(self):
|
||||
for line in self:
|
||||
if line.parent_id:
|
||||
line.price_unit = line.parent_id.price
|
||||
120
survey_record_generation/tests/test_computed_fields.py
Normal file
120
survey_record_generation/tests/test_computed_fields.py
Normal file
@@ -0,0 +1,120 @@
|
||||
from odoo_test_helper import FakeModelLoader
|
||||
|
||||
from odoo.addons.survey.tests.common import SurveyCase
|
||||
|
||||
|
||||
class TestComputedFields(SurveyCase):
|
||||
"""User's answers must not be overridden by computed fields depending on
|
||||
a link to another generated record.
|
||||
|
||||
Use case: a membership form creating a subscription and a subscription
|
||||
line whose price is typed by the participant (free amount), while the
|
||||
line price is computed from the subscription.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.loader = FakeModelLoader(cls.env, cls.__module__)
|
||||
cls.loader.backup_registry()
|
||||
from .fake_models import (
|
||||
SurveyRecordGenerationFakeLine,
|
||||
SurveyRecordGenerationFakeParent,
|
||||
)
|
||||
|
||||
cls.loader.update_registry(
|
||||
(SurveyRecordGenerationFakeParent, SurveyRecordGenerationFakeLine)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.loader.restore_registry()
|
||||
super().tearDownClass()
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.survey = self.env["survey.survey"].create({"title": "Join us"})
|
||||
self.question_amount = self._add_question(
|
||||
page=None,
|
||||
name="Free amount",
|
||||
qtype="numerical_box",
|
||||
survey_id=self.survey.id,
|
||||
sequence=1,
|
||||
)
|
||||
|
||||
# Parent, with a default price of 20
|
||||
self.parent_creation = self._add_record_creation(
|
||||
"Parent", "survey.record.generation.fake.parent", 0
|
||||
)
|
||||
self._add_field_value(
|
||||
self.parent_creation, "name", value_origin="fixed",
|
||||
fixed_value_char="Membership",
|
||||
)
|
||||
self._add_field_value(
|
||||
self.parent_creation, "price", value_origin="fixed",
|
||||
fixed_value_float=20.0,
|
||||
)
|
||||
|
||||
# Line, linked to the parent, with the price answered by the user
|
||||
self.line_creation = self._add_record_creation(
|
||||
"Line", "survey.record.generation.fake.line", 1
|
||||
)
|
||||
self._add_field_value(
|
||||
self.line_creation, "parent_id", value_origin="other_record",
|
||||
other_created_record_id=self.parent_creation.id,
|
||||
)
|
||||
self._add_field_value(
|
||||
self.line_creation, "price_unit", value_origin="question",
|
||||
question_id=self.question_amount.id,
|
||||
)
|
||||
|
||||
def _add_record_creation(self, name, model, sequence):
|
||||
return self.env["survey.record.creation"].create(
|
||||
{
|
||||
"name": name,
|
||||
"survey_id": self.survey.id,
|
||||
"model_id": self.env["ir.model"]._get(model).id,
|
||||
"sequence": sequence,
|
||||
}
|
||||
)
|
||||
|
||||
def _add_field_value(self, record_creation, field_name, **vals):
|
||||
field = self.env["ir.model.fields"]._get(
|
||||
record_creation.model_id.model, field_name
|
||||
)
|
||||
return self.env["survey.record.creation.field.values"].create(
|
||||
{
|
||||
"survey_record_creation_id": record_creation.id,
|
||||
"survey_id": self.survey.id,
|
||||
"model_id": record_creation.model_id.id,
|
||||
"field_id": field.id,
|
||||
**vals,
|
||||
}
|
||||
)
|
||||
|
||||
def _submit(self, amount=None):
|
||||
answer = self._add_answer(
|
||||
survey=self.survey, partner=False, email="jean@test.fr"
|
||||
)
|
||||
if amount is not None:
|
||||
self._add_answer_line(
|
||||
question=self.question_amount, answer=answer, answer_value=amount
|
||||
)
|
||||
answer._mark_done()
|
||||
return self.env["survey.record.generation.fake.line"].search(
|
||||
[("parent_id.name", "=", "Membership")]
|
||||
)
|
||||
|
||||
def test_answer_is_not_overridden_by_computed_field(self):
|
||||
line = self._submit(amount=33)
|
||||
|
||||
self.assertEqual(len(line), 1)
|
||||
self.assertEqual(line.price_unit, 33.0)
|
||||
|
||||
def test_computed_field_applies_when_question_is_not_displayed(self):
|
||||
# E.g. the "free amount" question is only displayed when the user
|
||||
# chose "Other amount": otherwise the line keeps the computed price.
|
||||
line = self._submit()
|
||||
|
||||
self.assertEqual(len(line), 1)
|
||||
self.assertEqual(line.price_unit, 20.0)
|
||||
156
survey_record_generation/tests/test_end_migration_18_0_1_0_1.py
Normal file
156
survey_record_generation/tests/test_end_migration_18_0_1_0_1.py
Normal file
@@ -0,0 +1,156 @@
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
import importlib.util
|
||||
import os
|
||||
|
||||
from odoo.addons.survey.tests.common import SurveyCase
|
||||
|
||||
|
||||
def _load_end_migration():
|
||||
migration_path = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
"migrations",
|
||||
"18.0.1.0.1",
|
||||
"end-migration.py",
|
||||
)
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"survey_record_generation_end_migration_18_0_1_0_1", migration_path
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
class TestEndMigration180101(SurveyCase):
|
||||
"""Covers the use cases the 18.0.1.0.1 end-migration backfill must
|
||||
handle: relinking survey.user_input.partner_id/email to the contact
|
||||
that actually matches each participant's own answers, for submissions
|
||||
whose partner_id/email were left wrong before the fix in this version.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.migration = _load_end_migration()
|
||||
|
||||
self.survey = self.env["survey.survey"].create({"title": "Test Survey"})
|
||||
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")
|
||||
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")]
|
||||
)
|
||||
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 _submit_answer(self, name):
|
||||
answer = self._add_answer(survey=self.survey, partner=False, email=False)
|
||||
self._add_answer_line(
|
||||
question=self.question_name, answer=answer, answer_value=name
|
||||
)
|
||||
answer._mark_done()
|
||||
return answer
|
||||
|
||||
def _corrupt_with_wrong_partner(self, answer):
|
||||
# Simulate a pre-fix record: partner_id/email point to someone
|
||||
# unrelated to this participant's own answers.
|
||||
wrong_partner = self.env["res.partner"].create({"name": "Wrong Partner"})
|
||||
answer.write({"partner_id": wrong_partner.id, "email": "wrong@test.fr"})
|
||||
return wrong_partner
|
||||
|
||||
def test_migrate_uses_generated_record_when_available(self):
|
||||
# The submission created its own res.partner: the migration must
|
||||
# relink partner_id/email to that exact contact.
|
||||
answer = self._submit_answer("Jean")
|
||||
jean = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
self._corrupt_with_wrong_partner(answer)
|
||||
|
||||
self.migration.migrate(self.cr, "18.0.1.0.0")
|
||||
answer.invalidate_recordset()
|
||||
|
||||
self.assertEqual(answer.partner_id, jean)
|
||||
self.assertEqual(answer.email, jean.email)
|
||||
|
||||
def test_migrate_falls_back_to_search_when_no_generated_record(self):
|
||||
# The submission matched an already-existing partner instead of
|
||||
# creating one: no survey.generated.record row exists for it, so the
|
||||
# migration must fall back to re-deriving the match through search.
|
||||
jean = self.env["res.partner"].create(
|
||||
{"name": "Jean", "email": "jean@test.fr"}
|
||||
)
|
||||
self.survey_record_creation.write(
|
||||
{
|
||||
"update_existing_records": True,
|
||||
"field_to_retrieve_existing_records": self.name_field.id,
|
||||
}
|
||||
)
|
||||
|
||||
answer = self._submit_answer("Jean")
|
||||
self.assertFalse(answer.generated_record_ids)
|
||||
self._corrupt_with_wrong_partner(answer)
|
||||
|
||||
self.migration.migrate(self.cr, "18.0.1.0.0")
|
||||
answer.invalidate_recordset()
|
||||
|
||||
self.assertEqual(answer.partner_id, jean)
|
||||
self.assertEqual(answer.email, jean.email)
|
||||
|
||||
def test_migrate_falls_back_to_search_when_generated_partner_was_deleted(self):
|
||||
# The res.partner the submission created has since been deleted
|
||||
# (e.g. merged into another contact): the migration must not crash
|
||||
# on the stale reference and must fall back to search instead.
|
||||
self.survey_record_creation.write(
|
||||
{
|
||||
"update_existing_records": True,
|
||||
"field_to_retrieve_existing_records": self.name_field.id,
|
||||
}
|
||||
)
|
||||
|
||||
answer = self._submit_answer("Jean")
|
||||
first_jean = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
first_jean.unlink()
|
||||
|
||||
# A new contact with the same name takes over.
|
||||
second_jean = self.env["res.partner"].create(
|
||||
{"name": "Jean", "email": "jean2@test.fr"}
|
||||
)
|
||||
self._corrupt_with_wrong_partner(answer)
|
||||
|
||||
self.migration.migrate(self.cr, "18.0.1.0.0")
|
||||
answer.invalidate_recordset()
|
||||
|
||||
self.assertEqual(answer.partner_id, second_jean)
|
||||
self.assertEqual(answer.email, second_jean.email)
|
||||
|
||||
def test_migrate_leaves_answer_untouched_when_no_partner_can_be_found(self):
|
||||
# No survey.generated.record row (e.g. historical data predating
|
||||
# that log) and no way to search (neither update_existing_records
|
||||
# nor unicity_check configured): the migration must leave
|
||||
# partner_id/email as they are instead of guessing or crashing.
|
||||
answer = self._submit_answer("Jean")
|
||||
answer.generated_record_ids.unlink()
|
||||
wrong_partner = self._corrupt_with_wrong_partner(answer)
|
||||
|
||||
self.migration.migrate(self.cr, "18.0.1.0.0")
|
||||
answer.invalidate_recordset()
|
||||
|
||||
self.assertEqual(answer.partner_id, wrong_partner)
|
||||
self.assertEqual(answer.email, "wrong@test.fr")
|
||||
@@ -906,6 +906,71 @@ class TestSurveyRecordCreation(SurveyCase):
|
||||
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
self.assertEqual(self.answer.partner_id, partner)
|
||||
|
||||
def test_partner_id_and_email_are_corrected_when_creating_a_new_partner(self):
|
||||
# A wrong partner_id/email is already set on the answer (e.g.
|
||||
# inherited from the Odoo user who was logged in when the
|
||||
# /survey/start link was opened). The record creation must still
|
||||
# fill partner_id/email with the contact actually created from the
|
||||
# participant's own answers, overriding that wrong value.
|
||||
wrong_partner = self.env["res.partner"].create({"name": "Wrong Partner"})
|
||||
|
||||
self.answer = self._add_answer(
|
||||
survey=self.survey, partner=wrong_partner, email="wrong@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(self.answer.partner_id, partner)
|
||||
self.assertNotEqual(self.answer.partner_id, wrong_partner)
|
||||
self.assertEqual(self.answer.email, partner.email)
|
||||
|
||||
def test_partner_id_and_email_are_corrected_when_matching_an_existing_partner(self):
|
||||
# Same as above, but this time the record creation matches an
|
||||
# already-existing partner (via update_existing_records) instead of
|
||||
# creating a new one: partner_id/email must still be corrected to
|
||||
# that matched partner, not left as the wrong pre-existing value.
|
||||
jean = self.env["res.partner"].create({"name": "Jean", "email": "jean@test.fr"})
|
||||
wrong_partner = self.env["res.partner"].create({"name": "Wrong Partner"})
|
||||
|
||||
self.survey_record_creation.write(
|
||||
{
|
||||
"update_existing_records": True,
|
||||
"field_to_retrieve_existing_records": self.name_field.id,
|
||||
}
|
||||
)
|
||||
|
||||
self.answer = self._add_answer(
|
||||
survey=self.survey, partner=wrong_partner, email="wrong@test.fr"
|
||||
)
|
||||
self._add_answer_line(
|
||||
question=self.question_name, answer=self.answer, answer_value="Jean"
|
||||
)
|
||||
self.answer._mark_done()
|
||||
|
||||
self.assertEqual(self.answer.partner_id, jean)
|
||||
self.assertEqual(self.answer.email, jean.email)
|
||||
|
||||
def test_partner_id_and_email_are_corrected_when_matching_a_duplicate(self):
|
||||
# Same as above, but through the unicity_check/find_duplicate branch
|
||||
# instead of update_existing_records.
|
||||
self.name_survey_record_creation_field_values.unicity_check = True
|
||||
jean = self.env["res.partner"].create({"name": "Jean", "email": "jean@test.fr"})
|
||||
wrong_partner = self.env["res.partner"].create({"name": "Wrong Partner"})
|
||||
|
||||
self.answer = self._add_answer(
|
||||
survey=self.survey, partner=wrong_partner, email="wrong@test.fr"
|
||||
)
|
||||
self._add_answer_line(
|
||||
question=self.question_name, answer=self.answer, answer_value="Jean"
|
||||
)
|
||||
self.answer._mark_done()
|
||||
|
||||
self.assertEqual(self.answer.partner_id, jean)
|
||||
self.assertEqual(self.answer.email, jean.email)
|
||||
|
||||
def test_partner_id_in_survey_input_is_filled_up_by_first_contact_record_creation(self):
|
||||
# In this test, we verify that when creating several contacts with the same survey,
|
||||
# the 1st created contact is used to fill up survey_input.partner_id
|
||||
@@ -952,3 +1017,137 @@ class TestSurveyRecordCreation(SurveyCase):
|
||||
|
||||
partner = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
self.assertEqual(self.answer.partner_id, partner)
|
||||
|
||||
def test_partner_id_is_not_overridden_by_second_contact_record_creation(self):
|
||||
# When several res.partner record creations run on the same survey,
|
||||
# partner_id/email must be corrected to match the first contact
|
||||
# created, and the second contact created must not override that
|
||||
# choice, even though the answer started with a wrong
|
||||
# partner_id/email.
|
||||
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": "Contact 2",
|
||||
"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,
|
||||
}
|
||||
)
|
||||
|
||||
wrong_partner = self.env["res.partner"].create({"name": "Wrong Partner"})
|
||||
|
||||
self.answer = self._add_answer(
|
||||
survey=self.survey, partner=wrong_partner, email="wrong@test.fr"
|
||||
)
|
||||
self._add_answer_line(
|
||||
question=self.question_name,
|
||||
answer=self.answer,
|
||||
answer_value="Jean",
|
||||
)
|
||||
self._add_answer_line(
|
||||
question=self.second_question_name,
|
||||
answer=self.answer,
|
||||
answer_value="Jeanne",
|
||||
)
|
||||
self.answer._mark_done()
|
||||
|
||||
jean = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||
jeanne = self.env["res.partner"].search([("name", "=", "Jeanne")])
|
||||
self.assertEqual(self.answer.partner_id, jean)
|
||||
self.assertNotEqual(self.answer.partner_id, jeanne)
|
||||
self.assertEqual(self.answer.email, jean.email)
|
||||
|
||||
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