1 #----------------------------------------------------------
2 # ir_http modular http routing
3 #----------------------------------------------------------
6 import werkzeug.exceptions
7 import werkzeug.routing
10 from openerp import http
11 from openerp.http import request
12 from openerp.osv import osv, orm
14 _logger = logging.getLogger(__name__)
17 # FIXME: replace by proxy on request.uid?
20 class ModelConverter(werkzeug.routing.BaseConverter):
22 def __init__(self, url_map, model=False):
23 super(ModelConverter, self).__init__(url_map)
25 # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
26 self.regex = '([0-9]+)'
28 def to_python(self, value):
30 # - raise routing.ValidationError() if no browse record can be createdm
32 return request.registry[self.model].browse(request.cr, _uid, int(value), context=request.context)
34 def to_url(self, value):
37 class ModelsConverter(werkzeug.routing.BaseConverter):
39 def __init__(self, url_map, model=False):
40 super(ModelsConverter, self).__init__(url_map)
42 # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
43 self.regex = '([0-9,]+)'
45 def to_python(self, value):
47 # - raise routing.ValidationError() if no browse record can be createdm
49 return request.registry[self.model].browse(request.cr, _uid, [int(i) for i in value.split(',')], context=request.context)
51 def to_url(self, value):
52 return ",".join(i.id for i in value)
54 class ir_http(osv.AbstractModel):
57 _description = "HTTP routing"
59 def _get_converters(self):
60 return {'model': ModelConverter, 'models': ModelsConverter}
62 def _find_handler(self):
63 return self.routing_map.bind_to_environ(request.httprequest.environ).match()
65 def _auth_method_user(self):
66 request.uid = request.session.uid
68 raise http.SessionExpiredException("Session expired")
70 def _auth_method_admin(self):
72 raise http.SessionExpiredException("No valid database for request %s" % request.httprequest)
73 request.uid = openerp.SUPERUSER_ID
75 def _auth_method_none(self):
76 request.disable_db = True
79 def _authenticate(self, func, arguments):
80 auth_method = getattr(func, "auth", "user")
81 if request.session.uid:
83 request.session.check_security()
84 # what if error in security.check()
85 # -> res_users.check()
86 # -> res_users.check_credentials()
87 except http.SessionExpiredException:
88 request.session.logout()
89 raise http.SessionExpiredException("Session expired for request %s" % request.httprequest)
90 getattr(self, "_auth_method_%s" % auth_method)()
93 def _handle_404(self, exception):
96 def _handle_403(self, exception):
99 def _handle_500(self, exception):
103 # locate the controller method
105 func, arguments = self._find_handler()
106 except werkzeug.exceptions.NotFound, e:
107 return self._handle_404(e)
109 # check authentication level
111 auth_method = self._authenticate(func, arguments)
112 except werkzeug.exceptions.NotFound, e:
113 return self._handle_403(e)
115 # post process arg to set uid on browse records
116 for arg in arguments:
117 if isinstance(arg, orm.browse_record) and arg._uid is _uid:
118 arg._uid = request.uid
120 # set and execute handler
122 request.set_handler(func, arguments, auth_method)
123 result = request.dispatch()
124 if isinstance(result, Exception):
127 fn = getattr(self, '_handle_%s' % getattr(e, 'code', 500),
134 def routing_map(self):
135 if not hasattr(self, '_routing_map'):
136 _logger.info("Generating routing map")
138 m = request.registry.get('ir.module.module')
139 ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')], context=request.context)
140 installed = set(x['name'] for x in m.read(cr, 1, ids, ['name'], context=request.context))
141 mods = ['', "web"] + sorted(installed)
142 self._routing_map = http.routing_map(mods, False, converters=self._get_converters())
144 return self._routing_map