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.

97 lines
3.5 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 openerp 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.one
  33. @api.onchange("destination_object_id")
  34. def change_destination_object(self):
  35. """Update some fields for the new message."""
  36. if self.destination_object_id:
  37. self.model = self.destination_object_id._name
  38. self.res_id = self.destination_object_id.id
  39. model_name = (self.env["ir.model"]
  40. .search([("model", "=", self.model)])
  41. .name)
  42. record_name = self.destination_object_id.name_get()[0][1]
  43. if model_name:
  44. record_name = "%s %s" % (model_name, record_name)
  45. self.record_name = record_name
  46. else:
  47. self.model = self.res_id = self.record_name = False
  48. @api.one
  49. def send_mail(self):
  50. """Send mail and execute the attachment relocation if needed."""
  51. # Let the original wizard do de hard work
  52. result = self.original_wizard_id.send_mail()
  53. # Relocate attachments if needed
  54. if (self.move_attachments and
  55. self.model and
  56. self.res_id and
  57. self.attachment_ids):
  58. for attachment in self.attachment_ids:
  59. attachment.res_model = self.model
  60. attachment.res_id = self.res_id
  61. return result
  62. destination_object_id = fields.Reference(
  63. _get_model_selection,
  64. "Destination object",
  65. help="Object where the forwarded message will be attached")
  66. move_attachments = fields.Boolean(
  67. "Move attachments",
  68. help="Attachments will be assigned to the chosen destination "
  69. "object and you will be able to pick them from its "
  70. "'Attachments' button, but they will not be there for "
  71. "the current object if any. In any case you can always "
  72. "open it from the message itself.")
  73. original_wizard_id = fields.Many2one(
  74. "mail.compose.message",
  75. "Original message compose wizard",
  76. delegate=True,
  77. ondelete="cascade",
  78. required=True)