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.

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