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.

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