portal_share_2.py 2.0 KB

123456789101112131415161718192021222324252627282930313233
  1. def action_send_mail(self):
  2. active_record = self.env[self.res_model].browse(self.res_id)
  3. template = self.env.ref('portal.portal_share_template', False)
  4. note = self.env.ref('mail.mt_note')
  5. signup_enabled = self.env['ir.config_parameter'].sudo().get_param('auth_signup.invitation_scope') == 'b2c'
  6. if hasattr(active_record, 'access_token') and active_record.access_token or not signup_enabled:
  7. partner_ids = self.partner_ids
  8. else:
  9. partner_ids = self.partner_ids.filtered(lambda x: x.user_ids)
  10. # if partner already user or record has access token send common link in batch to all user
  11. for partner in self.partner_ids:
  12. share_link = active_record.get_base_url() + active_record._get_share_url(redirect=True, pid=partner.id)
  13. active_record.with_context(mail_post_autofollow=True).message_post_with_view(template,
  14. values={'partner': partner, 'note': self.note, 'record': active_record,
  15. 'share_link': share_link},
  16. subject=_("You are invited to access %s" % active_record.display_name),
  17. subtype_id=note.id,
  18. email_layout_xmlid='mail.mail_notification_light',
  19. partner_ids=[(6, 0, partner.ids)])
  20. # when partner not user send individual mail with signup token
  21. for partner in self.partner_ids - partner_ids:
  22. # prepare partner for signup and send singup url with redirect url
  23. partner.signup_get_auth_param()
  24. share_link = partner._get_signup_url_for_action(action='/mail/view', res_id=self.res_id, model=self.model)[partner.id]
  25. active_record.with_context(mail_post_autofollow=True).message_post_with_view(template,
  26. values={'partner': partner, 'note': self.note, 'record': active_record,
  27. 'share_link': share_link},
  28. subject=_("You are invited to access %s" % active_record.display_name),
  29. subtype_id=note.id,
  30. email_layout_xmlid='mail.mail_notification_light',
  31. partner_ids=[(6, 0, partner.ids)])
  32. return {'type': 'ir.actions.act_window_close'}