You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import SUPERUSER_ID, api, models
  5. from openerp.addons.mail.mail_thread import mail_thread
  6. class MailThread(models.Model):
  7. _inherit = 'mail.thread'
  8. @api.multi
  9. def _get_subscription_data(self, name, args, user_pid=None):
  10. result = super(MailThread, self)._get_subscription_data(
  11. name, args, user_pid=user_pid)
  12. subtypes = self.env['mail.message.subtype'].search([
  13. ('hidden', '=', False),
  14. '|',
  15. ('res_model', '=', self._name),
  16. ('res_model', '=', False),
  17. ])
  18. for follower in self.env['mail.followers'].search([
  19. ('res_model', '=', self._name),
  20. ('res_id', 'in', result.keys()),
  21. ('partner_id', '=', user_pid or self.env.user.partner_id.id),
  22. ]):
  23. # values are ordered dicts, so we get the correct matches
  24. for subtype, data in zip(
  25. subtypes,
  26. result[follower.res_id]['message_subtype_data'].values()):
  27. data['force_mail'] = 'default'
  28. if subtype in follower.force_mail_subtype_ids:
  29. data['force_mail'] = 'force_yes'
  30. elif subtype in follower.force_nomail_subtype_ids:
  31. data['force_mail'] = 'force_no'
  32. data['force_own'] =\
  33. subtype in follower.force_own_subtype_ids
  34. return result
  35. @api.multi
  36. def message_custom_notification_update_user(self, custom_notifications):
  37. """change custom_notifications from user ids to partner ids"""
  38. user2partner = dict(
  39. self.env['res.users'].browse(map(int, custom_notifications.keys()))
  40. .mapped(lambda user: (str(user.id), str(user.partner_id.id)))
  41. )
  42. return self.message_custom_notification_update({
  43. user2partner[user_id]: data
  44. for user_id, data in custom_notifications.iteritems()
  45. })
  46. @api.multi
  47. def message_custom_notification_update(self, custom_notifications):
  48. """custom_notifications is a dictionary with partner ids as keys
  49. and dictionaries mapping message subtype ids to custom notification
  50. values"""
  51. def ids_with_value(data, key, value):
  52. return map(lambda x: int(x[0]),
  53. filter(lambda x: x[1][key] == value,
  54. data.iteritems()))
  55. custom_notifications = {
  56. int(key): value
  57. for key, value in custom_notifications.iteritems()
  58. if key != 'False'
  59. }
  60. for follower in self.env['mail.followers'].search([
  61. ('res_model', '=', self._name),
  62. ('res_id', 'in', self.ids),
  63. ('partner_id', 'in', custom_notifications.keys()),
  64. ]):
  65. data = custom_notifications[follower.partner_id.id]
  66. follower.write({
  67. 'force_mail_subtype_ids': [(6, 0, ids_with_value(
  68. data, 'force_mail', 'force_yes'))],
  69. 'force_nomail_subtype_ids': [(6, 0, ids_with_value(
  70. data, 'force_mail', 'force_no'))],
  71. 'force_own_subtype_ids': [(6, 0, ids_with_value(
  72. data, 'force_own', '1'))]
  73. }),
  74. def _register_hook(self, cr):
  75. model_ids = self.pool['ir.model'].search(cr, SUPERUSER_ID, [])
  76. rebuilt = []
  77. for model in self.pool['ir.model'].browse(cr, SUPERUSER_ID, model_ids):
  78. if model.model not in self.pool:
  79. continue
  80. model_object = self.pool[model.model]
  81. if not isinstance(model_object, mail_thread):
  82. continue
  83. if isinstance(model_object, MailThread):
  84. continue
  85. bases = list(model_object.__class__.__bases__)
  86. if MailThread not in bases:
  87. bases.insert(1, MailThread)
  88. class_dict = dict(model_object.__dict__)
  89. class_dict['_inherit'] = model_object._name
  90. new_model_class = type(model_object._name, tuple(bases),
  91. class_dict)
  92. new_model = new_model_class._build_model(self.pool, cr)
  93. self.pool.models[model.model] = new_model
  94. new_model._prepare_setup(cr, SUPERUSER_ID)
  95. new_model._setup_base(cr, SUPERUSER_ID, False)
  96. new_model._setup_fields(cr, SUPERUSER_ID)
  97. rebuilt.append(new_model)
  98. for model in rebuilt:
  99. model._setup_complete(cr, SUPERUSER_ID)
  100. return super(MailThread, self)._register_hook(cr)