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.

63 lines
2.5 KiB

10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 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. from openerp import models, fields, api, exceptions
  22. from openerp.tools.safe_eval import safe_eval
  23. from openerp import _
  24. class res_groups(models.Model):
  25. _inherit = 'res.groups'
  26. is_dynamic = fields.Boolean('Dynamic')
  27. dynamic_group_condition = fields.Text(
  28. 'Condition', help='The condition to be met for a user to be a '
  29. 'member of this group. It is evaluated as python code at login '
  30. 'time, you get `user` passed as a browse record')
  31. @api.multi
  32. def eval_dynamic_group_condition(self, uid=None):
  33. user = self.env['res.users'].browse([uid]) if uid else self.env.user
  34. result = all(
  35. self.mapped(
  36. lambda this: safe_eval(
  37. this.dynamic_group_condition or 'False',
  38. {
  39. 'user': user.sudo(),
  40. 'any': any,
  41. 'all': all,
  42. 'filter': filter,
  43. })))
  44. return result
  45. @api.multi
  46. @api.constrains('dynamic_group_condition')
  47. def _check_dynamic_group_condition(self):
  48. try:
  49. self.filtered('is_dynamic').eval_dynamic_group_condition()
  50. except (NameError, SyntaxError, TypeError):
  51. raise exceptions.ValidationError(
  52. _('The condition doesn\'t evaluate correctly!'))
  53. @api.multi
  54. def action_evaluate(self):
  55. res_users = self.env['res.users']
  56. for user in res_users.search([]):
  57. res_users.update_dynamic_groups(user.id, self.env.cr.dbname)