[ADD]urvey_record_generation: manage the case where the other record is a mandatory field

This commit is contained in:
2025-05-14 14:42:18 +02:00
parent 38df9c61d0
commit c4302da556
6 changed files with 35 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ class SurveyRecordCreation(models.Model):
model_id = fields.Many2one('ir.model', "Model", help="Model of generated record")
field_values_ids = fields.One2many('survey.record.creation.field.values', 'survey_record_creation_id', string="Field values")
warning_message = fields.Html('Warning message', compute="_compute_warning_message")
sequence = fields.Integer("sequence")
@api.onchange('model_id')
def clear_field_values_ids(self):

View File

@@ -113,7 +113,7 @@ class SurveyRecordCreationFieldValues(models.Model):
@api.onchange('field_id')
def _onchange_field_id(self):
# clean values
# clean values
self.clean_values()
# Set reference field model and select first record
if self.field_id and self.field_id.ttype == 'many2one' and self.field_id.relation:

View File

@@ -1,5 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models, fields
from odoo import models, fields, _
from odoo.exceptions import UserError
class SurveyUserInput(models.Model):
@@ -37,9 +38,10 @@ class SurveyUserInput(models.Model):
created_records = {}
fields_to_update = []
for record_creation in user_input.survey_id.survey_record_creation_ids:
for record_creation in user_input.survey_id.survey_record_creation_ids.sorted('sequence'):
model = record_creation.model_id.model
vals = {}
ModelClass = self.env[model]
for field_value in record_creation.field_values_ids:
if field_value.value_origin == 'fixed':
@@ -69,7 +71,17 @@ class SurveyUserInput(models.Model):
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