Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Register the given presence of the current user in openerp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 352
    Comment on it

    In openerp register the given presence of the current user, and trigger a im_status change if necessary. The status will not be written or sent if not necessary and param user_presence : True, if the user (self._uid) is still detected using its browser.
    use this function show below,

    1. @api.model
    2. def update(self, user_presence=True):
    3.  
    4. presence = self.search([('user_id', '=', self._uid)], limit=1)
    5. # set the default values
    6. send_notification = True
    7. values = {
    8. 'last_poll': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
    9. 'status': presence and presence.status or 'offline'
    10. }
    11. # update the user or a create a new one
    12. if not presence: # create a new presence for the user
    13. values['status'] = 'online'
    14. values['user_id'] = self._uid
    15. self.create(values)
    16. else: # write the user presence if necessary
    17. if user_presence:
    18. values['last_presence'] = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
    19. values['status'] = 'online'
    20. else:
    21. threshold = datetime.datetime.now() - datetime.timedelta(seconds=AWAY_TIMER)
    22. if datetime.datetime.strptime(presence.last_presence, DEFAULT_SERVER_DATETIME_FORMAT) < threshold:
    23. values['status'] = 'away'
    24. send_notification = presence.status != values['status']
    25. # write only if the last_poll is passed TIMEOUT, or if the status has changed
    26. delta = datetime.datetime.utcnow() - datetime.datetime.strptime(presence.last_poll, DEFAULT_SERVER_DATETIME_FORMAT)
    27. if delta > datetime.timedelta(seconds=TIMEOUT) or send_notification:
    28. # Hide transaction serialization errors, which can be ignored, the presence update is not essential
    29. with tools.mute_logger('openerp.sql_db'):
    30. presence.write(values)
    31. # avoid TransactionRollbackError
    32. self.env.cr.commit() # TODO : check if still necessary
    33. # notify if the status has changed
    34. if send_notification: # TODO : add user_id to the channel tuple to allow using user_watch in controller presence
    35. self.env['bus.bus'].sendone((self._cr.dbname, 'bus.presence'), {'id': self._uid, 'im_status': values['status']})
    36. # gc : disconnect the users having a too old last_poll. 1 on 100 chance to do it.
    37. if random.random() < 0.01:
    38. self.check_users_disconnection()
    39. return True

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: