1 #----------------------------------------------------------
2 # ir_http modular http routing
3 #----------------------------------------------------------
8 import werkzeug.exceptions
9 import werkzeug.routing
14 import openerp.exceptions
15 from openerp import http
16 from openerp.http import request
17 from openerp.osv import osv, orm
19 _logger = logging.getLogger(__name__)
21 UID_PLACEHOLDER = object()
23 class ModelConverter(werkzeug.routing.BaseConverter):
25 def __init__(self, url_map, model=False):
26 super(ModelConverter, self).__init__(url_map)
28 self.regex = '([0-9]+)'
30 def to_python(self, value):
31 m = re.match(self.regex, value)
32 return request.registry[self.model].browse(
33 request.cr, UID_PLACEHOLDER, int(m.group(1)), context=request.context)
35 def to_url(self, value):
38 class ModelsConverter(werkzeug.routing.BaseConverter):
40 def __init__(self, url_map, model=False):
41 super(ModelsConverter, self).__init__(url_map)
43 # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
44 self.regex = '([0-9,]+)'
46 def to_python(self, value):
47 return request.registry[self.model].browse(request.cr, UID_PLACEHOLDER, [int(i) for i in value.split(',')], context=request.context)
49 def to_url(self, value):
50 return ",".join(i.id for i in value)
52 class ir_http(osv.AbstractModel):
54 _description = "HTTP routing"
56 def _get_converters(self):
57 return {'model': ModelConverter, 'models': ModelsConverter}
59 def _find_handler(self, return_rule=False):
60 return self.routing_map().bind_to_environ(request.httprequest.environ).match(return_rule=return_rule)
62 def _auth_method_user(self):
63 request.uid = request.session.uid
65 if not request.params.get('noredirect'):
66 query = werkzeug.urls.url_encode({
67 'redirect': request.httprequest.url,
69 response = werkzeug.utils.redirect('/web/login?%s' % query)
70 werkzeug.exceptions.abort(response)
71 raise http.SessionExpiredException("Session expired")
73 def _auth_method_none(self):
76 def _auth_method_public(self):
77 if not request.session.uid:
78 dummy, request.uid = self.pool['ir.model.data'].get_object_reference(request.cr, openerp.SUPERUSER_ID, 'base', 'public_user')
80 request.uid = request.session.uid
82 def _authenticate(self, auth_method='user'):
84 if request.session.uid:
86 request.session.check_security()
87 # what if error in security.check()
88 # -> res_users.check()
89 # -> res_users.check_credentials()
90 except (openerp.exceptions.AccessDenied, openerp.http.SessionExpiredException):
91 # All other exceptions mean undetermined status (e.g. connection pool full),
93 request.session.logout()
94 getattr(self, "_auth_method_%s" % auth_method)()
95 except (openerp.exceptions.AccessDenied, openerp.http.SessionExpiredException):
98 _logger.exception("Exception during request Authentication.")
99 raise openerp.exceptions.AccessDenied()
102 def _handle_exception(self, exception):
103 # If handle_exception returns something different than None, it will be used as a response
104 return request._handle_exception(exception)
107 # locate the controller method
109 rule, arguments = self._find_handler(return_rule=True)
111 except werkzeug.exceptions.NotFound, e:
112 return self._handle_exception(e)
114 # check authentication level
116 auth_method = self._authenticate(func.routing["auth"])
118 # force a Forbidden exception with the original traceback
119 return self._handle_exception(
120 convert_exception_to(
121 werkzeug.exceptions.Forbidden))
123 processing = self._postprocess_args(arguments, rule)
128 # set and execute handler
130 request.set_handler(func, arguments, auth_method)
131 result = request.dispatch()
132 if isinstance(result, Exception):
135 return self._handle_exception(e)
139 def _postprocess_args(self, arguments, rule):
140 """ post process arg to set uid on browse records """
141 for arg in arguments.itervalues():
142 if isinstance(arg, orm.browse_record) and arg._uid is UID_PLACEHOLDER:
143 arg._uid = request.uid
147 return self._handle_exception(werkzeug.exceptions.NotFound())
149 def routing_map(self):
150 if not hasattr(self, '_routing_map'):
151 _logger.info("Generating routing map")
153 m = request.registry.get('ir.module.module')
154 ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')], context=request.context)
155 installed = set(x['name'] for x in m.read(cr, 1, ids, ['name'], context=request.context))
156 if openerp.tools.config['test_enable']:
157 installed.add(openerp.modules.module.current_test)
158 mods = [''] + openerp.conf.server_wide_modules + sorted(installed)
159 self._routing_map = http.routing_map(mods, False, converters=self._get_converters())
161 return self._routing_map
163 def convert_exception_to(to_type, with_message=False):
164 """ Should only be called from an exception handler. Fetches the current
165 exception data from sys.exc_info() and creates a new exception of type
166 ``to_type`` with the original traceback.
168 If ``with_message`` is ``True``, sets the new exception's message to be
169 the stringification of the original exception. If ``False``, does not
170 set the new exception's message. Otherwise, uses ``with_message`` as the
171 new exception's message.
173 :type with_message: str|bool
175 etype, original, tb = sys.exc_info()
177 if with_message is False:
179 elif with_message is True:
180 message = str(original)
182 message = str(with_message)
184 raise to_type, message, tb