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.

131 lines
5.0 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. algorithms.sort()
  40. return algorithms
  41. _columns = {
  42. 'sequence': fields.integer('Sequence'),
  43. 'path': fields.char(
  44. 'Path', size=256, help='The path to your mail '
  45. "folder. Typically would be something like 'INBOX.myfolder'",
  46. required=True
  47. ),
  48. 'model_id': fields.many2one(
  49. 'ir.model', 'Model', required=True,
  50. help='The model to attach emails to'
  51. ),
  52. 'model_field': fields.char(
  53. 'Field (model)', size=128,
  54. help='The field in your model that contains the field to match '
  55. 'against.\n'
  56. 'Examples:\n'
  57. "'email' if your model is res.partner, or "
  58. "'partner_id.email' if you're matching sale orders"
  59. ),
  60. 'model_order': fields.char(
  61. 'Order (model)', size=128,
  62. help='Fields to order by, this mostly useful in conjunction '
  63. "with 'Use 1st match'"
  64. ),
  65. 'match_algorithm': fields.selection(
  66. _get_match_algorithms_sel,
  67. 'Match algorithm', required=True, translate=True,
  68. help='The algorithm used to determine which object an email '
  69. 'matches.'
  70. ),
  71. 'mail_field': fields.char(
  72. 'Field (email)', size=128,
  73. help='The field in the email used for matching. Typically '
  74. "this is 'to' or 'from'"
  75. ),
  76. 'server_id': fields.many2one('fetchmail.server', 'Server'),
  77. 'delete_matching': fields.boolean(
  78. 'Delete matches',
  79. help='Delete matched emails from server'
  80. ),
  81. 'flag_nonmatching': fields.boolean(
  82. 'Flag nonmatching',
  83. help="Flag emails in the server that don't match any object "
  84. 'in OpenERP'
  85. ),
  86. 'match_first': fields.boolean(
  87. 'Use 1st match',
  88. help='If there are multiple matches, use the first one. If '
  89. 'not checked, multiple matches count as no match at all'
  90. ),
  91. 'domain': fields.char(
  92. 'Domain', size=128, help='Fill in a search '
  93. 'filter to narrow down objects to match'
  94. ),
  95. 'msg_state': fields.selection(
  96. [
  97. ('sent', 'Sent'),
  98. ('received', 'Received'),
  99. ],
  100. 'Message state',
  101. help='The state messages fetched from this folder should be '
  102. 'assigned in OpenERP'
  103. ),
  104. }
  105. _defaults = {
  106. 'flag_nonmatching': True,
  107. 'msg_state': 'received',
  108. }
  109. def get_algorithm(self, cr, uid, ids, context=None):
  110. for this in self.browse(cr, uid, ids, context):
  111. return self._get_match_algorithms()[this.match_algorithm]()
  112. def button_attach_mail_manually(self, cr, uid, ids, context=None):
  113. for this in self.browse(cr, uid, ids, context):
  114. context.update({'default_folder_id': this.id})
  115. return {
  116. 'type': 'ir.actions.act_window',
  117. 'res_model': 'fetchmail.attach.mail.manually',
  118. 'target': 'new',
  119. 'context': context,
  120. 'view_type': 'form',
  121. 'view_mode': 'form',
  122. }