155 lines
6.0 KiB
Python
155 lines
6.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
import traceback
|
|
|
|
import werkzeug
|
|
import werkzeug.routing
|
|
|
|
import openerp
|
|
from openerp.addons.base import ir
|
|
from openerp.addons.website.models.website import slug
|
|
from openerp.http import request
|
|
from openerp.osv import orm
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class ir_http(orm.AbstractModel):
|
|
_inherit = 'ir.http'
|
|
|
|
rerouting_limit = 10
|
|
|
|
def _get_converters(self):
|
|
return dict(
|
|
super(ir_http, self)._get_converters(),
|
|
model=ModelConverter,
|
|
page=PageConverter,
|
|
)
|
|
|
|
def _auth_method_public(self):
|
|
if not request.session.uid:
|
|
request.uid = request.registry['website'].get_public_user(
|
|
request.cr, openerp.SUPERUSER_ID, request.context)
|
|
else:
|
|
request.uid = request.session.uid
|
|
|
|
def _dispatch(self):
|
|
first_pass = not hasattr(request, 'website')
|
|
request.website = None
|
|
func = None
|
|
try:
|
|
func, arguments = self._find_handler()
|
|
request.website_enabled = func.routing.get('website', False)
|
|
except werkzeug.exceptions.NotFound:
|
|
# either we have a language prefixed route, either a real 404
|
|
# in all cases, website processes them
|
|
request.website_enabled = True
|
|
|
|
if request.website_enabled:
|
|
if func:
|
|
self._authenticate(func.routing['auth'])
|
|
else:
|
|
self._auth_method_public()
|
|
request.website = request.registry['website'].get_current_website(request.cr, request.uid, context=request.context)
|
|
if first_pass:
|
|
request.lang = request.website.default_lang_code
|
|
request.context['lang'] = request.lang
|
|
request.website.preprocess_request(request)
|
|
if not func:
|
|
path = request.httprequest.path.split('/')
|
|
langs = [lg[0] for lg in request.website.get_languages()]
|
|
if path[1] in langs:
|
|
request.lang = request.context['lang'] = path.pop(1)
|
|
path = '/'.join(path) or '/'
|
|
return self.reroute(path)
|
|
return self._handle_exception(code=404)
|
|
return super(ir_http, self)._dispatch()
|
|
|
|
def reroute(self, path):
|
|
if not hasattr(request, 'rerouting'):
|
|
request.rerouting = []
|
|
if path in request.rerouting:
|
|
raise Exception("Rerouting loop is forbidden")
|
|
request.rerouting.append(path)
|
|
if len(request.rerouting) > self.rerouting_limit:
|
|
raise Exception("Rerouting limit exceeded")
|
|
request.httprequest.environ['PATH_INFO'] = path
|
|
# void werkzeug cached_property. TODO: find a proper way to do this
|
|
for key in ('path', 'full_path', 'url', 'base_url'):
|
|
request.httprequest.__dict__.pop(key, None)
|
|
|
|
return self._dispatch()
|
|
|
|
def _handle_exception(self, exception=None, code=500):
|
|
if isinstance(exception, werkzeug.exceptions.HTTPException) and exception.response:
|
|
return exception.response
|
|
if getattr(request, 'website_enabled', False) and request.website:
|
|
values = dict(
|
|
exception=exception,
|
|
traceback=traceback.format_exc(exception),
|
|
)
|
|
if exception:
|
|
if isinstance(exception, openerp.exceptions.AccessError):
|
|
code = 403
|
|
else:
|
|
code = getattr(exception, 'code', code)
|
|
values.update(
|
|
qweb_template=getattr(exception, 'qweb_template', None),
|
|
qweb_node=getattr(exception, 'qweb_node', None),
|
|
qweb_eval=getattr(exception, 'qweb_eval', None),
|
|
)
|
|
if code == 500:
|
|
logger.error("500 Internal Server Error:\n\n%s", values['traceback'])
|
|
if values['qweb_template']:
|
|
view = request.registry.get("ir.ui.view")
|
|
views = view._views_get(request.cr, request.uid, values['qweb_template'], request.context)
|
|
to_reset = [view for view in views if view.model_data_id.noupdate == True]
|
|
values['views'] = to_reset
|
|
elif code == 403:
|
|
logger.warn("403 Forbidden:\n\n%s", values['traceback'])
|
|
|
|
values.update(
|
|
status_message=werkzeug.http.HTTP_STATUS_CODES[code],
|
|
status_code=code,
|
|
)
|
|
|
|
if not request.uid:
|
|
self._auth_method_public()
|
|
|
|
try:
|
|
html = request.website._render('website.%s' % code, values)
|
|
except:
|
|
html = request.website._render('website.http_error', values)
|
|
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
|
|
|
|
return super(ir_http, self)._handle_exception(exception)
|
|
|
|
class ModelConverter(ir.ir_http.ModelConverter):
|
|
def __init__(self, url_map, model=False):
|
|
super(ModelConverter, self).__init__(url_map, model)
|
|
self.regex = r'(?:[A-Za-z0-9-_]+?-)?(\d+)(?=$|/)'
|
|
|
|
def to_url(self, value):
|
|
return slug(value)
|
|
|
|
def generate(self, cr, uid, query=None, context=None):
|
|
return request.registry[self.model].name_search(
|
|
cr, uid, name=query or '', context=context)
|
|
|
|
class PageConverter(werkzeug.routing.PathConverter):
|
|
""" Only point of this converter is to bundle pages enumeration logic
|
|
|
|
Sads got: no way to get the view's human-readable name even if one exists
|
|
"""
|
|
def generate(self, cr, uid, query=None, context=None):
|
|
View = request.registry['ir.ui.view']
|
|
views = View.search_read(
|
|
cr, uid, [['page', '=', True]],
|
|
fields=[], order='name', context=context)
|
|
xids = View.get_external_id(
|
|
cr, uid, [view['id'] for view in views], context=context)
|
|
|
|
for view in views:
|
|
xid = xids[view['id']]
|
|
if xid and (not query or query in xid):
|
|
yield xid
|