utm_campaign.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, api, SUPERUSER_ID
  4. class UtmCampaign(models.Model):
  5. _name = 'utm.campaign'
  6. _description = 'UTM Campaign'
  7. _rec_name = 'title'
  8. name = fields.Char(string='Campaign Identifier', required=True, compute='_compute_name',
  9. store=True, readonly=False, precompute=True, translate=False)
  10. title = fields.Char(string='Campaign Name', required=True, translate=True)
  11. user_id = fields.Many2one(
  12. 'res.users', string='Responsible',
  13. required=True, default=lambda self: self.env.uid)
  14. stage_id = fields.Many2one(
  15. 'utm.stage', string='Stage', ondelete='restrict', required=True,
  16. default=lambda self: self.env['utm.stage'].search([], limit=1),
  17. group_expand='_group_expand_stage_ids')
  18. tag_ids = fields.Many2many(
  19. 'utm.tag', 'utm_tag_rel',
  20. 'tag_id', 'campaign_id', string='Tags')
  21. is_auto_campaign = fields.Boolean(default=False, string="Automatically Generated Campaign", help="Allows us to filter relevant Campaigns")
  22. color = fields.Integer(string='Color Index')
  23. _sql_constraints = [
  24. ('unique_name', 'UNIQUE(name)', 'The name must be unique'),
  25. ]
  26. @api.depends('title')
  27. def _compute_name(self):
  28. new_names = self.env['utm.mixin']._get_unique_names(self._name, [c.title for c in self])
  29. for campaign, new_name in zip(self, new_names):
  30. campaign.name = new_name
  31. @api.model_create_multi
  32. def create(self, vals_list):
  33. for vals in vals_list:
  34. if not vals.get('title') and vals.get('name'):
  35. vals['title'] = vals['name']
  36. new_names = self.env['utm.mixin']._get_unique_names(self._name, [vals.get('name') for vals in vals_list])
  37. for vals, new_name in zip(vals_list, new_names):
  38. if new_name:
  39. vals['name'] = new_name
  40. return super().create(vals_list)
  41. @api.model
  42. def _group_expand_stage_ids(self, stages, domain, order):
  43. """Read group customization in order to display all the stages in the
  44. Kanban view, even if they are empty.
  45. """
  46. stage_ids = stages._search([], order=order, access_rights_uid=SUPERUSER_ID)
  47. return stages.browse(stage_ids)