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.

99 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014-2015 Grupo ESOC <www.grupoesoc.es>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. class MailForwardComposeMessage(models.TransientModel):
  6. """Allow forwarding a message.
  7. It duplicates the message and optionally attaches it to another object
  8. of the database and sends it to another recipients than the original one.
  9. """
  10. _name = "mail_forward.compose_message"
  11. _inherits = {"mail.compose.message": "original_wizard_id"}
  12. @api.model
  13. def default_get(self, fields):
  14. """Fix default values.
  15. Sometimes :meth:`openerp.addons.mail.mail_compose_message
  16. .mail_compose_message.default_get` overwrites the default value
  17. for the ``subject`` field, even when it gets the right default value
  18. from the context.
  19. This method fixes that by getting it from the context if available.
  20. """
  21. result = self.original_wizard_id.default_get(fields)
  22. if "subject" in result and "default_subject" in self.env.context:
  23. result["subject"] = self.env.context["default_subject"]
  24. return result
  25. @api.model
  26. def _get_model_selection(self):
  27. """Get allowed models and their names."""
  28. model_objs = self.env["res.request.link"].search(
  29. [("mail_forward_target", "=", True)],
  30. order="name")
  31. return [(m.object, m.name) for m in model_objs]
  32. @api.onchange("destination_object_id")
  33. def change_destination_object(self):
  34. """Update some fields for the new message."""
  35. if self.destination_object_id:
  36. self.model = self.destination_object_id._name
  37. self.res_id = self.destination_object_id.id
  38. model_name = (self.env["ir.model"]
  39. .search([("model", "=", self.model)])
  40. .name)
  41. record_name = self.destination_object_id.name_get()[0][1]
  42. if model_name:
  43. record_name = "%s %s" % (model_name, record_name)
  44. self.record_name = record_name
  45. @api.multi
  46. def send_mail_action(self):
  47. # action buttons call with positional arguments only, so we need an
  48. # intermediary function to ensure the context is passed correctly
  49. return self.send_mail()
  50. def send_mail(self):
  51. """Send mail and execute the attachment relocation if needed."""
  52. # Let the original wizard do de hard work
  53. result = self.original_wizard_id.send_mail()
  54. # Relocate attachments if needed
  55. if (self.move_attachments and
  56. self.model and
  57. self.res_id and
  58. self.attachment_ids):
  59. for attachment in self.attachment_ids:
  60. attachment.res_model = self.model
  61. attachment.res_id = self.res_id
  62. return result
  63. destination_object_id = fields.Reference(
  64. _get_model_selection,
  65. "Destination object",
  66. help="Object where the forwarded message will be attached")
  67. move_attachments = fields.Boolean(
  68. "Move attachments",
  69. help="Attachments will be assigned to the chosen destination "
  70. "object and you will be able to pick them from its "
  71. "'Attachments' button, but they will not be there for "
  72. "the current object if any. In any case you can always "
  73. "open it from the message itself.")
  74. original_wizard_id = fields.Many2one(
  75. "mail.compose.message",
  76. "Original message compose wizard",
  77. delegate=True,
  78. ondelete="cascade",
  79. required=True)