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.

122 lines
4.5 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. _order = 'sequence'
  28. def _get_match_algorithms(self):
  29. def get_all_subclasses(cls):
  30. return (cls.__subclasses__() +
  31. [subsub
  32. for sub in cls.__subclasses__()
  33. for subsub in get_all_subclasses(sub)])
  34. return dict([(cls.__name__, cls)
  35. for cls in get_all_subclasses(
  36. match_algorithm.base.base)])
  37. def _get_match_algorithms_sel(self):
  38. algorithms = []
  39. for cls in self._get_match_algorithms().itervalues():
  40. algorithms.append((cls.__name__, cls.name))
  41. algorithms.sort()
  42. return algorithms
  43. sequence = fields.Integer('Sequence')
  44. path = fields.Char(
  45. 'Path',
  46. help="The path to your mail folder. Typically would be something like "
  47. "'INBOX.myfolder'", required=True)
  48. model_id = fields.Many2one(
  49. 'ir.model', 'Model', required=True,
  50. help='The model to attach emails to')
  51. model_field = fields.Char(
  52. 'Field (model)',
  53. help='The field in your model that contains the field to match '
  54. 'against.\n'
  55. 'Examples:\n'
  56. "'email' if your model is res.partner, or "
  57. "'partner_id.email' if you're matching sale orders")
  58. model_order = fields.Char(
  59. 'Order (model)',
  60. help='Field(s) to order by, this mostly useful in conjunction '
  61. "with 'Use 1st match'")
  62. match_algorithm = fields.Selection(
  63. _get_match_algorithms_sel,
  64. 'Match algorithm', required=True,
  65. help='The algorithm used to determine which object an email matches.')
  66. mail_field = fields.Char(
  67. 'Field (email)',
  68. help='The field in the email used for matching. Typically '
  69. "this is 'to' or 'from'")
  70. server_id = fields.Many2one('fetchmail.server', 'Server')
  71. delete_matching = fields.Boolean(
  72. 'Delete matches',
  73. help='Delete matched emails from server')
  74. flag_nonmatching = fields.Boolean(
  75. 'Flag nonmatching',
  76. help="Flag emails in the server that don't match any object in Odoo")
  77. match_first = fields.Boolean(
  78. 'Use 1st match',
  79. help='If there are multiple matches, use the first one. If '
  80. 'not checked, multiple matches count as no match at all')
  81. domain = fields.Char(
  82. 'Domain',
  83. help='Fill in a search filter to narrow down objects to match')
  84. context = fields.Char(
  85. 'Context',
  86. help=('Context that is passed during mail processing. Can be used to '
  87. 'set default values.'))
  88. msg_state = fields.Selection(
  89. [
  90. ('sent', 'Sent'),
  91. ('received', 'Received'),
  92. ],
  93. 'Message state',
  94. help='The state messages fetched from this folder should be '
  95. 'assigned in Odoo')
  96. active = fields.Boolean('Active')
  97. _defaults = {
  98. 'flag_nonmatching': True,
  99. 'msg_state': 'received',
  100. 'active': True,
  101. }
  102. @api.multi
  103. def get_algorithm(self):
  104. return self._get_match_algorithms()[self.match_algorithm]()
  105. @api.multi
  106. def button_attach_mail_manually(self):
  107. return {
  108. 'type': 'ir.actions.act_window',
  109. 'res_model': 'fetchmail.attach.mail.manually',
  110. 'target': 'new',
  111. 'context': dict(self.env.context, default_folder_id=self.id),
  112. 'view_type': 'form',
  113. 'view_mode': 'form',
  114. }