Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d48840

Browse files
committed
[MERGE] Forward-port of saas-3 up to 141e1b2
2 parents 975a9b6 + 141e1b2 commit 4d48840

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

addons/website/models/ir_http.py

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def _dispatch(self):
6161
request.lang = request.context['lang'] = path.pop(1)
6262
path = '/'.join(path) or '/'
6363
return self.reroute(path)
64-
return self._handle_exception(code=404)
6564
return super(ir_http, self)._dispatch()
6665

6766
def reroute(self, path):
@@ -90,8 +89,8 @@ def _postprocess_args(self, arguments, rule):
9089
try:
9190
_, path = rule.build(arguments)
9291
assert path is not None
93-
except Exception:
94-
return self._handle_exception(werkzeug.exceptions.NotFound())
92+
except Exception, e:
93+
return self._handle_exception(e, code=404)
9594

9695
if request.httprequest.method in ('GET', 'HEAD'):
9796
generated_path = werkzeug.url_unquote_plus(path)
@@ -125,51 +124,66 @@ def _serve_attachment(self):
125124
response.data = datas.decode('base64')
126125
return response
127126

128-
def _handle_exception(self, exception=None, code=500):
129-
try:
127+
def _handle_exception(self, exception, code=500):
128+
# This is done first as the attachment path may
129+
# not match any HTTP controller, so the request
130+
# may not be website-enabled.
131+
attach = self._serve_attachment()
132+
if attach:
133+
return attach
134+
135+
is_website_request = bool(getattr(request, 'website_enabled', False) and request.website)
136+
if not is_website_request:
137+
# Don't touch non website requests exception handling
130138
return super(ir_http, self)._handle_exception(exception)
131-
except Exception:
132-
133-
attach = self._serve_attachment()
134-
if attach:
135-
return attach
136-
137-
if getattr(request, 'website_enabled', False) and request.website:
138-
values = dict(
139-
exception=exception,
140-
traceback=traceback.format_exc(exception),
141-
)
142-
if exception:
143-
code = getattr(exception, 'code', code)
144-
if isinstance(exception, ir_qweb.QWebException):
145-
values.update(qweb_exception=exception)
146-
if isinstance(exception.qweb.get('cause'), openerp.exceptions.AccessError):
147-
code = 403
148-
if code == 500:
149-
logger.error("500 Internal Server Error:\n\n%s", values['traceback'])
150-
if 'qweb_exception' in values:
151-
view = request.registry.get("ir.ui.view")
152-
views = view._views_get(request.cr, request.uid, exception.qweb['template'], request.context)
153-
to_reset = [v for v in views if v.model_data_id.noupdate is True]
154-
values['views'] = to_reset
155-
elif code == 403:
156-
logger.warn("403 Forbidden:\n\n%s", values['traceback'])
157-
158-
values.update(
159-
status_message=werkzeug.http.HTTP_STATUS_CODES[code],
160-
status_code=code,
161-
)
162-
163-
if not request.uid:
164-
self._auth_method_public()
165-
166-
try:
167-
html = request.website._render('website.%s' % code, values)
168-
except Exception:
169-
html = request.website._render('website.http_error', values)
170-
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
171-
172-
raise
139+
else:
140+
try:
141+
response = super(ir_http, self)._handle_exception(exception)
142+
if isinstance(response, Exception):
143+
exception = response
144+
else:
145+
# if parent excplicitely returns a plain response, then we don't touch it
146+
return response
147+
except Exception, e:
148+
exception = e
149+
150+
values = dict(
151+
exception=exception,
152+
traceback=traceback.format_exc(exception),
153+
)
154+
code = getattr(exception, 'code', code)
155+
156+
if isinstance(exception, openerp.exceptions.AccessError):
157+
code = 403
158+
159+
if isinstance(exception, ir_qweb.QWebException):
160+
values.update(qweb_exception=exception)
161+
if isinstance(exception.qweb.get('cause'), openerp.exceptions.AccessError):
162+
code = 403
163+
164+
if code == 500:
165+
logger.error("500 Internal Server Error:\n\n%s", values['traceback'])
166+
if 'qweb_exception' in values:
167+
view = request.registry.get("ir.ui.view")
168+
views = view._views_get(request.cr, request.uid, exception.qweb['template'], request.context)
169+
to_reset = [v for v in views if v.model_data_id.noupdate is True]
170+
values['views'] = to_reset
171+
elif code == 403:
172+
logger.warn("403 Forbidden:\n\n%s", values['traceback'])
173+
174+
values.update(
175+
status_message=werkzeug.http.HTTP_STATUS_CODES[code],
176+
status_code=code,
177+
)
178+
179+
if not request.uid:
180+
self._auth_method_public()
181+
182+
try:
183+
html = request.website._render('website.%s' % code, values)
184+
except Exception:
185+
html = request.website._render('website.http_error', values)
186+
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
173187

174188
class ModelConverter(ir.ir_http.ModelConverter):
175189
def __init__(self, url_map, model=False, domain='[]'):

openerp/http.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,10 @@ def dispatch(self, environ, start_response):
12241224
request = self.get_request(httprequest)
12251225

12261226
def _dispatch_nodb():
1227-
func, arguments = self.nodb_routing_map.bind_to_environ(request.httprequest.environ).match()
1227+
try:
1228+
func, arguments = self.nodb_routing_map.bind_to_environ(request.httprequest.environ).match()
1229+
except werkzeug.exceptions.HTTPException, e:
1230+
return request._handle_exception(e)
12281231
request.set_handler(func, arguments, "none")
12291232
result = request.dispatch()
12301233
return result

0 commit comments

Comments
 (0)