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