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.

86 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 datetime import datetime, timedelta
  24. from collections import defaultdict
  25. from odoo import api, models, fields
  26. _logger = logging.getLogger(__name__)
  27. REFRESH_BEAT = 55
  28. class ServerActions(models.Model):
  29. _inherit = 'ir.actions.server'
  30. _last_refresh_timestamp = defaultdict(lambda: None)
  31. _in_memory_refresh = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))
  32. #----------------------------------------------------------
  33. # Database
  34. #----------------------------------------------------------
  35. state = fields.Selection(
  36. selection_add=[('refresh', 'Refresh Views')])
  37. #----------------------------------------------------------
  38. # Functions
  39. #----------------------------------------------------------
  40. @api.model
  41. def run_action_refresh_multi(self, action, eval_context={}):
  42. if not self.env.context.get('refresh_disable', False) and \
  43. self.env.recompute and self.env.context.get('recompute', True):
  44. cls = type(self)
  45. dbname = self.env.cr.dbname
  46. now_timestamp = datetime.now()
  47. now_delta = now_timestamp - timedelta(seconds=REFRESH_BEAT)
  48. old_timestamp = cls._last_refresh_timestamp[dbname]
  49. record = eval_context.get('record', None)
  50. records = eval_context.get('records', None)
  51. if record and record._log_access and record.create_date == record.write_date:
  52. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].add(True)
  53. else:
  54. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].update(record and record.ids or [])
  55. cls._in_memory_refresh[dbname][action.model_name][self.env.uid].update(records and records.ids or [])
  56. if not old_timestamp or old_timestamp < now_delta:
  57. cls._last_refresh_timestamp[dbname] = now_timestamp
  58. for model, data in cls._in_memory_refresh[dbname].items():
  59. for user, ids in data.items():
  60. create = False
  61. if True in ids:
  62. ids.remove(True)
  63. create = True
  64. self.env['bus.bus'].sendone('refresh', {
  65. 'uid': user,
  66. 'model': model,
  67. 'ids': list(ids),
  68. 'create': create,
  69. })
  70. cls._in_memory_refresh = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))