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.

96 lines
3.2 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2017 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import hashlib
  21. import logging
  22. import itertools
  23. from odoo import _, SUPERUSER_ID
  24. from odoo import models, api, fields
  25. from odoo.exceptions import AccessError
  26. _logger = logging.getLogger(__name__)
  27. class LockingModel(models.AbstractModel):
  28. _name = 'muk_security.mixins.locking'
  29. _description = 'Locking Mixin'
  30. #----------------------------------------------------------
  31. # Database
  32. #----------------------------------------------------------
  33. locked_by = fields.Many2one(
  34. comodel_name='res.users',
  35. string="Locked by")
  36. is_locked = fields.Boolean(
  37. compute='_compute_locked',
  38. string="Locked")
  39. is_lock_editor = fields.Boolean(
  40. compute='_compute_locked',
  41. string="Editor")
  42. #----------------------------------------------------------
  43. # Locking
  44. #----------------------------------------------------------
  45. @api.multi
  46. def lock(self):
  47. self.write({'locked_by': self.env.uid})
  48. @api.multi
  49. def unlock(self):
  50. self.write({'locked_by': None})
  51. @api.multi
  52. def check_lock(self, *largs, **kwargs):
  53. for record in self:
  54. if record.locked_by.exists() and not record.locked_by.id in (self.env.uid, SUPERUSER_ID):
  55. raise AccessError(_("The record (%s [%s]) is locked, by an other user.") % (record._description, record.id))
  56. #----------------------------------------------------------
  57. # Read, View
  58. #----------------------------------------------------------
  59. @api.depends('locked_by')
  60. def _compute_locked(self):
  61. for record in self:
  62. if record.locked_by.exists():
  63. record.update({'is_locked': True, 'is_lock_editor': record.locked_by.id == record.env.uid})
  64. else:
  65. record.update({'is_locked': False, 'is_lock_editor': False})
  66. #----------------------------------------------------------
  67. # Create, Update, Delete
  68. #----------------------------------------------------------
  69. @api.multi
  70. def write(self, vals):
  71. self.check_lock()
  72. return super(BaseModelLocking, self).write(vals)
  73. @api.multi
  74. def unlink(self):
  75. self.check_lock()
  76. return super(BaseModelLocking, self).unlink()