1 #----------------------------------------------------------
2 # ir_http modular http routing
3 #----------------------------------------------------------
8 import werkzeug.exceptions
9 import werkzeug.routing
12 from openerp import http
13 from openerp.http import request
14 from openerp.osv import osv, orm
16 _logger = logging.getLogger(__name__)
18 UID_PLACEHOLDER = object()
20 class ModelConverter(werkzeug.routing.BaseConverter):
22 def __init__(self, url_map, model=False):
23 super(ModelConverter, self).__init__(url_map)
25 self.regex = '([0-9]+)'
27 def to_python(self, value):
28 m = re.match(self.regex, value)
29 return request.registry[self.model].browse(
30 request.cr, UID_PLACEHOLDER, int(m.group(1)), context=request.context)
32 def to_url(self, value):
35 class ModelsConverter(werkzeug.routing.BaseConverter):
37 def __init__(self, url_map, model=False):
38 super(ModelsConverter, self).__init__(url_map)
40 # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
41 self.regex = '([0-9,]+)'
43 def to_python(self, value):
44 return request.registry[self.model].browse(request.cr, UID_PLACEHOLDER, [int(i) for i in value.split(',')], context=request.context)
46 def to_url(self, value):
47 return ",".join(i.id for i in value)
49 class ir_http(osv.AbstractModel):
51 _description = "HTTP routing"
53 def _get_converters(self):
54 return {'model': ModelConverter, 'models': ModelsConverter}
56 def _find_handler(self):
57 return self.routing_map().bind_to_environ(request.httprequest.environ).match()
59 def _auth_method_user(self):
60 request.uid = request.session.uid
62 raise http.SessionExpiredException("Session expired")
64 def _auth_method_admin(self):
66 raise http.SessionExpiredException("No valid database for request %s" % request.httprequest)
67 request.uid = openerp.SUPERUSER_ID
69 def _auth_method_none(self):
70 request.disable_db = True
73 def _auth_method_public(self):
74 if not request.session.uid:
75 dummy, request.uid = self.pool['ir.model.data'].get_object_reference(request.cr, openerp.SUPERUSER_ID, 'base', 'public_user')
77 request.uid = request.session.uid
79 def _authenticate(self, auth_method='user'):
80 if request.session.uid:
82 request.session.check_security()
83 # what if error in security.check()
84 # -> res_users.check()
85 # -> res_users.check_credentials()
87 request.session.logout()
88 getattr(self, "_auth_method_%s" % auth_method)()
91 def _handle_exception(self, exception):
92 # If handle exception return something different than None, it will be used as a response
96 # locate the controller method
98 func, arguments = self._find_handler()
99 except werkzeug.exceptions.NotFound, e:
100 return self._handle_exception(e)
102 # check authentication level
104 auth_method = self._authenticate(func.routing["auth"])
106 # force a Forbidden exception with the original traceback
107 return self._handle_exception(
108 convert_exception_to(
109 werkzeug.exceptions.Forbidden))
111 processing = self._postprocess_args(arguments)
116 # set and execute handler
118 request.set_handler(func, arguments, auth_method)
119 result = request.dispatch()
120 if isinstance(result, Exception):
123 return self._handle_exception(e)
127 def _postprocess_args(self, arguments):
128 """ post process arg to set uid on browse records """
129 for arg in arguments.itervalues():
130 if isinstance(arg, orm.browse_record) and arg._uid is UID_PLACEHOLDER:
131 arg._uid = request.uid
135 return self._handle_exception(werkzeug.exceptions.NotFound())
137 def routing_map(self):
138 if not hasattr(self, '_routing_map'):
139 _logger.info("Generating routing map")
141 m = request.registry.get('ir.module.module')
142 ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')], context=request.context)
143 installed = set(x['name'] for x in m.read(cr, 1, ids, ['name'], context=request.context))
144 mods = ['', "web"] + sorted(installed)
145 self._routing_map = http.routing_map(mods, False, converters=self._get_converters())
147 return self._routing_map
149 def convert_exception_to(to_type, with_message=False):
150 """ Should only be called from an exception handler. Fetches the current
151 exception data from sys.exc_info() and creates a new exception of type
152 ``to_type`` with the original traceback.
154 If ``with_message`` is ``True``, sets the new exception's message to be
155 the stringification of the original exception. If ``False``, does not
156 set the new exception's message. Otherwise, uses ``with_message`` as the
157 new exception's message.
159 :type with_message: str|bool
161 etype, original, tb = sys.exc_info()
163 if with_message is False:
165 elif with_message is True:
166 message = str(original)
168 message = str(with_message)
170 raise to_type, message, tb