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.

83 lines
2.5 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. @api.multi
  20. @api.depends('line_ids.user_id')
  21. def _compute_user_ids(self):
  22. for role in self:
  23. role.user_ids = role.line_ids.mapped('user_id')
  24. @api.model
  25. def create(self, vals):
  26. new_record = super(ResUsersRole, self).create(vals)
  27. new_record.update_users()
  28. return new_record
  29. @api.multi
  30. def write(self, vals):
  31. res = super(ResUsersRole, self).write(vals)
  32. self.update_users()
  33. return res
  34. @api.multi
  35. def update_users(self):
  36. """Update all the users concerned by the roles identified by `ids`."""
  37. users = self.mapped('user_ids')
  38. users.set_groups_from_roles()
  39. return True
  40. @api.model
  41. def cron_update_users(self):
  42. logging.info(u"Update user roles")
  43. self.search([]).update_users()
  44. class ResUsersRoleLine(models.Model):
  45. _name = 'res.users.role.line'
  46. _description = 'Users associated to a role'
  47. role_id = fields.Many2one(
  48. 'res.users.role', string=u"Role", ondelete='cascade')
  49. user_id = fields.Many2one(
  50. 'res.users', string=u"User")
  51. date_from = fields.Date(u"From")
  52. date_to = fields.Date(u"To")
  53. is_enabled = fields.Boolean(u"Enabled", compute='_compute_is_enabled')
  54. @api.multi
  55. @api.depends('date_from', 'date_to')
  56. def _compute_is_enabled(self):
  57. today = datetime.date.today()
  58. for role_line in self:
  59. role_line.is_enabled = True
  60. if role_line.date_from:
  61. date_from = fields.Date.from_string(role_line.date_from)
  62. if date_from > today:
  63. role_line.is_enabled = False
  64. if role_line.date_to:
  65. date_to = fields.Date.from_string(role_line.date_to)
  66. if today > date_to:
  67. role_line.is_enabled = False