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.

120 lines
3.9 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. _models = [
  13. "crm.lead",
  14. "crm.meeting",
  15. "crm.phonecall",
  16. "mail.group",
  17. "note.note",
  18. "product.product",
  19. "project.project",
  20. "project.task",
  21. "res.partner",
  22. "sale.order",
  23. ]
  24. @api.model
  25. def default_get(self, fields):
  26. """Fix default values.
  27. Sometimes :meth:`openerp.addons.mail.mail_compose_message
  28. .mail_compose_message.default_get` overwrites the default value
  29. for the ``subject`` field, even when it gets the right default value
  30. from the context.
  31. This method fixes that by getting it from the context if available.
  32. """
  33. result = self.original_wizard_id.default_get(fields)
  34. if "subject" in result and "default_subject" in self.env.context:
  35. result["subject"] = self.env.context["default_subject"]
  36. return result
  37. @api.model
  38. def models(self):
  39. """Get allowed models and their names.
  40. It searches for the models on the database, so if modules are not
  41. installed, models will not be shown.
  42. """
  43. model_objs = self.env["ir.model"].search(
  44. [("model", "in", self.env.context.get("model_list",
  45. self._models))],
  46. order="name")
  47. return [(m.model, m.name) for m in model_objs]
  48. @api.one
  49. @api.onchange("destination_object_id")
  50. def change_destination_object(self):
  51. """Update some fields for the new message."""
  52. if self.destination_object_id:
  53. self.model = self.destination_object_id._name
  54. self.res_id = self.destination_object_id.id
  55. model_name = (self.env["ir.model"]
  56. .search([("model", "=", self.model)])
  57. .name)
  58. record_name = self.destination_object_id.name_get()[0][1]
  59. if model_name:
  60. record_name = "%s %s" % (model_name, record_name)
  61. self.record_name = record_name
  62. else:
  63. self.model = self.res_id = self.record_name = False
  64. @api.one
  65. def send_mail(self):
  66. """Send mail and execute the attachment relocation if needed."""
  67. # Let the original wizard do de hard work
  68. result = self.original_wizard_id.send_mail()
  69. # Relocate attachments if needed
  70. if (self.move_attachments and
  71. self.model and
  72. self.res_id and
  73. self.attachment_ids):
  74. for attachment in self.attachment_ids:
  75. attachment.res_model = self.model
  76. attachment.res_id = self.res_id
  77. return result
  78. destination_object_id = fields.Reference(
  79. models,
  80. "Destination object",
  81. help="Object where the forwarded message will be attached")
  82. move_attachments = fields.Boolean(
  83. "Move attachments",
  84. help="Attachments will be assigned to the chosen destination "
  85. "object and you will be able to pick them from its "
  86. "'Attachments' button, but they will not be there for "
  87. "the current object if any. In any case you can always "
  88. "open it from the message itself.")
  89. original_wizard_id = fields.Many2one(
  90. "mail.compose.message",
  91. "Original message compose wizard",
  92. delegate=True,
  93. ondelete="cascade",
  94. required=True)