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.

107 lines
4.4 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, 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. if folder.state == 'done':
  41. folder.retrieve_imap_folder(connection)
  42. connection.close()
  43. except Exception:
  44. _logger.error(_(
  45. "General failure when trying to connect to"
  46. " %s server %s."),
  47. this.type, this.name, exc_info=True)
  48. finally:
  49. if connection:
  50. connection.logout()
  51. return True
  52. def fields_view_get(
  53. self, view_id=None, view_type='form',
  54. toolbar=False, submenu=False):
  55. """Set modifiers for form fields in folder_ids depending on algorithm.
  56. A field will be readonly and/or required if this is specified in the
  57. algorithm.
  58. """
  59. result = super(FetchmailServer, self).fields_view_get(
  60. view_id=view_id, view_type=view_type, toolbar=toolbar,
  61. submenu=submenu)
  62. if view_type == 'form':
  63. view = etree.fromstring(
  64. result['fields']['folder_ids']['views']['form']['arch'])
  65. modifiers = {}
  66. docstr = ''
  67. folder_model = self.env['fetchmail.server.folder']
  68. match_algorithms = folder_model._get_match_algorithms()
  69. for algorithm in match_algorithms.itervalues():
  70. for modifier in ['required', 'readonly']:
  71. for field in getattr(algorithm, modifier + '_fields'):
  72. modifiers.setdefault(field, {})
  73. modifiers[field].setdefault(modifier, [])
  74. if modifiers[field][modifier]:
  75. modifiers[field][modifier].insert(0, '|')
  76. modifiers[field][modifier].append(
  77. ("match_algorithm", "==", algorithm.__name__))
  78. docstr += _(algorithm.name) + '\n' + _(algorithm.__doc__) + \
  79. '\n\n'
  80. for field in view.xpath('//field'):
  81. if field.tag == 'field' and field.get('name') in modifiers:
  82. patched_modifiers = field.attrib['modifiers'].replace(
  83. 'false', 'False').replace('true', 'True')
  84. original_dict = safe_eval(
  85. patched_modifiers,
  86. UnquoteEvalContext({}),
  87. nocopy=True)
  88. modifier_dict = modifiers[field.attrib['name']]
  89. combined_dict = dict(original_dict, **modifier_dict)
  90. field.set('modifiers', simplejson.dumps(combined_dict))
  91. if (field.tag == 'field' and
  92. field.get('name') == 'match_algorithm'):
  93. field.set('help', docstr)
  94. result['fields']['folder_ids']['views']['form']['arch'] = \
  95. etree.tostring(view)
  96. return result