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.

62 lines
2.4 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):
  33. result = all(
  34. self.mapped(
  35. lambda this: safe_eval(
  36. this.dynamic_group_condition,
  37. {
  38. 'user': self.env.user,
  39. 'any': any,
  40. 'all': all,
  41. 'filter': filter,
  42. })))
  43. return result
  44. @api.multi
  45. @api.constrains('dynamic_group_condition')
  46. def _check_dynamic_group_condition(self):
  47. try:
  48. self.filtered('is_dynamic').eval_dynamic_group_condition()
  49. except (NameError, SyntaxError, TypeError):
  50. raise exceptions.ValidationError(
  51. _('The condition doesn\'t evaluate correctly!'))
  52. @api.multi
  53. def action_evaluate(self):
  54. res_users = self.env['res.users']
  55. for user in res_users.search([]):
  56. res_users.update_dynamic_groups(user.id, self.env.cr.dbname)