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.

107 lines
3.8 KiB

  1. # Copyright 2018 David Juaneda - <djuaneda@sdi.es>
  2. # Copyright 2018 Eficent Business and IT Consulting Services, S.L.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models, fields, SUPERUSER_ID
  5. class MailActivity(models.Model):
  6. _inherit = "mail.activity"
  7. res_model_id_name = fields.Char(
  8. related='res_model_id.name', string="Origin",
  9. readonly=True)
  10. duration = fields.Float(
  11. related='calendar_event_id.duration', readonly=True)
  12. calendar_event_id_start = fields.Datetime(
  13. related='calendar_event_id.start', readonly=True)
  14. calendar_event_id_partner_ids = fields.Many2many(
  15. related='calendar_event_id.partner_ids',
  16. readonly=True)
  17. @api.multi
  18. def open_origin(self):
  19. self.ensure_one()
  20. vid = self.env[self.res_model].browse(self.res_id).get_formview_id()
  21. response = {
  22. 'type': 'ir.actions.act_window',
  23. 'res_model': self.res_model,
  24. 'view_mode': 'form',
  25. 'res_id': self.res_id,
  26. 'target': 'current',
  27. 'flags': {
  28. 'form': {
  29. 'action_buttons': False
  30. }
  31. },
  32. 'views': [
  33. (vid, "form")
  34. ]
  35. }
  36. return response
  37. @api.model
  38. def action_activities_board(self):
  39. action = self.env.ref(
  40. 'mail_activity_board.open_boards_activities').read()[0]
  41. return action
  42. @api.model
  43. def _find_allowed_model_wise(self, doc_model, doc_dict):
  44. doc_ids = list(doc_dict)
  45. allowed_doc_ids = self.env[doc_model].with_context(
  46. active_test=False).search([('id', 'in', doc_ids)]).ids
  47. return set([message_id for allowed_doc_id in allowed_doc_ids
  48. for message_id in doc_dict[allowed_doc_id]])
  49. @api.model
  50. def _find_allowed_doc_ids(self, model_ids):
  51. ir_model_access_model = self.env['ir.model.access']
  52. allowed_ids = set()
  53. for doc_model, doc_dict in model_ids.items():
  54. if not ir_model_access_model.check(doc_model, 'read', False):
  55. continue
  56. allowed_ids |= self._find_allowed_model_wise(doc_model, doc_dict)
  57. return allowed_ids
  58. @api.model
  59. def _search(self, args, offset=0, limit=None, order=None, count=False,
  60. access_rights_uid=None):
  61. # Rules do not apply to administrator
  62. if self._uid == SUPERUSER_ID:
  63. return super(MailActivity, self)._search(
  64. args, offset=offset, limit=limit, order=order,
  65. count=count, access_rights_uid=access_rights_uid)
  66. ids = super(MailActivity, self)._search(
  67. args, offset=offset, limit=limit, order=order,
  68. count=False, access_rights_uid=access_rights_uid)
  69. if not ids and count:
  70. return 0
  71. elif not ids:
  72. return ids
  73. # check read access rights before checking the actual rules
  74. super(MailActivity, self.sudo(access_rights_uid or self._uid)).\
  75. check_access_rights('read')
  76. model_ids = {}
  77. self._cr.execute("""
  78. SELECT DISTINCT a.id, im.id, im.model, a.res_id
  79. FROM "%s" a
  80. LEFT JOIN ir_model im ON im.id = a.res_model_id
  81. WHERE a.id = ANY (%%(ids)s)""" % self._table, dict(ids=ids))
  82. for a_id, ir_model_id, model, model_id in self._cr.fetchall():
  83. model_ids.setdefault(model, {}).setdefault(
  84. model_id, set()).add(a_id)
  85. allowed_ids = self._find_allowed_doc_ids(model_ids)
  86. final_ids = allowed_ids
  87. if count:
  88. return len(final_ids)
  89. else:
  90. # re-construct a list based on ids, because set didn't keep order
  91. id_list = [a_id for a_id in ids if a_id in final_ids]
  92. return id_list