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,
@api.model
def update(self, user_presence=True):
presence = self.search([('user_id', '=', self._uid)], limit=1)
# set the default values
send_notification = True
values = {
'last_poll': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'status': presence and presence.status or 'offline'
}
# update the user or a create a new one
if not presence: # create a new presence for the user
values['status'] = 'online'
values['user_id'] = self._uid
self.create(values)
else: # write the user presence if necessary
if user_presence:
values['last_presence'] = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
values['status'] = 'online'
else:
threshold = datetime.datetime.now() - datetime.timedelta(seconds=AWAY_TIMER)
if datetime.datetime.strptime(presence.last_presence, DEFAULT_SERVER_DATETIME_FORMAT) < threshold:
values['status'] = 'away'
send_notification = presence.status != values['status']
# write only if the last_poll is passed TIMEOUT, or if the status has changed
delta = datetime.datetime.utcnow() - datetime.datetime.strptime(presence.last_poll, DEFAULT_SERVER_DATETIME_FORMAT)
if delta > datetime.timedelta(seconds=TIMEOUT) or send_notification:
# Hide transaction serialization errors, which can be ignored, the presence update is not essential
with tools.mute_logger('openerp.sql_db'):
presence.write(values)
# avoid TransactionRollbackError
self.env.cr.commit() # TODO : check if still necessary
# notify if the status has changed
if send_notification: # TODO : add user_id to the channel tuple to allow using user_watch in controller presence
self.env['bus.bus'].sendone((self._cr.dbname, 'bus.presence'), {'id': self._uid, 'im_status': values['status']})
# gc : disconnect the users having a too old last_poll. 1 on 100 chance to do it.
if random.random() < 0.01:
self.check_users_disconnection()
return True
0 Comment(s)