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
2.5 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 odoo import api, fields, models
  5. from odoo.addons.bus.models.bus_presence import AWAY_TIMER
  6. from odoo.addons.bus.models.bus_presence import DISCONNECTION_TIMER
  7. from ..status_constants import ONLINE, AWAY, OFFLINE
  8. class ResPartner(models.Model):
  9. _inherit = 'res.partner'
  10. im_status_custom = fields.Selection(
  11. selection=[
  12. (ONLINE, 'Online'),
  13. (AWAY, 'Away'),
  14. (OFFLINE, 'Offline'),
  15. ],
  16. string='Status',
  17. default=ONLINE,
  18. )
  19. @api.model
  20. def _get_partners_presence(self):
  21. """ This method gets any relevant partners' im status.
  22. Example:
  23. .. code-block:: python
  24. res = self.env['res.partner']._get_partners_presence()
  25. im_status = res.get(partner.id, OFFLINE)
  26. Returns:
  27. dict: Structured like so: {partner_id: im_status}
  28. """
  29. self.env.cr.execute(
  30. """
  31. SELECT
  32. U.partner_id as id,
  33. CASE WHEN age(now() AT TIME ZONE 'UTC', B.last_poll)
  34. > interval %s THEN 'offline'
  35. WHEN age(now() AT TIME ZONE 'UTC', B.last_presence)
  36. > interval %s THEN 'away'
  37. ELSE 'online'
  38. END as status
  39. FROM bus_presence B
  40. JOIN res_users U ON B.user_id = U.id
  41. WHERE U.partner_id IN %s AND U.active = 't'
  42. """,
  43. ("%s seconds" % DISCONNECTION_TIMER,
  44. "%s seconds" % AWAY_TIMER, tuple(self.ids))
  45. )
  46. all_status = self.env.cr.dictfetchall()
  47. all_status_dict = dict(
  48. ((status['id'], status['status']) for status in all_status)
  49. )
  50. return all_status_dict
  51. @api.multi
  52. def _compute_im_status(self):
  53. presence = self._get_partners_presence()
  54. for record in self:
  55. record.im_status = presence.get(record.id, OFFLINE)
  56. computed = record.im_status
  57. custom = record.im_status_custom
  58. if computed == OFFLINE:
  59. record.im_status_custom = computed
  60. elif custom in (AWAY, OFFLINE):
  61. record.im_status = custom
  62. elif computed == AWAY and custom == ONLINE:
  63. record.im_status_custom = computed