portal_share.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. class PortalShare(models.TransientModel):
  5. _name = 'portal.share'
  6. _description = 'Portal Sharing'
  7. @api.model
  8. def default_get(self, fields):
  9. result = super(PortalShare, self).default_get(fields)
  10. result['res_model'] = self._context.get('active_model', False)
  11. result['res_id'] = self._context.get('active_id', False)
  12. if result['res_model'] and result['res_id']:
  13. record = self.env[result['res_model']].browse(result['res_id'])
  14. result['share_link'] = record.get_base_url() + record._get_share_url(redirect=True)
  15. return result
  16. @api.model
  17. def _selection_target_model(self):
  18. return [(model.model, model.name) for model in self.env['ir.model'].sudo().search([])]
  19. res_model = fields.Char('Related Document Model', required=True)
  20. res_id = fields.Integer('Related Document ID', required=True)
  21. resource_ref = fields.Reference('_selection_target_model', 'Related Document', compute='_compute_resource_ref')
  22. partner_ids = fields.Many2many('res.partner', string="Recipients", required=True)
  23. note = fields.Text(help="Add extra content to display in the email")
  24. share_link = fields.Char(string="Link", compute='_compute_share_link')
  25. access_warning = fields.Text("Access warning", compute="_compute_access_warning")
  26. @api.depends('res_model', 'res_id')
  27. def _compute_resource_ref(self):
  28. for wizard in self:
  29. if wizard.res_model and wizard.res_model in self.env:
  30. wizard.resource_ref = '%s,%s' % (wizard.res_model, wizard.res_id or 0)
  31. else:
  32. wizard.resource_ref = None
  33. @api.depends('res_model', 'res_id')
  34. def _compute_share_link(self):
  35. for rec in self:
  36. rec.share_link = False
  37. if rec.res_model:
  38. res_model = self.env[rec.res_model]
  39. if isinstance(res_model, self.pool['portal.mixin']) and rec.res_id:
  40. record = res_model.browse(rec.res_id)
  41. rec.share_link = record.get_base_url() + record._get_share_url(redirect=True)
  42. @api.depends('res_model', 'res_id')
  43. def _compute_access_warning(self):
  44. for rec in self:
  45. rec.access_warning = False
  46. if rec.res_model:
  47. res_model = self.env[rec.res_model]
  48. if isinstance(res_model, self.pool['portal.mixin']) and rec.res_id:
  49. record = res_model.browse(rec.res_id)
  50. rec.access_warning = record.access_warning
  51. @api.model
  52. def _get_note(self):
  53. return self.env.ref('mail.mt_note')
  54. def _send_public_link(self, note, partners=None):
  55. if partners is None:
  56. partners = self.partner_ids
  57. for partner in partners:
  58. share_link = self.resource_ref.get_base_url() + self.resource_ref._get_share_url(redirect=True, pid=partner.id)
  59. saved_lang = self.env.lang
  60. self = self.with_context(lang=partner.lang)
  61. template = self.env.ref('portal.portal_share_template', False)
  62. self.resource_ref.message_post_with_view(template,
  63. values={'partner': partner, 'note': self.note, 'record': self.resource_ref,
  64. 'share_link': share_link},
  65. subject=_("You are invited to access %s", self.resource_ref.display_name),
  66. subtype_id=note.id,
  67. email_layout_xmlid='mail.mail_notification_light',
  68. partner_ids=[(6, 0, partner.ids)])
  69. self = self.with_context(lang=saved_lang)
  70. def _send_signup_link(self, note, partners=None):
  71. if partners is None:
  72. partners = self.partner_ids.filtered(lambda partner: not partner.user_ids)
  73. for partner in partners:
  74. # prepare partner for signup and send singup url with redirect url
  75. partner.signup_get_auth_param()
  76. share_link = partner._get_signup_url_for_action(action='/mail/view', res_id=self.res_id, model=self.res_model)[partner.id]
  77. saved_lang = self.env.lang
  78. self = self.with_context(lang=partner.lang)
  79. template = self.env.ref('portal.portal_share_template', False)
  80. self.resource_ref.message_post_with_view(template,
  81. values={'partner': partner, 'note': self.note, 'record': self.resource_ref,
  82. 'share_link': share_link},
  83. subject=_("You are invited to access %s", self.resource_ref.display_name),
  84. subtype_id=note.id,
  85. email_layout_xmlid='mail.mail_notification_light',
  86. partner_ids=[(6, 0, partner.ids)])
  87. self = self.with_context(lang=saved_lang)
  88. def action_send_mail(self):
  89. note = self._get_note()
  90. signup_enabled = self.env['ir.config_parameter'].sudo().get_param('auth_signup.invitation_scope') == 'b2c'
  91. if getattr(self.resource_ref, 'access_token', False) or not signup_enabled:
  92. partner_ids = self.partner_ids
  93. else:
  94. partner_ids = self.partner_ids.filtered(lambda x: x.user_ids)
  95. # if partner already user or record has access token send common link in batch to all user
  96. self._send_public_link(note, partner_ids)
  97. # when partner not user send individual mail with signup token
  98. self._send_signup_link(note, self.partner_ids - partner_ids)
  99. # subscribe all recipients so that they receive future communication (better than
  100. # using autofollow as more precise)
  101. self.resource_ref.message_subscribe(partner_ids=self.partner_ids.ids)
  102. return {'type': 'ir.actions.act_window_close'}