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.

87 lines
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import time
  22. from openerp.osv.orm import TransientModel
  23. from openerp.osv import fields, expression
  24. from openerp.tools.safe_eval import const_eval
  25. class IrFiltersCombineWithExisting(TransientModel):
  26. _name = 'ir.filters.combine.with.existing'
  27. _description = 'Combine a selection with an existing filter'
  28. _columns = {
  29. 'action': fields.selection(
  30. [('union', 'Union'), ('complement', 'Complement')],
  31. 'Action', required=True),
  32. 'domain': fields.char('Domain', required=True),
  33. 'context': fields.char('Context', required=True),
  34. 'model': fields.char('Model', required=True),
  35. 'filter_id': fields.many2one('ir.filters', 'Filter', required=True),
  36. }
  37. def button_save(self, cr, uid, ids, context=None):
  38. assert len(ids) == 1
  39. this = self.browse(cr, uid, ids[0], context=context)
  40. domain = const_eval(this.domain)
  41. is_frozen = (len(domain) == 1 and
  42. expression.is_leaf(domain[0]) and
  43. domain[0][0] == 'id')
  44. if this.action == 'union':
  45. if is_frozen and this.filter_id.is_frozen:
  46. domain[0][2] = list(set(domain[0][2]).union(
  47. set(const_eval(this.filter_id.domain)[0][2])))
  48. this.filter_id.write({'domain': str(domain)})
  49. else:
  50. this.filter_id.write(
  51. {
  52. 'union_filter_ids': [(0, 0, {
  53. 'name': '%s_%s_%d' % (
  54. this.filter_id.name, 'add', time.time()),
  55. 'active': False,
  56. 'domain': str(domain),
  57. 'context': this.context,
  58. 'model_id': this.model,
  59. 'user_id': uid,
  60. })],
  61. })
  62. elif this.action == 'complement':
  63. if is_frozen and this.filter_id.is_frozen:
  64. complement_set = set(const_eval(this.filter_id.domain)[0][2])
  65. domain[0][2] = list(
  66. complement_set.difference(set(domain[0][2])))
  67. this.filter_id.write({'domain': str(domain)})
  68. else:
  69. this.filter_id.write(
  70. {
  71. 'complement_filter_ids': [(0, 0, {
  72. 'name': '%s_%s_%d' % (
  73. this.filter_id.name, 'remove', time.time()),
  74. 'active': False,
  75. 'domain': str(domain),
  76. 'context': this.context,
  77. 'model_id': this.model,
  78. 'user_id': uid,
  79. })],
  80. })
  81. return {'type': 'ir.actions.act_window.close'}