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.

121 lines
4.9 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. import simplejson
  6. from lxml import etree
  7. from odoo import _, api, exceptions, fields, models
  8. from odoo.tools.safe_eval import safe_eval
  9. from odoo.tools.misc import UnquoteEvalContext
  10. _logger = logging.getLogger(__name__)
  11. class FetchmailServer(models.Model):
  12. _inherit = 'fetchmail.server'
  13. folder_ids = fields.One2many(
  14. comodel_name='fetchmail.server.folder',
  15. inverse_name='server_id',
  16. string='Folders',
  17. context={'active_test': False})
  18. object_id = fields.Many2one(required=False) # comodel_name='ir.model'
  19. type = fields.Selection(default='imap')
  20. folders_only = fields.Boolean(
  21. string='Only folders, not inbox',
  22. help="Check this field to leave imap inbox alone"
  23. " and only retrieve mail from configured folders.")
  24. @api.onchange('type', 'is_ssl', 'object_id')
  25. def onchange_server_type(self):
  26. super(FetchmailServer, self).onchange_server_type()
  27. self.state = 'draft'
  28. @api.onchange('folder_ids')
  29. def onchange_folder_ids(self):
  30. self.onchange_server_type()
  31. @api.multi
  32. def fetch_mail(self):
  33. for this in self:
  34. if not this.folders_only:
  35. super(FetchmailServer, this).fetch_mail()
  36. try:
  37. # New connection for retrieving folders
  38. connection = this.connect()
  39. for folder in this.folder_ids.filtered('active'):
  40. folder.retrieve_imap_folder(connection)
  41. connection.close()
  42. except Exception:
  43. _logger.error(_(
  44. "General failure when trying to connect to"
  45. " %s server %s."),
  46. this.type, this.name, exc_info=True)
  47. finally:
  48. if connection:
  49. connection.logout()
  50. return True
  51. @api.multi
  52. def button_confirm_login(self):
  53. retval = super(FetchmailServer, self).button_confirm_login()
  54. for this in self:
  55. this.write({'state': 'draft'})
  56. connection = this.connect()
  57. connection.select()
  58. for folder in this.folder_ids.filtered('active'):
  59. if connection.select(folder.path)[0] != 'OK':
  60. raise exceptions.ValidationError(
  61. _('Mailbox %s not found!') % folder.path)
  62. connection.close()
  63. this.write({'state': 'done'})
  64. return retval
  65. def fields_view_get(
  66. self, view_id=None, view_type='form',
  67. toolbar=False, submenu=False):
  68. """Set modifiers for form fields in folder_ids depending on algorithm.
  69. A field will be readonly and/or required if this is specified in the
  70. algorithm.
  71. """
  72. result = super(FetchmailServer, self).fields_view_get(
  73. view_id=view_id, view_type=view_type, toolbar=toolbar,
  74. submenu=submenu)
  75. if view_type == 'form':
  76. view = etree.fromstring(
  77. result['fields']['folder_ids']['views']['form']['arch'])
  78. modifiers = {}
  79. docstr = ''
  80. folder_model = self.env['fetchmail.server.folder']
  81. match_algorithms = folder_model._get_match_algorithms()
  82. for algorithm in match_algorithms.itervalues():
  83. for modifier in ['required', 'readonly']:
  84. for field in getattr(algorithm, modifier + '_fields'):
  85. modifiers.setdefault(field, {})
  86. modifiers[field].setdefault(modifier, [])
  87. if modifiers[field][modifier]:
  88. modifiers[field][modifier].insert(0, '|')
  89. modifiers[field][modifier].append(
  90. ("match_algorithm", "==", algorithm.__name__))
  91. docstr += _(algorithm.name) + '\n' + _(algorithm.__doc__) + \
  92. '\n\n'
  93. for field in view.xpath('//field'):
  94. if field.tag == 'field' and field.get('name') in modifiers:
  95. patched_modifiers = field.attrib['modifiers'].replace(
  96. 'false', 'False').replace('true', 'True')
  97. original_dict = safe_eval(
  98. patched_modifiers,
  99. UnquoteEvalContext({}),
  100. nocopy=True)
  101. modifier_dict = modifiers[field.attrib['name']]
  102. combined_dict = dict(original_dict, **modifier_dict)
  103. field.set('modifiers', simplejson.dumps(combined_dict))
  104. if (field.tag == 'field' and
  105. field.get('name') == 'match_algorithm'):
  106. field.set('help', docstr)
  107. result['fields']['folder_ids']['views']['form']['arch'] = \
  108. etree.tostring(view)
  109. return result