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.

106 lines
3.7 KiB

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