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.

89 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright - 2013-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class AttachMailManually(models.TransientModel):
  8. _name = 'fetchmail.attach.mail.manually'
  9. folder_id = fields.Many2one(
  10. 'fetchmail.server.folder', 'Folder', readonly=True)
  11. mail_ids = fields.One2many(
  12. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails')
  13. @api.model
  14. def default_get(self, fields_list):
  15. defaults = super(AttachMailManually, self).default_get(fields_list)
  16. defaults['mail_ids'] = []
  17. folder_model = self.env['fetchmail.server.folder']
  18. folder_id = self.env.context.get('folder_id')
  19. defaults['folder_id'] = folder_id
  20. folder = folder_model.browse([folder_id])
  21. connection = folder.server_id.connect()
  22. connection.select(folder.path)
  23. criteria = 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED'
  24. msgids = folder.get_msgids(connection, criteria)
  25. for msgid in msgids[0].split():
  26. mail_message, message_org = folder.fetch_msg(connection, msgid)
  27. defaults['mail_ids'].append((0, 0, {
  28. 'msgid': msgid,
  29. 'subject': mail_message.get('subject', ''),
  30. 'date': mail_message.get('date', ''),
  31. 'object_id': '%s,-1' % folder.model_id.model}))
  32. connection.close()
  33. return defaults
  34. @api.multi
  35. def attach_mails(self):
  36. self.ensure_one()
  37. folder = self.folder_id
  38. server = folder.server_id
  39. connection = server.connect()
  40. connection.select(folder.path)
  41. for mail in self.mail_ids:
  42. if not mail.object_id:
  43. continue
  44. msgid = mail.msgid
  45. mail_message, message_org = folder.fetch_msg(connection, msgid)
  46. folder.attach_mail(mail.object_id, mail_message)
  47. folder.update_msg(
  48. connection, msgid, matched=True,
  49. flagged=folder.flag_nonmatching)
  50. connection.close()
  51. return {'type': 'ir.actions.act_window_close'}
  52. @api.model
  53. def fields_view_get(
  54. self, view_id=None, view_type='form',
  55. toolbar=False, submenu=False):
  56. result = super(AttachMailManually, self).fields_view_get(
  57. view_id=view_id, view_type=view_type, toolbar=toolbar,
  58. submenu=submenu)
  59. folder_model = self.env['fetchmail.server.folder']
  60. folder_id = self.env.context.get('folder_id')
  61. folder = folder_model.browse([folder_id])
  62. tree = result['fields']['mail_ids']['views']['tree']
  63. tree['fields']['object_id']['selection'] = [
  64. (folder.model_id.model, folder.model_id.name)]
  65. return result
  66. class AttachMailManuallyMail(models.TransientModel):
  67. _name = 'fetchmail.attach.mail.manually.mail'
  68. wizard_id = fields.Many2one(
  69. 'fetchmail.attach.mail.manually', readonly=True)
  70. msgid = fields.Char('Message id', readonly=True)
  71. subject = fields.Char('Subject', readonly=True)
  72. date = fields.Datetime('Date', readonly=True)
  73. object_id = fields.Reference(
  74. lambda self: [
  75. (m.model, m.name)
  76. for m in self.env['ir.model'].search([])],
  77. string='Object')