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.

133 lines
4.8 KiB

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 and not hasattr(model, "rule_refresh_create"):
  57. model._patch_method('create', rule._make_create())
  58. setattr(type(model), "rule_refresh_create", True)
  59. if rule.refresh_write and not hasattr(model, "rule_refresh_write"):
  60. model._patch_method('write', rule._make_write())
  61. setattr(type(model), "rule_refresh_write", True)
  62. if rule.refresh_unlink and not hasattr(model, "rule_refresh_unlink"):
  63. model._patch_method('unlink', rule._make_unlink())
  64. setattr(type(model), "rule_refresh_unlink", True)
  65. @api.multi
  66. def _revert_methods(self):
  67. for rule in self:
  68. model = self.env[rule.model._name]
  69. for method in ['create', 'write', 'unlink']:
  70. if getattr(rule, 'refresh_%s' % method) and hasattr(getattr(model, method), 'origin'):
  71. model._revert_method(method)
  72. delattr(type(model), 'refresh_ruled_%s' % method)
  73. @api.model
  74. def create(self, vals):
  75. record = super(RefreshRule, self).create(vals)
  76. record._register_hook()
  77. modules.registry.RegistryManager.signal_registry_change(self.env.cr.dbname)
  78. return record
  79. @api.multi
  80. def write(self, vals):
  81. super(RefreshRule, self).write(vals)
  82. self._register_hook()
  83. modules.registry.RegistryManager.signal_registry_change(self.env.cr.dbname)
  84. return True
  85. @api.multi
  86. def unlink(self):
  87. self._revert_methods()
  88. modules.registry.RegistryManager.signal_registry_change(self.env.cr.dbname)
  89. return super(RefreshRule, self).unlink()
  90. @api.multi
  91. def _make_create(self):
  92. @api.model
  93. @api.returns('self', lambda value: value.id)
  94. def create_refresh(self, vals, **kwargs):
  95. result = create_refresh.origin(self, vals, **kwargs)
  96. self.env['bus.bus'].sendone("%s_refresh" % self.env.cr.dbname, self._name)
  97. return result
  98. return create_refresh
  99. @api.multi
  100. def _make_write(self):
  101. @api.multi
  102. def write_refresh(self, vals, **kwargs):
  103. result = write_refresh.origin(self, vals, **kwargs)
  104. self.env['bus.bus'].sendone("%s_refresh" % self.env.cr.dbname, self._name)
  105. return result
  106. return write_refresh
  107. @api.multi
  108. def _make_unlink(self):
  109. @api.multi
  110. def unlink_refresh(self, **kwargs):
  111. result = unlink_refresh.origin(self, **kwargs)
  112. self.env['bus.bus'].sendone("%s_refresh" % self.env.cr.dbname, self._name)
  113. return result
  114. return unlink_refresh