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.

114 lines
5.0 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. class attach_mail_manually(TransientModel):
  25. _name = 'fetchmail.attach.mail.manually'
  26. _columns = {
  27. 'folder_id': fields.many2one('fetchmail.server.folder', 'Folder',
  28. readonly=True),
  29. 'mail_ids': fields.one2many(
  30. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails'),
  31. }
  32. def default_get(self, cr, uid, fields_list, context=None):
  33. if context is None:
  34. context = {}
  35. defaults = super(attach_mail_manually, self).default_get(cr, uid,
  36. fields_list, context)
  37. for folder in self.pool.get('fetchmail.server.folder').browse(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(None,
  43. 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
  44. if result != 'OK':
  45. logger.error('Could not search mailbox %s on %s' % (
  46. folder.path, this.server))
  47. continue
  48. attach_mail_manually_mail._columns['object_id'].selection=[
  49. (folder.model_id.model, folder.model_id.name)]
  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, this.server))
  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. defaults['mail_ids'].append((0, 0, {
  61. 'msgid': msgid,
  62. 'subject': mail_message.get('subject', ''),
  63. 'date': mail_message.get('date', ''),
  64. 'object_id': folder.model_id.model+',False'
  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. logger.error('Could not fetch %s in %s on %s' % (
  76. msgid, folder.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(connection,
  83. mail.object_id.id, this.folder_id, mail_message,
  84. mail.msgid)
  85. connection.close()
  86. return {'type': 'ir.actions.act_window_close'}
  87. class attach_mail_manually_mail(TransientModel):
  88. _name = 'fetchmail.attach.mail.manually.mail'
  89. _columns = {
  90. 'wizard_id': fields.many2one('fetchmail.attach.mail.manually',
  91. readonly=True),
  92. 'msgid': fields.char('Message id', size=16, readonly=True),
  93. 'subject': fields.char('Subject', size=128, readonly=True),
  94. 'date': fields.datetime('Date', readonly=True),
  95. 'object_id': fields.reference('Object',
  96. selection=lambda self, cr, uid, context:
  97. [(m.model, m.name) for m in
  98. self.pool.get('ir.model').browse(cr, uid,
  99. self.pool.get('ir.model').search(cr, uid, []),
  100. context)], size=128),
  101. }