Files
odoo_source/addons/utm/models/utm_medium.py
T
std-odoo 4dbcefb5e5 [IMP] utm: ensure the uniqueness of the names of the UTM models
Purpose
=======
Ensure that the names of the UTMs models (campaign, medium and source)
are unique.

If not, generate automatically an unique name.

The name field is no more translatable; it makes no sense to translate
a technical which will be added in the URL, and can even cause issues
(the UTM record is not recognized because of the translation).

For the campaign, we keep a "translatable" name which is called
"title".

For the UTM source, use a mixin to generate automatically the name of
the source based on the content (_rec_name) of the record. So we remove
the override of create / write / copy in the different models and
ensure consistency between those models.

Task-2245823

closes odoo/odoo#60501

Related: odoo/enterprise#21882
Related: odoo/upgrade#1863
Signed-off-by: Warnon Aurélien (awa) <awa@odoo.com>
2022-02-22 12:53:00 +00:00

37 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models
from odoo.exceptions import UserError
class UtmMedium(models.Model):
_name = 'utm.medium'
_description = 'UTM Medium'
_order = 'name'
name = fields.Char(string='Medium Name', required=True, translate=False)
active = fields.Boolean(default=True)
_sql_constraints = [
('unique_name', 'UNIQUE(name)', 'The name must be unique'),
]
@api.model_create_multi
def create(self, vals_list):
new_names = self.env['utm.mixin']._get_unique_names(self._name, [vals.get('name') for vals in vals_list])
for vals, new_name in zip(vals_list, new_names):
vals['name'] = new_name
return super().create(vals_list)
@api.ondelete(at_uninstall=False)
def _unlink_except_utm_medium_email(self):
utm_medium_email = self.env.ref('utm.utm_medium_email', raise_if_not_found=False)
if utm_medium_email and utm_medium_email in self:
raise UserError(_(
"The UTM medium '%s' cannot be deleted as it is used in some main "
"functional flows, such as the recruitment and the mass mailing.",
utm_medium_email.name
))