This commit is the 14th commit of a comprehensive refactor of our HTTP framework. See odoo/odoo#78857 for complete historic, discussions and rationnals. * `request.uid = x` => `request.update_env(user=x)`. * `request.context = x` => `request.update_env(context=x)`. * `request.context = dict(request.context, x=y)` => `request.update_context(x=y)`. * `request.cr = None` => `request.cr.close()`. * `http.mono_db()` => `request.db`. * `http.dispatch_rpc()` => `service.dispatch_rpc()`. * `@service.model.check` => `service.model.retrying()`. * `request.endpoint` => `env['ir.http']._match(request.httprequest.path)[0].endpoint`. * `request.routing_iteration `=> `removed`. * `request.jsonrequest` => `request.dispatcher.jsonrequest`. Note that `request.params` is now set much later in the process. If you are in a situation where you values from the query string or the http body you can use `request.get_http_params()`. Note that using the new `request.future_response`, it is possible to add headers and cookies on the response object before the response object is initialized. Please note that headers/cookies saved on the future response will NOT be injected in case of error. PR: odoo#78857 Task: 2571224
49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import json
|
|
import logging
|
|
|
|
from odoo import exceptions, _
|
|
from odoo.http import Controller, request, route
|
|
from odoo.addons.bus.models.bus import dispatch
|
|
|
|
|
|
class BusController(Controller):
|
|
|
|
# override to add channels
|
|
def _poll(self, dbname, channels, last, options):
|
|
channels = list(channels) # do not alter original list
|
|
channels.append('broadcast')
|
|
# update the user presence
|
|
if request.session.uid and 'bus_inactivity' in options:
|
|
request.env['bus.presence'].update(inactivity_period=options.get('bus_inactivity'), identity_field='user_id', identity_value=request.session.uid)
|
|
request.env.cr.close()
|
|
return dispatch.poll(dbname, channels, last, options)
|
|
|
|
@route('/longpolling/poll', type="json", auth="public", cors="*")
|
|
def poll(self, channels, last, options=None):
|
|
if options is None:
|
|
options = {}
|
|
if not dispatch:
|
|
raise Exception("bus.Bus unavailable")
|
|
if [c for c in channels if not isinstance(c, str)]:
|
|
raise Exception("bus.Bus only string channels are allowed.")
|
|
if request.registry.in_test_mode():
|
|
exc = exceptions.UserError(_("bus.Bus not available in test mode"))
|
|
exc.loglevel = logging.INFO
|
|
raise exc
|
|
return self._poll(request.db, channels, last, options)
|
|
|
|
@route('/longpolling/im_status', type="json", auth="user")
|
|
def im_status(self, partner_ids):
|
|
return request.env['res.partner'].with_context(active_test=False).search([('id', 'in', partner_ids)]).read(['im_status'])
|
|
|
|
@route('/longpolling/health', type='http', auth='none', save_session=False)
|
|
def health(self):
|
|
data = json.dumps({
|
|
'status': 'pass',
|
|
})
|
|
headers = [('Content-Type', 'application/json'),
|
|
('Cache-Control', 'no-store')]
|
|
return request.make_response(data, headers)
|