[FIX]survey_record_generation
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m34s

This commit is contained in:
2026-04-23 16:52:17 +02:00
parent aff1a6caae
commit ab1ba7dbb6
4 changed files with 40 additions and 28 deletions

View File

@@ -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.1",
"license": "AGPL-3",
"author": "Elabore",
"website": "https://www.elabore.coop",

View File

@@ -32,8 +32,9 @@ class SurveyQuestion(models.Model):
def fill(self):
for question in self:
if question.model_id:
new_suggested_answer_ids = [Command.clear()]
if question.suggested_answer_ids:
question.suggested_answer_ids = [Command.clear()]
elif question.model_id:
record_model = question.model_id.model
if question.fill_domain:
@@ -43,7 +44,11 @@ class SurveyQuestion(models.Model):
records = self.env[record_model].search(domain)
new_suggested_answer_ids += [Command.create({'value':record.display_name, 'record_id':f"{record_model},{record.id}"
}) for record in records]
question.suggested_answer_ids = new_suggested_answer_ids
question.suggested_answer_ids = [
Command.create({
'value': record.display_name,
'record_id': f"{record_model},{record.id}",
})
for record in records
]

View File

@@ -5,9 +5,6 @@ from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools.misc import format_date
_logger = logging.getLogger(__name__)
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"],
"text": ["char_box", "date", "simple_choice"],
@@ -78,20 +75,28 @@ class SurveyRecordCreationFieldValues(models.Model):
record.field_help = field_help
@api.depends('field_id')
@api.depends('field_id', 'survey_record_creation_id.survey_id')
def _compute_allowed_question_ids(self):
for record_creation_field_values in self:
if not record_creation_field_values.survey_id or not record_creation_field_values.field_id:
record_creation_field_values.allowed_question_ids = None
for record in self:
survey = record.survey_id or record.survey_record_creation_id.survey_id
if not survey or not record.field_id:
record.allowed_question_ids = False
continue
question_domain = [('survey_id','=',record_creation_field_values.survey_id.id)]
if record_creation_field_values.field_id.ttype in ['many2one','many2many']:
question_domain.extend(['|','&',('answer_values_type','=','record'),('model_id','=',record_creation_field_values.field_id.relation),('answer_values_type','=','value')])
if record_creation_field_values.field_id.ttype in type_mapping:
question_domain.append(('question_type','in',type_mapping[record_creation_field_values.field_id.ttype]))
question_domain = [('survey_id', '=', survey.id)]
record_creation_field_values.allowed_question_ids = self.env['survey.question'].search(question_domain)
if record.field_id.ttype in ['many2one', 'many2many']:
question_domain.extend([
'|', '&',
('answer_values_type', '=', 'record'),
('model_id', '=', record.field_id.relation),
('answer_values_type', '=', 'value'),
])
if record.field_id.ttype in type_mapping:
question_domain.append(('question_type', 'in', type_mapping[record.field_id.ttype]))
questions = self.env['survey.question'].search(question_domain)
record.allowed_question_ids = questions
@api.model

View File

@@ -6,15 +6,17 @@
<field name="inherit_id" ref="survey.survey_question_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='suggested_answer_ids']" position="before">
<field name="answer_values_type" />
<field name="model_id" invisible="answer_values_type != 'record'" />
<field name="model_name" invisible="1" />
<field name="fill_domain" widget="domain" options="{'model': 'model_name'}" invisible="answer_values_type != 'record' or not model_id" />
<button name="fill" string="Empty and fill"
type="object"
colspan="2"
help="Empty the list and fill it with all items of selected model matching domain"
invisible="answer_values_type != 'record'" />
<group invisible="question_type not in ['simple_choice', 'multiple_choice', 'matrix']">
<field name="answer_values_type" />
<field name="model_id" invisible="answer_values_type != 'record'" />
<field name="model_name" invisible="1" />
<field name="fill_domain" widget="domain" options="{'model': 'model_name'}" invisible="answer_values_type != 'record' or not model_id" />
<button name="fill" string="Empty and fill"
type="object"
colspan="2"
help="Empty the list and fill it with all items of selected model matching domain"
invisible="answer_values_type != 'record'" />
</group>
</xpath>
<xpath expr="//field[@name='suggested_answer_ids']" position="attributes">
<attribute