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.

88 lines
3.8 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. import json
  23. from openerp.osv.orm import TransientModel
  24. from openerp.osv import fields, expression
  25. from openerp.tools.safe_eval import safe_eval
  26. class IrFiltersCombineWithExisting(TransientModel):
  27. _name = 'ir.filters.combine.with.existing'
  28. _description = 'Combine a selection with an existing filter'
  29. _columns = {
  30. 'action': fields.selection(
  31. [('union', 'Union'), ('complement', 'Complement')],
  32. 'Action', required=True),
  33. 'domain': fields.char('Domain', required=True),
  34. 'context': fields.char('Context', required=True),
  35. 'model': fields.char('Model', required=True),
  36. 'filter_id': fields.many2one('ir.filters', 'Filter', required=True),
  37. }
  38. def button_save(self, cr, uid, ids, context=None):
  39. assert len(ids) == 1
  40. this = self.browse(cr, uid, ids[0], context=context)
  41. domain = json.loads(this.domain)
  42. is_frozen = (len(domain) == 1 and
  43. expression.is_leaf(domain[0]) and
  44. domain[0][0] == 'id')
  45. if this.action == 'union':
  46. if is_frozen and this.filter_id.is_frozen:
  47. domain[0][2] = list(set(domain[0][2]).union(
  48. set(safe_eval(this.filter_id.domain)[0][2])))
  49. this.filter_id.write({'domain': str(domain)})
  50. else:
  51. this.filter_id.write(
  52. {
  53. 'union_filter_ids': [(0, 0, {
  54. 'name': '%s_%s_%d' % (
  55. this.filter_id.name, 'add', time.time()),
  56. 'active': False,
  57. 'domain': str(domain),
  58. 'context': this.context,
  59. 'model_id': this.model,
  60. 'user_id': this.filter_id.user_id.id or False,
  61. })],
  62. })
  63. elif this.action == 'complement':
  64. if is_frozen and this.filter_id.is_frozen:
  65. complement_set = set(safe_eval(this.filter_id.domain)[0][2])
  66. domain[0][2] = list(
  67. complement_set.difference(set(domain[0][2])))
  68. this.filter_id.write({'domain': str(domain)})
  69. else:
  70. this.filter_id.write(
  71. {
  72. 'complement_filter_ids': [(0, 0, {
  73. 'name': '%s_%s_%d' % (
  74. this.filter_id.name, 'remove', time.time()),
  75. 'active': False,
  76. 'domain': str(domain),
  77. 'context': this.context,
  78. 'model_id': this.model,
  79. 'user_id': this.filter_id.user_id.id or False,
  80. })],
  81. })
  82. return {'type': 'ir.actions.act_window.close'}