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.

74 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2012-2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
  4. import operator
  5. from odoo import api, fields, models
  6. from odoo.addons.server_environment import serv_config
  7. class FetchmailServer(models.Model):
  8. """Incoming POP/IMAP mail server account"""
  9. _inherit = 'fetchmail.server'
  10. server = fields.Char(compute='_compute_server_env',
  11. states={})
  12. port = fields.Integer(compute='_compute_server_env',
  13. states={})
  14. type = fields.Selection(compute='_compute_server_env',
  15. search='_search_type',
  16. states={})
  17. user = fields.Char(compute='_compute_server_env',
  18. states={})
  19. password = fields.Char(compute='_compute_server_env',
  20. states={})
  21. is_ssl = fields.Boolean(compute='_compute_server_env')
  22. attach = fields.Boolean(compute='_compute_server_env')
  23. original = fields.Boolean(compute='_compute_server_env')
  24. @api.depends()
  25. def _compute_server_env(self):
  26. for fetchmail in self:
  27. global_section_name = 'incoming_mail'
  28. key_types = {'port': int,
  29. 'is_ssl': lambda a: bool(int(a or 0)),
  30. 'attach': lambda a: bool(int(a or 0)),
  31. 'original': lambda a: bool(int(a or 0)),
  32. }
  33. # default vals
  34. config_vals = {'port': 993,
  35. 'is_ssl': 0,
  36. 'attach': 0,
  37. 'original': 0,
  38. }
  39. if serv_config.has_section(global_section_name):
  40. config_vals.update(serv_config.items(global_section_name))
  41. custom_section_name = '.'.join((global_section_name,
  42. fetchmail.name))
  43. if serv_config.has_section(custom_section_name):
  44. config_vals.update(serv_config.items(custom_section_name))
  45. for key, to_type in key_types.iteritems():
  46. if config_vals.get(key):
  47. config_vals[key] = to_type(config_vals[key])
  48. fetchmail.update(config_vals)
  49. @api.model
  50. def _search_type(self, oper, value):
  51. operators = {
  52. '=': operator.eq,
  53. '!=': operator.ne,
  54. 'in': operator.contains,
  55. 'not in': lambda a, b: not operator.contains(a, b),
  56. }
  57. if oper not in operators:
  58. return [('id', 'in', [])]
  59. servers = self.search([]).filtered(
  60. lambda s: operators[oper](value, s.type)
  61. )
  62. return [('id', 'in', servers.ids)]