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.

129 lines
5.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Therp BV (<http://therp.nl>)
  6. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import fields, models
  23. import logging
  24. _logger = logging.getLogger(__name__)
  25. class attach_mail_manually(models.TransientModel):
  26. _name = 'fetchmail.attach.mail.manually'
  27. folder_id = fields.Many2one(
  28. 'fetchmail.server.folder', 'Folder', readonly=True)
  29. mail_ids = fields.One2many(
  30. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails')
  31. def default_get(self, cr, uid, fields_list, context=None):
  32. if context is None:
  33. context = {}
  34. defaults = super(attach_mail_manually, self).default_get(
  35. cr, uid, fields_list, context
  36. )
  37. for folder in self.pool.get('fetchmail.server.folder').browse(
  38. cr, uid,
  39. [context.get('default_folder_id')], context):
  40. defaults['mail_ids'] = []
  41. connection = folder.server_id.connect()
  42. connection.select(folder.path)
  43. result, msgids = connection.search(
  44. None,
  45. 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
  46. if result != 'OK':
  47. _logger.error('Could not search mailbox %s on %s',
  48. folder.path, folder.server_id.name)
  49. continue
  50. for msgid in msgids[0].split():
  51. result, msgdata = connection.fetch(msgid, '(RFC822)')
  52. if result != 'OK':
  53. _logger.error('Could not fetch %s in %s on %s',
  54. msgid, folder.path, folder.server_id.name)
  55. continue
  56. mail_message = self.pool.get('mail.thread').message_parse(
  57. cr, uid, msgdata[0][1],
  58. save_original=folder.server_id.original,
  59. context=context
  60. )
  61. defaults['mail_ids'].append((0, 0, {
  62. 'msgid': msgid,
  63. 'subject': mail_message.get('subject', ''),
  64. 'date': mail_message.get('date', ''),
  65. 'object_id': '%s,-1' % folder.model_id.model,
  66. }))
  67. connection.close()
  68. return defaults
  69. def attach_mails(self, cr, uid, ids, context=None):
  70. for this in self.browse(cr, uid, ids, context):
  71. for mail in this.mail_ids:
  72. connection = this.folder_id.server_id.connect()
  73. connection.select(this.folder_id.path)
  74. result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
  75. if result != 'OK':
  76. _logger.error('Could not fetch %s in %s on %s',
  77. mail.msgid, this.folder_id.path, this.server)
  78. continue
  79. mail_message = self.pool.get('mail.thread').message_parse(
  80. cr, uid, msgdata[0][1],
  81. save_original=this.folder_id.server_id.original,
  82. context=context)
  83. this.folder_id.server_id.attach_mail(
  84. connection,
  85. mail.object_id.id, this.folder_id, mail_message,
  86. mail.msgid
  87. )
  88. connection.close()
  89. return {'type': 'ir.actions.act_window_close'}
  90. def fields_view_get(self, cr, user, view_id=None, view_type='form',
  91. context=None, toolbar=False, submenu=False):
  92. result = super(attach_mail_manually, self).fields_view_get(
  93. cr, user, view_id, view_type, context, toolbar, submenu)
  94. tree = result['fields']['mail_ids']['views']['tree']
  95. for folder in self.pool['fetchmail.server.folder'].browse(
  96. cr, user, [context.get('default_folder_id')], context):
  97. tree['fields']['object_id']['selection'] = [
  98. (folder.model_id.model, folder.model_id.name)
  99. ]
  100. return result
  101. class attach_mail_manually_mail(models.TransientModel):
  102. _name = 'fetchmail.attach.mail.manually.mail'
  103. wizard_id = fields.Many2one(
  104. 'fetchmail.attach.mail.manually', readonly=True)
  105. msgid = fields.Char('Message id', readonly=True)
  106. subject = fields.Char('Subject', readonly=True)
  107. date = fields.Datetime('Date', readonly=True)
  108. object_id = fields.Reference(
  109. lambda self: [
  110. (m.model, m.name)
  111. for m in self.env['ir.model'].search([])
  112. ],
  113. string='Object')