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.

88 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from datetime import datetime
  5. from odoo import api, fields, models
  6. from odoo.addons.bus.models.bus_presence import AWAY_TIMER, DISCONNECTION_TIMER
  7. from ..status_constants import ONLINE, AWAY, OFFLINE
  8. class BusPresence(models.Model):
  9. _inherit = 'bus.presence'
  10. status_realtime = fields.Selection(
  11. selection=[
  12. (ONLINE, 'Online'),
  13. (AWAY, 'Away'),
  14. (OFFLINE, 'Offline')
  15. ],
  16. string='Realtime IM Status',
  17. compute='_compute_status_realtime',
  18. help='Status that is affected by disconnection '
  19. 'and away timers. Used to override the bus.presence '
  20. 'status field in _get_partners_statuses or '
  21. '_get_users_statuses if the timers have been reached. '
  22. 'If wanting to change the user status, write '
  23. 'directly to the status field.',
  24. )
  25. partner_id = fields.Many2one(
  26. string='Partner',
  27. related='user_id.partner_id',
  28. comodel_name='res.partner',
  29. )
  30. @api.multi
  31. def _get_partners_statuses(self):
  32. self._status_check_disconnection_and_away_timers()
  33. return {rec.partner_id.id: rec.status for rec in self}
  34. @api.multi
  35. def _get_users_statuses(self):
  36. self._status_check_disconnection_and_away_timers()
  37. return {rec.user_id.id: rec.status for rec in self}
  38. @api.multi
  39. def _status_check_disconnection_and_away_timers(self):
  40. """ Overrides user-defined status if timers reached """
  41. for record in self:
  42. status_realtime = record.status_realtime
  43. status_stored = record.status
  44. conditions = (
  45. status_realtime == OFFLINE,
  46. status_realtime == AWAY and status_stored == ONLINE,
  47. )
  48. if any(conditions):
  49. record.status = status_realtime
  50. @api.multi
  51. def _compute_status_realtime(self):
  52. now_dt = datetime.now()
  53. for record in self:
  54. last_poll = fields.Datetime.from_string(
  55. record.last_poll
  56. )
  57. last_presence = fields.Datetime.from_string(
  58. record.last_presence
  59. )
  60. last_poll_s = (now_dt - last_poll).total_seconds()
  61. last_presence_s = (now_dt - last_presence).total_seconds()
  62. if last_poll_s > DISCONNECTION_TIMER:
  63. record.status_realtime = OFFLINE
  64. elif last_presence_s > AWAY_TIMER:
  65. record.status_realtime = AWAY
  66. else:
  67. record.status_realtime = ONLINE