[IMP]survey_record_generation:show alerte when deleting a crm tag used in lead record creation
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m42s
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m42s
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from . import base
|
||||
from . import survey_question_answer
|
||||
from . import survey_question
|
||||
from . import survey_record_creation_field_values
|
||||
|
||||
65
survey_record_generation/models/base.py
Normal file
65
survey_record_generation/models/base.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from odoo import models, _
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class Base(models.AbstractModel):
|
||||
_inherit = 'base'
|
||||
|
||||
def unlink(self):
|
||||
if not self.env.context.get('module_uninstall'):
|
||||
self._check_survey_record_creation_references()
|
||||
return super().unlink()
|
||||
|
||||
def _check_survey_record_creation_references(self):
|
||||
if not self or self._name in (
|
||||
'survey.record.creation.field.values',
|
||||
'survey.record.creation.field.values.x2m',
|
||||
):
|
||||
return
|
||||
|
||||
FieldValues = self.env['survey.record.creation.field.values'].sudo()
|
||||
if not FieldValues.search_count([('field_id.relation', '=', self._name)], limit=1):
|
||||
return
|
||||
|
||||
references = [f"{self._name},{record.id}" for record in self]
|
||||
|
||||
used_in_m2o = FieldValues.search([
|
||||
('fixed_value_many2one', 'in', references),
|
||||
])
|
||||
used_in_m2m = self.env['survey.record.creation.field.values.x2m'].sudo().search([
|
||||
('value_reference', 'in', references),
|
||||
])
|
||||
|
||||
if not used_in_m2o and not used_in_m2m:
|
||||
return
|
||||
|
||||
usages = set()
|
||||
for fv in used_in_m2o:
|
||||
usages.add((
|
||||
fv.survey_id.display_name or '',
|
||||
fv.field_id.field_description or fv.field_id.name or '',
|
||||
fv.fixed_value_many2one.display_name or '',
|
||||
))
|
||||
for fvx in used_in_m2m:
|
||||
parent = fvx.survey_record_creation_field_values_id
|
||||
usages.add((
|
||||
parent.survey_id.display_name or '',
|
||||
parent.field_id.field_description or parent.field_id.name or '',
|
||||
fvx.value_reference.display_name or '',
|
||||
))
|
||||
|
||||
usage_lines = '\n'.join(
|
||||
'- ' + _(
|
||||
'Survey "%(survey)s", field "%(field)s" (value: %(value)s)',
|
||||
survey=survey, field=field, value=value,
|
||||
)
|
||||
for survey, field, value in sorted(usages)
|
||||
)
|
||||
|
||||
raise UserError(_(
|
||||
'Cannot delete this record because it is used as a default value '
|
||||
'in the following survey record creation configurations:\n\n'
|
||||
'%(usages)s\n\n'
|
||||
'Please update or remove the corresponding survey configuration first.',
|
||||
usages=usage_lines,
|
||||
))
|
||||
@@ -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
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user