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.

80 lines
3.3 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 api, models
  5. class MailThread(models.AbstractModel):
  6. _inherit = ['base.patch.models.mixin', 'mail.thread']
  7. _name = '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. }),