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.

89 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ABF OSIELL <http://osiell.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import datetime
  5. import logging
  6. from openerp import api, fields, models
  7. _logger = logging.getLogger(__name__)
  8. class ResUsersRole(models.Model):
  9. _name = 'res.users.role'
  10. _inherits = {'res.groups': 'group_id'}
  11. _description = "User role"
  12. group_id = fields.Many2one(
  13. 'res.groups', required=True, ondelete='cascade',
  14. readonly=True, string=u"Associated group")
  15. line_ids = fields.One2many(
  16. 'res.users.role.line', 'role_id', string=u"Users")
  17. user_ids = fields.One2many(
  18. 'res.users', string=u"Users", compute='_compute_user_ids')
  19. _defaults = { # pylint: disable=attribute-deprecated
  20. 'category_id': api.model(
  21. lambda cls: cls.env.ref(
  22. 'base_user_role.ir_module_category_role').id),
  23. }
  24. @api.multi
  25. @api.depends('line_ids.user_id')
  26. def _compute_user_ids(self):
  27. for role in self:
  28. role.user_ids = role.line_ids.mapped('user_id')
  29. @api.model
  30. def create(self, vals):
  31. new_record = super(ResUsersRole, self).create(vals)
  32. new_record.update_users()
  33. return new_record
  34. @api.multi
  35. def write(self, vals):
  36. res = super(ResUsersRole, self).write(vals)
  37. self.update_users()
  38. return res
  39. @api.multi
  40. def update_users(self):
  41. """Update all the users concerned by the roles identified by `ids`."""
  42. users = self.mapped('user_ids')
  43. users.set_groups_from_roles()
  44. return True
  45. @api.model
  46. def cron_update_users(self):
  47. logging.info(u"Update user roles")
  48. self.search([]).update_users()
  49. class ResUsersRoleLine(models.Model):
  50. _name = 'res.users.role.line'
  51. _description = 'Users associated to a role'
  52. role_id = fields.Many2one(
  53. 'res.users.role', string=u"Role", ondelete='cascade')
  54. user_id = fields.Many2one(
  55. 'res.users', string=u"User")
  56. date_from = fields.Date(u"From")
  57. date_to = fields.Date(u"To")
  58. is_enabled = fields.Boolean(u"Enabled", compute='_compute_is_enabled')
  59. @api.multi
  60. @api.depends('date_from', 'date_to')
  61. def _compute_is_enabled(self):
  62. today = datetime.date.today()
  63. for role_line in self:
  64. role_line.is_enabled = True
  65. if role_line.date_from:
  66. date_from = fields.Date.from_string(role_line.date_from)
  67. if date_from > today:
  68. role_line.is_enabled = False
  69. if role_line.date_to:
  70. date_to = fields.Date.from_string(role_line.date_to)
  71. if today > date_to:
  72. role_line.is_enabled = False