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.

121 lines
4.8 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.osv import fields
  23. from openerp.osv.orm import Model
  24. from .. import match_algorithm
  25. class fetchmail_server_folder(Model):
  26. _name = 'fetchmail.server.folder'
  27. _rec_name = 'path'
  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) for cls in get_all_subclasses(
  35. match_algorithm.base.base)])
  36. def _get_match_algorithms_sel(self, cr, uid, context=None):
  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. _columns = {
  43. 'sequence': fields.integer('Sequence'),
  44. 'path': fields.char(
  45. 'Path', size=256, help='The path to your mail '
  46. "folder. Typically would be something like 'INBOX.myfolder'",
  47. 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)', size=128,
  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)', size=128,
  60. help='Fields 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, translate=True,
  65. help='The algorithm used to determine which object an email '
  66. 'matches.'),
  67. 'mail_field': fields.char(
  68. 'Field (email)', size=128,
  69. help='The field in the email used for matching. Typically '
  70. "this is 'to' or 'from'"),
  71. 'server_id': fields.many2one('fetchmail.server', 'Server'),
  72. 'delete_matching': fields.boolean(
  73. 'Delete matches',
  74. help='Delete matched emails from server'),
  75. 'flag_nonmatching': fields.boolean(
  76. 'Flag nonmatching',
  77. help="Flag emails in the server that don't match any object "
  78. 'in OpenERP'),
  79. 'match_first': fields.boolean(
  80. 'Use 1st match',
  81. help='If there are multiple matches, use the first one. If '
  82. 'not checked, multiple matches count as no match at all'),
  83. 'domain': fields.char(
  84. 'Domain', size=128, help='Fill in a search '
  85. 'filter to narrow down objects to match'),
  86. 'msg_state': fields.selection(
  87. [
  88. ('sent', 'Sent'),
  89. ('received', 'Received'),
  90. ],
  91. 'Message state',
  92. help='The state messages fetched from this folder should be '
  93. 'assigned in OpenERP'),
  94. }
  95. _defaults = {
  96. 'flag_nonmatching': True,
  97. 'msg_state': 'received',
  98. }
  99. def get_algorithm(self, cr, uid, ids, context=None):
  100. for this in self.browse(cr, uid, ids, context):
  101. return self._get_match_algorithms()[this.match_algorithm]()
  102. def button_attach_mail_manually(self, cr, uid, ids, context=None):
  103. for this in self.browse(cr, uid, ids, context):
  104. context.update({'default_folder_id': this.id})
  105. return {
  106. 'type': 'ir.actions.act_window',
  107. 'res_model': 'fetchmail.attach.mail.manually',
  108. 'target': 'new',
  109. 'context': context,
  110. 'view_type': 'form',
  111. 'view_mode': 'form',
  112. }