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.

128 lines
5.2 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. class attach_mail_manually(models.TransientModel):
  25. _name = 'fetchmail.attach.mail.manually'
  26. folder_id = fields.Many2one(
  27. 'fetchmail.server.folder', 'Folder', readonly=True)
  28. mail_ids = fields.One2many(
  29. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails')
  30. def default_get(self, cr, uid, fields_list, context=None):
  31. if context is None:
  32. context = {}
  33. defaults = super(attach_mail_manually, self).default_get(
  34. cr, uid, fields_list, context
  35. )
  36. for folder in self.pool.get('fetchmail.server.folder').browse(
  37. cr, uid,
  38. [context.get('default_folder_id')], context):
  39. defaults['mail_ids'] = []
  40. connection = folder.server_id.connect()
  41. connection.select(folder.path)
  42. result, msgids = connection.search(
  43. None,
  44. 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
  45. if result != 'OK':
  46. logging.error('Could not search mailbox %s on %s' % (
  47. folder.path, folder.server_id.name))
  48. continue
  49. for msgid in msgids[0].split():
  50. result, msgdata = connection.fetch(msgid, '(RFC822)')
  51. if result != 'OK':
  52. logging.error('Could not fetch %s in %s on %s' % (
  53. msgid, folder.path, folder.server_id.name))
  54. continue
  55. mail_message = self.pool.get('mail.thread').message_parse(
  56. cr, uid, msgdata[0][1],
  57. save_original=folder.server_id.original,
  58. context=context
  59. )
  60. defaults['mail_ids'].append((0, 0, {
  61. 'msgid': msgid,
  62. 'subject': mail_message.get('subject', ''),
  63. 'date': mail_message.get('date', ''),
  64. 'object_id': '%s,-1' % folder.model_id.model,
  65. }))
  66. connection.close()
  67. return defaults
  68. def attach_mails(self, cr, uid, ids, context=None):
  69. for this in self.browse(cr, uid, ids, context):
  70. for mail in this.mail_ids:
  71. connection = this.folder_id.server_id.connect()
  72. connection.select(this.folder_id.path)
  73. result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
  74. if result != 'OK':
  75. logging.error('Could not fetch %s in %s on %s' % (
  76. mail.msgid, this.folder_id.path, this.server))
  77. continue
  78. mail_message = self.pool.get('mail.thread').message_parse(
  79. cr, uid, msgdata[0][1],
  80. save_original=this.folder_id.server_id.original,
  81. context=context)
  82. this.folder_id.server_id.attach_mail(
  83. connection,
  84. mail.object_id.id, this.folder_id, mail_message,
  85. mail.msgid
  86. )
  87. connection.close()
  88. return {'type': 'ir.actions.act_window_close'}
  89. def fields_view_get(self, cr, user, view_id=None, view_type='form',
  90. context=None, toolbar=False, submenu=False):
  91. result = super(attach_mail_manually, self).fields_view_get(
  92. cr, user, view_id, view_type, context, toolbar, submenu)
  93. tree = result['fields']['mail_ids']['views']['tree']
  94. for folder in self.pool['fetchmail.server.folder'].browse(
  95. cr, user, [context.get('default_folder_id')], context):
  96. tree['fields']['object_id']['selection'] = [
  97. (folder.model_id.model, folder.model_id.name)
  98. ]
  99. return result
  100. class attach_mail_manually_mail(models.TransientModel):
  101. _name = 'fetchmail.attach.mail.manually.mail'
  102. wizard_id = fields.Many2one(
  103. 'fetchmail.attach.mail.manually', readonly=True)
  104. msgid = fields.Char('Message id', readonly=True)
  105. subject = fields.Char('Subject', readonly=True)
  106. date = fields.Datetime('Date', readonly=True)
  107. object_id = fields.Reference(
  108. lambda self: [
  109. (m.model, m.name)
  110. for m in self.env['ir.model'].search([])
  111. ],
  112. string='Object')