[IMP] survey_record_generation: manage filling of answers

This commit is contained in:
clementthomas
2025-02-24 18:04:15 +01:00
parent 2acd034934
commit f2da40afeb
2 changed files with 33 additions and 4 deletions

View File

@@ -1,7 +1,8 @@
import logging import logging
import ast
from odoo import api, fields, models, _ 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__)
@@ -11,10 +12,13 @@ class SurveyQuestion(models.Model):
_inherit = 'survey.question' _inherit = 'survey.question'
model_id = fields.Many2one('ir.model', string="Model") model_id = fields.Many2one('ir.model', string="Model")
model_name = fields.Char(related="model_id.model")
fill_domain = fields.Char("Domain", default="[]")
answer_values_type = fields.Selection([('no', 'No values'),('value','Value'),('record','Record')], string="Associate value to answer", default="no", required=True) answer_values_type = fields.Selection([('no', 'No values'),('value','Value'),('record','Record')], string="Associate value to answer", default="no", required=True)
@api.onchange('model_id') @api.onchange('model_id')
def onchange_model_id(self): def onchange_model_id(self):
self.fill_domain = "[]"
if self.model_id: if self.model_id:
rec = self.env[self.model_id.model].search([], limit=1) rec = self.env[self.model_id.model].search([], limit=1)
if not rec: if not rec:
@@ -23,3 +27,21 @@ class SurveyQuestion(models.Model):
for answer in self.suggested_answer_ids: for answer in self.suggested_answer_ids:
answer.record_id = f"{self.model_id.model},{rec.id}" answer.record_id = f"{self.model_id.model},{rec.id}"
def fill(self):
for question in self:
if question.model_id:
new_suggested_answer_ids = [Command.clear()]
record_model = question.model_id.model
if question.fill_domain:
domain = ast.literal_eval(question.fill_domain)
else:
domain = []
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

View File

@@ -8,6 +8,13 @@
<xpath expr="//field[@name='suggested_answer_ids']" position="before"> <xpath expr="//field[@name='suggested_answer_ids']" position="before">
<field name="answer_values_type" /> <field name="answer_values_type" />
<field name="model_id" attrs="{'invisible':[('answer_values_type','!=','record')]}" /> <field name="model_id" attrs="{'invisible':[('answer_values_type','!=','record')]}" />
<field name="model_name" invisible="1" />
<field name="fill_domain" widget="domain" options="{'model': 'model_name'}" attrs="{'invisible':['|',('answer_values_type','!=','record'),('model_id','=',False)]}" />
<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"
attrs="{'invisible':[('answer_values_type','!=','record')]}" />
</xpath> </xpath>
<xpath expr="//field[@name='suggested_answer_ids']" position="attributes"> <xpath expr="//field[@name='suggested_answer_ids']" position="attributes">
<attribute <attribute