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.

132 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. ###################################################################################
  3. #
  4. # Copyright (C) 2017 MuK IT GmbH
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ###################################################################################
  20. import logging
  21. from odoo import _
  22. from odoo import models, modules, api, fields
  23. from odoo.exceptions import ValidationError, AccessError
  24. _logger = logging.getLogger(__name__)
  25. class RefreshRule(models.Model):
  26. _name = 'muk_web_client_refresh.rule'
  27. _description = "Auto Refresh Rule"
  28. name = fields.Char(
  29. string="Name",
  30. required=True)
  31. model = fields.Many2one(
  32. 'ir.model',
  33. string="Model",
  34. required=True,
  35. help="Select model for which you want to refresh the corresponding views.")
  36. refresh_create = fields.Boolean(
  37. string="Refresh on Create",
  38. default=True)
  39. refresh_write = fields.Boolean(
  40. string="Refresh on Writes",
  41. default=True)
  42. refresh_unlink = fields.Boolean(
  43. string="Refresh on Unlink",
  44. default=True)
  45. _sql_constraints = [
  46. ('model_uniq', 'unique(model)',
  47. ("There is already a rule defined on this model."))
  48. ]
  49. def _register_hook(self):
  50. super(RefreshRule, self)._register_hook()
  51. return self._patch_methods()
  52. @api.multi
  53. def _patch_methods(self):
  54. for rule in self:
  55. model = self.env[rule.model.model]
  56. if rule.refresh_create:
  57. model._patch_method('create', rule._make_create())
  58. if rule.refresh_write:
  59. model._patch_method('write', rule._make_write())
  60. if rule.refresh_unlink:
  61. model._patch_method('unlink', rule._make_unlink())
  62. @api.multi
  63. def _revert_methods(self):
  64. for rule in self:
  65. model = self.env[rule.model.model]
  66. for method in ['create', 'write', 'unlink']:
  67. if getattr(rule, 'refresh_%s' % method) and hasattr(getattr(model, method), 'origin'):
  68. model._revert_method(method)
  69. @api.model
  70. def create(self, vals):
  71. record = super(RefreshRule, self).create(vals)
  72. record._register_hook()
  73. modules.registry.Registry(self.env.cr.dbname).signal_changes()
  74. return record
  75. @api.multi
  76. def write(self, vals):
  77. self._revert_methods()
  78. result = super(RefreshRule, self).write(vals)
  79. self._patch_methods()
  80. modules.registry.Registry(self.env.cr.dbname).signal_changes()
  81. return result
  82. @api.multi
  83. def unlink(self):
  84. self._revert_methods()
  85. modules.registry.Registry(self.env.cr.dbname).signal_changes()
  86. return super(RefreshRule, self).unlink()
  87. @api.multi
  88. def _make_create(self):
  89. @api.model
  90. @api.returns('self', lambda value: value.id)
  91. def create_refresh(self, vals, **kwargs):
  92. result = create_refresh.origin(self, vals, **kwargs)
  93. self.env['bus.bus'].sendone('refresh', [self.env.cr.dbname, self._name, self._uid])
  94. return result
  95. return create_refresh
  96. @api.multi
  97. def _make_write(self):
  98. @api.multi
  99. def write_refresh(self, vals, **kwargs):
  100. result = write_refresh.origin(self, vals, **kwargs)
  101. self.env['bus.bus'].sendone('refresh', [self.env.cr.dbname, self._name, self._uid])
  102. return result
  103. return write_refresh
  104. @api.multi
  105. def _make_unlink(self):
  106. @api.multi
  107. def unlink_refresh(self, **kwargs):
  108. result = unlink_refresh.origin(self, **kwargs)
  109. self.env['bus.bus'].sendone('refresh', [self.env.cr.dbname, self._name, self._uid])
  110. return result
  111. return unlink_refresh