mailing_trace.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import random
  4. import string
  5. from odoo import api, fields, models
  6. from odoo.osv import expression
  7. class MailingTrace(models.Model):
  8. """ Improve statistics model to add SMS support. Main attributes of
  9. statistics model are used, only some specific data is required. """
  10. _inherit = 'mailing.trace'
  11. CODE_SIZE = 3
  12. trace_type = fields.Selection(selection_add=[
  13. ('sms', 'SMS')
  14. ], ondelete={'sms': 'set default'})
  15. sms_sms_id = fields.Many2one('sms.sms', string='SMS', index='btree_not_null', ondelete='set null')
  16. sms_sms_id_int = fields.Integer(
  17. string='SMS ID (tech)',
  18. index='btree_not_null'
  19. # Integer because the related sms.sms can be deleted separately from its statistics.
  20. # However the ID is needed for several action and controllers.
  21. )
  22. sms_number = fields.Char('Number')
  23. sms_code = fields.Char('Code')
  24. failure_type = fields.Selection(selection_add=[
  25. ('sms_number_missing', 'Missing Number'),
  26. ('sms_number_format', 'Wrong Number Format'),
  27. ('sms_credit', 'Insufficient Credit'),
  28. ('sms_server', 'Server Error'),
  29. ('sms_acc', 'Unregistered Account'),
  30. # mass mode specific codes
  31. ('sms_blacklist', 'Blacklisted'),
  32. ('sms_duplicate', 'Duplicate'),
  33. ('sms_optout', 'Opted Out'),
  34. ])
  35. @api.model_create_multi
  36. def create(self, values_list):
  37. for values in values_list:
  38. if 'sms_sms_id' in values:
  39. values['sms_sms_id_int'] = values['sms_sms_id']
  40. if values.get('trace_type') == 'sms' and not values.get('sms_code'):
  41. values['sms_code'] = self._get_random_code()
  42. return super(MailingTrace, self).create(values_list)
  43. def _get_random_code(self):
  44. """ Generate a random code for trace. Uniqueness is not really necessary
  45. as it serves as obfuscation when unsubscribing. A valid trio
  46. code / mailing_id / number will be requested. """
  47. return ''.join(random.choice(string.ascii_letters + string.digits) for dummy in range(self.CODE_SIZE))