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.

73 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Guewen Baconnier
  5. # Copyright 2015 Camptocamp SA
  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. from openerp.osv import orm, fields
  22. class IrModel(orm.Model):
  23. _inherit = 'ir.model'
  24. def _compute_has_an_active_field(self, cr, uid, ids, name,
  25. args, context=None):
  26. res = {}
  27. for model_id in ids:
  28. active_field_ids = self.pool['ir.model.fields'].search(
  29. cr, uid,
  30. [('model_id', '=', model_id),
  31. ('name', '=', 'active'),
  32. ],
  33. limit=1,
  34. context=context)
  35. res[model_id] = bool(active_field_ids)
  36. return res
  37. def _search_has_an_active_field(self, cr, uid, obj, name, args,
  38. context=None):
  39. if not len(args):
  40. return []
  41. fields_model = self.pool['ir.model.fields']
  42. domain = []
  43. for field, operator, value in args:
  44. assert field == name
  45. active_field_ids = fields_model.search(
  46. cr, uid, [('name', '=', 'active')], context=context)
  47. active_fields = fields_model.read(cr, uid, active_field_ids,
  48. fields=['model_id'],
  49. load='_classic_write',
  50. context=context)
  51. model_ids = [field['model_id'] for field in active_fields]
  52. if operator == '=' or not value:
  53. domain.append(('id', 'in', model_ids))
  54. elif operator == '!=' or value:
  55. domain.append(('id', 'not in', model_ids))
  56. else:
  57. raise AssertionError('operator %s not allowed' % operator)
  58. return domain
  59. _columns = {
  60. 'has_an_active_field': fields.function(
  61. _compute_has_an_active_field,
  62. fnct_search=_search_has_an_active_field,
  63. string='Has an active field',
  64. readonly=True,
  65. type='boolean',
  66. ),
  67. }