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
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Web Refresh
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. from odoo import api, models, fields
  24. _logger = logging.getLogger(__name__)
  25. REFRESH_BEAT = 55
  26. class ServerActions(models.Model):
  27. _inherit = 'ir.actions.server'
  28. _last_refresh_timestamp = defaultdict(lambda: None)
  29. _in_memory_refresh = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))
  30. #----------------------------------------------------------
  31. # Database
  32. #----------------------------------------------------------
  33. state = fields.Selection(
  34. selection_add=[('refresh', 'Refresh Views')])
  35. #----------------------------------------------------------
  36. # Functions
  37. #----------------------------------------------------------
  38. @api.model
  39. def run_action_refresh_multi(self, action, eval_context={}):
  40. if not self.env.context.get('refresh_disable', False) and \
  41. self.env.recompute and self.env.context.get('recompute', True):
  42. cls = type(self)
  43. dbname = self.env.cr.dbname
  44. now_timestamp = datetime.now()
  45. now_delta = now_timestamp - timedelta(seconds=REFRESH_BEAT)
  46. old_timestamp = cls._last_refresh_timestamp[dbname]
  47. record = eval_context.get('record', None)
  48. records = eval_context.get('records', None)
  49. if record and record._log_access and record.create_date == record.write_date:
  50. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].add(True)
  51. else:
  52. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].update(record and record.ids or [])
  53. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].update(records and records.ids or [])
  54. if not old_timestamp or old_timestamp < now_delta:
  55. cls._last_refresh_timestamp[dbname] = now_timestamp
  56. for model, data in cls._in_memory_refresh[dbname].items():
  57. for user, ids in data.items():
  58. create = False
  59. if True in ids:
  60. ids.remove(True)
  61. create = True
  62. self.env['bus.bus'].sendone('refresh', {
  63. 'uid': user,
  64. 'model': model,
  65. 'ids': list(ids),
  66. 'create': create,
  67. })
  68. cls._in_memory_refresh = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))