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.

110 lines
4.7 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__() + [subsub
  31. for sub in cls.__subclasses__()
  32. for subsub in get_all_subclasses(sub)]
  33. return dict([(cls.__name__, cls) for cls in get_all_subclasses(
  34. match_algorithm.base.base)])
  35. def _get_match_algorithms_sel(self, cr, uid, context=None):
  36. algorithms=[]
  37. for cls in self._get_match_algorithms().itervalues():
  38. algorithms.append((cls.__name__, cls.name))
  39. return tuple(sorted(algorithms, lambda a, b: cmp(a[0], b[0])))
  40. _columns = {
  41. 'sequence': fields.integer('Sequence'),
  42. 'path': fields.char(
  43. 'Path', size=256, help='The path to your mail '
  44. 'folder. Typically would be something like \'INBOX.myfolder\'',
  45. required=True),
  46. 'model_id': fields.many2one(
  47. 'ir.model', 'Model', required=True,
  48. help='The model to attach emails to'),
  49. 'model_field': fields.char(
  50. 'Field (model)', size=128,
  51. help='The field in your model that contains the field to match '
  52. 'against.\n'
  53. 'Examples:\n'
  54. '\'email\' if your model is res.partner, or '
  55. '\'partner_id.email\' if you\'re matching sale orders'),
  56. 'model_order': fields.char(
  57. 'Order (model)', size=128,
  58. help='Fields to order by, this mostly useful in conjunction '
  59. 'with \'Use 1st match\''),
  60. 'match_algorithm': fields.selection(
  61. _get_match_algorithms_sel,
  62. 'Match algorithm', required=True, translate=True,
  63. help='The algorithm used to determine which object an email '
  64. 'matches.'),
  65. 'mail_field': fields.char(
  66. 'Field (email)', size=128,
  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 '
  76. 'in OpenERP'),
  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', size=128, help='Fill in a search '
  83. 'filter to narrow down objects to match')
  84. }
  85. _defaults = {
  86. 'flag_nonmatching': True,
  87. }
  88. def get_algorithm(self, cr, uid, ids, context=None):
  89. for this in self.browse(cr, uid, ids, context):
  90. return self._get_match_algorithms()[this.match_algorithm]()
  91. def button_attach_mail_manually(self, cr, uid, ids, context=None):
  92. for this in self.browse(cr, uid, ids, context):
  93. context.update({'default_folder_id': this.id})
  94. return {
  95. 'type': 'ir.actions.act_window',
  96. 'res_model': 'fetchmail.attach.mail.manually',
  97. 'target': 'new',
  98. 'context': context,
  99. 'view_type': 'form',
  100. 'view_mode': 'form',
  101. }