portal_share.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. res_model = fields.Char('Related Document Model', required=True)
  17. res_id = fields.Integer('Related Document ID', required=True)
  18. partner_ids = fields.Many2many('res.partner', string="Recipients", required=True)
  19. note = fields.Text(help="Add extra content to display in the email")
  20. share_link = fields.Char(string="Link", compute='_compute_share_link')
  21. access_warning = fields.Text("Access warning", compute="_compute_access_warning")
  22. @api.depends('res_model', 'res_id')
  23. def _compute_share_link(self):
  24. for rec in self:
  25. rec.share_link = False
  26. if rec.res_model:
  27. res_model = self.env[rec.res_model]
  28. if isinstance(res_model, self.pool['portal.mixin']) and rec.res_id:
  29. record = res_model.browse(rec.res_id)
  30. rec.share_link = record.get_base_url() + record._get_share_url(redirect=True)
  31. @api.depends('res_model', 'res_id')
  32. def _compute_access_warning(self):
  33. for rec in self:
  34. rec.access_warning = False
  35. if rec.res_model:
  36. res_model = self.env[rec.res_model]
  37. if isinstance(res_model, self.pool['portal.mixin']) and rec.res_id:
  38. record = res_model.browse(rec.res_id)
  39. rec.access_warning = record.access_warning
  40. def action_send_mail(self):
  41. active_record = self.env[self.res_model].browse(self.res_id)
  42. template = self.env.ref('portal.portal_share_template', False)
  43. note = self.env.ref('mail.mt_note')
  44. signup_enabled = self.env['ir.config_parameter'].sudo().get_param('auth_signup.invitation_scope') == 'b2c'
  45. if hasattr(active_record, 'access_token') and active_record.access_token or not signup_enabled:
  46. partner_ids = self.partner_ids
  47. else:
  48. partner_ids = self.partner_ids.filtered(lambda x: x.user_ids)
  49. # if partner already user or record has access token send common link in batch to all user
  50. for partner in self.partner_ids:
  51. share_link = active_record.get_base_url() + active_record._get_share_url(redirect=True, pid=partner.id)
  52. active_record.with_context(mail_post_autofollow=True).message_post_with_view(template,
  53. values={'partner': partner, 'note': self.note, 'record': active_record,
  54. 'share_link': share_link},
  55. subject=_("You are invited to access %s" % active_record.display_name),
  56. subtype_id=note.id,
  57. email_layout_xmlid='mail.mail_notification_light',
  58. partner_ids=[(6, 0, partner.ids)])
  59. # when partner not user send individual mail with signup token
  60. for partner in self.partner_ids - partner_ids:
  61. # prepare partner for signup and send singup url with redirect url
  62. partner.signup_get_auth_param()
  63. share_link = partner._get_signup_url_for_action(action='/mail/view', res_id=self.res_id, model=self.model)[partner.id]
  64. active_record.with_context(mail_post_autofollow=True).message_post_with_view(template,
  65. values={'partner': partner, 'note': self.note, 'record': active_record,
  66. 'share_link': share_link},
  67. subject=_("You are invited to access %s" % active_record.display_name),
  68. subtype_id=note.id,
  69. email_layout_xmlid='mail.mail_notification_light',
  70. partner_ids=[(6, 0, partner.ids)])
  71. return {'type': 'ir.actions.act_window_close'}