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.

110 lines
4.8 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.message').parse_message(
  57. msgdata[0][1])
  58. defaults['mail_ids'].append((0, 0, {
  59. 'msgid': msgid,
  60. 'subject': mail_message.get('subject', ''),
  61. 'date': mail_message.get('date', ''),
  62. 'object_id': folder.model_id.model+',False'
  63. }))
  64. connection.close()
  65. return defaults
  66. def attach_mails(self, cr, uid, ids, context=None):
  67. for this in self.browse(cr, uid, ids, context):
  68. for mail in this.mail_ids:
  69. connection = this.folder_id.server_id.connect()
  70. connection.select(this.folder_id.path)
  71. result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
  72. if result != 'OK':
  73. logger.error('Could not fetch %s in %s on %s' % (
  74. msgid, folder.path, this.server))
  75. continue
  76. mail_message = self.pool.get('mail.message').parse_message(
  77. msgdata[0][1], this.folder_id.server_id.original)
  78. this.folder_id.server_id.attach_mail(connection,
  79. mail.object_id.id, this.folder_id, mail_message,
  80. mail.msgid)
  81. connection.close()
  82. return {'type': 'ir.actions.act_window_close'}
  83. class attach_mail_manually_mail(TransientModel):
  84. _name = 'fetchmail.attach.mail.manually.mail'
  85. _columns = {
  86. 'wizard_id': fields.many2one('fetchmail.attach.mail.manually',
  87. readonly=True),
  88. 'msgid': fields.char('Message id', size=16, readonly=True),
  89. 'subject': fields.char('Subject', size=128, readonly=True),
  90. 'date': fields.datetime('Date', readonly=True),
  91. 'object_id': fields.reference('Object',
  92. selection=lambda self, cr, uid, context:
  93. [(m.model, m.name) for m in
  94. self.pool.get('ir.model').browse(cr, uid,
  95. self.pool.get('ir.model').search(cr, uid, []),
  96. context)], size=128),
  97. }