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.

115 lines
4.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Therp BV (<http://therp.nl>)
  6. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ########################################################################
  22. from openerp import api, models, fields
  23. from .. import match_algorithm
  24. class fetchmail_server_folder(models.Model):
  25. _name = 'fetchmail.server.folder'
  26. _rec_name = 'path'
  27. def _get_match_algorithms(self):
  28. def get_all_subclasses(cls):
  29. return (cls.__subclasses__() +
  30. [subsub
  31. for sub in cls.__subclasses__()
  32. for subsub in get_all_subclasses(sub)])
  33. return dict([(cls.__name__, cls)
  34. for cls in get_all_subclasses(
  35. match_algorithm.base.base)])
  36. def _get_match_algorithms_sel(self):
  37. algorithms = []
  38. for cls in self._get_match_algorithms().itervalues():
  39. algorithms.append((cls.__name__, cls.name))
  40. algorithms.sort()
  41. return algorithms
  42. sequence = fields.Integer('Sequence')
  43. path = fields.Char(
  44. 'Path',
  45. help="The path to your mail folder. Typically would be something like "
  46. "'INBOX.myfolder'", required=True)
  47. model_id = fields.Many2one(
  48. 'ir.model', 'Model', required=True,
  49. help='The model to attach emails to')
  50. model_field = fields.Char(
  51. 'Field (model)',
  52. help='The field in your model that contains the field to match '
  53. 'against.\n'
  54. 'Examples:\n'
  55. "'email' if your model is res.partner, or "
  56. "'partner_id.email' if you're matching sale orders")
  57. model_order = fields.Char(
  58. 'Order (model)',
  59. help='Field(s) to order by, this mostly useful in conjunction '
  60. "with 'Use 1st match'")
  61. match_algorithm = fields.Selection(
  62. _get_match_algorithms_sel,
  63. 'Match algorithm', required=True,
  64. help='The algorithm used to determine which object an email matches.')
  65. mail_field = fields.Char(
  66. 'Field (email)',
  67. help='The field in the email used for matching. Typically '
  68. "this is 'to' or 'from'")
  69. server_id = fields.Many2one('fetchmail.server', 'Server')
  70. delete_matching = fields.Boolean(
  71. 'Delete matches',
  72. help='Delete matched emails from server')
  73. flag_nonmatching = fields.Boolean(
  74. 'Flag nonmatching',
  75. help="Flag emails in the server that don't match any object in Odoo")
  76. match_first = fields.Boolean(
  77. 'Use 1st match',
  78. help='If there are multiple matches, use the first one. If '
  79. 'not checked, multiple matches count as no match at all')
  80. domain = fields.Char(
  81. 'Domain',
  82. help='Fill in a search filter to narrow down objects to match')
  83. msg_state = fields.Selection(
  84. [
  85. ('sent', 'Sent'),
  86. ('received', 'Received'),
  87. ],
  88. 'Message state',
  89. help='The state messages fetched from this folder should be '
  90. 'assigned in Odoo')
  91. _defaults = {
  92. 'flag_nonmatching': True,
  93. 'msg_state': 'received',
  94. }
  95. @api.multi
  96. def get_algorithm(self):
  97. return self._get_match_algorithms()[self.match_algorithm]()
  98. @api.multi
  99. def button_attach_mail_manually(self):
  100. return {
  101. 'type': 'ir.actions.act_window',
  102. 'res_model': 'fetchmail.attach.mail.manually',
  103. 'target': 'new',
  104. 'context': dict(self.env.context, default_folder_id=self.id),
  105. 'view_type': 'form',
  106. 'view_mode': 'form',
  107. }