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.

103 lines
3.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import _, api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class AttachMailManually(models.TransientModel):
  8. _name = 'fetchmail.attach.mail.manually'
  9. name = fields.Char()
  10. folder_id = fields.Many2one(
  11. 'fetchmail.server.folder', 'Folder', readonly=True)
  12. mail_ids = fields.One2many(
  13. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails')
  14. @api.model
  15. def _prepare_mail(self, folder, msgid, mail_message):
  16. return {
  17. 'msgid': msgid,
  18. 'subject': mail_message.get('subject', ''),
  19. 'date': mail_message.get('date', ''),
  20. 'body': mail_message.get('body', ''),
  21. 'email_from': mail_message.get('from', ''),
  22. 'object_id': '%s,-1' % folder.model_id.model}
  23. @api.model
  24. def default_get(self, fields_list):
  25. defaults = super(AttachMailManually, self).default_get(fields_list)
  26. if not fields_list or 'name' in fields_list:
  27. defaults['name'] = _('Attach emails manually')
  28. defaults['mail_ids'] = []
  29. folder_model = self.env['fetchmail.server.folder']
  30. folder_id = self.env.context.get('folder_id')
  31. defaults['folder_id'] = folder_id
  32. folder = folder_model.browse([folder_id])
  33. connection = folder.server_id.connect()
  34. connection.select(folder.path)
  35. criteria = 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED'
  36. msgids = folder.get_msgids(connection, criteria)
  37. for msgid in msgids[0].split():
  38. mail_message, message_org = folder.fetch_msg(connection, msgid)
  39. defaults['mail_ids'].append(
  40. (0, 0, self._prepare_mail(folder, msgid, mail_message)))
  41. connection.close()
  42. return defaults
  43. @api.multi
  44. def attach_mails(self):
  45. self.ensure_one()
  46. folder = self.folder_id
  47. server = folder.server_id
  48. connection = server.connect()
  49. connection.select(folder.path)
  50. for mail in self.mail_ids:
  51. if not mail.object_id:
  52. continue
  53. msgid = mail.msgid
  54. mail_message, message_org = folder.fetch_msg(connection, msgid)
  55. folder.attach_mail(mail.object_id, mail_message)
  56. folder.update_msg(
  57. connection, msgid, matched=True,
  58. flagged=folder.flag_nonmatching)
  59. connection.close()
  60. return {'type': 'ir.actions.act_window_close'}
  61. @api.model
  62. def fields_view_get(
  63. self, view_id=None, view_type='form',
  64. toolbar=False, submenu=False):
  65. result = super(AttachMailManually, self).fields_view_get(
  66. view_id=view_id, view_type=view_type, toolbar=toolbar,
  67. submenu=submenu)
  68. if view_type != 'form':
  69. return result
  70. folder_model = self.env['fetchmail.server.folder']
  71. folder_id = self.env.context.get('folder_id')
  72. folder = folder_model.browse([folder_id])
  73. form = result['fields']['mail_ids']['views']['form']
  74. form['fields']['object_id']['selection'] = [
  75. (folder.model_id.model, folder.model_id.name)]
  76. return result
  77. class AttachMailManuallyMail(models.TransientModel):
  78. _name = 'fetchmail.attach.mail.manually.mail'
  79. wizard_id = fields.Many2one(
  80. 'fetchmail.attach.mail.manually', readonly=True)
  81. msgid = fields.Char('Message id', readonly=True)
  82. subject = fields.Char('Subject', readonly=True)
  83. date = fields.Datetime('Date', readonly=True)
  84. email_from = fields.Char('From', readonly=True)
  85. body = fields.Html('Body', readonly=True)
  86. object_id = fields.Reference(
  87. lambda self: [
  88. (m.model, m.name)
  89. for m in self.env['ir.model'].search([])],
  90. string='Object')