[IMP] http move db dispatching on the orm level
[odoo/odoo.git] / openerp / addons / base / ir / ir_http.py
1 #----------------------------------------------------------
2 # ir_http modular http routing
3 #----------------------------------------------------------
4 import logging
5
6 import werkzeug.exceptions
7 import werkzeug.routing
8
9 import openerp
10 from openerp import http
11 from openerp.http import request
12 from openerp.osv import osv
13
14 _logger = logging.getLogger(__name__)
15
16 class ir_http(osv.osv):
17     _name = 'ir.http'
18     _description = "HTTP routing"
19
20     def __init__(self, registry, cr):
21         osv.osv.__init__(self, registry, cr)
22
23     def _find_handler(self):
24         # TODO move to __init__(self, registry, cr)
25         if not hasattr(self, 'routing_map'):
26             _logger.info("Generating routing map")
27             cr = request.cr
28             m = request.registry.get('ir.module.module')
29             ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')])
30             installed = set(x['name'] for x in m.read(cr, 1, ids, ['name']))
31             mods = ['', "web"] + sorted(installed)
32             self.routing_map = http.routing_map(mods, False)
33
34         # fallback to non-db handlers
35         path = request.httprequest.path
36         urls = self.routing_map.bind_to_environ(request.httprequest.environ)
37
38         return urls.match(path)
39
40     def _auth_method_user(self):
41         request.uid = request.session.uid
42         if not request.uid:
43             raise SessionExpiredException("Session expired")
44
45     def _auth_method_admin(self):
46         if not request.db:
47             raise SessionExpiredException("No valid database for request %s" % request.httprequest)
48         request.uid = openerp.SUPERUSER_ID
49
50     def _auth_method_none(self):
51         request.disable_db = True
52         request.uid = None
53
54     def _authenticate(self, func, arguments):
55         auth_method = getattr(func, "auth", "user")
56         if request.session.uid:
57             try:
58                 request.session.check_security()
59             except SessionExpiredException, e:
60                 request.session.logout()
61                 raise SessionExpiredException("Session expired for request %s" % request.httprequest)
62         getattr(self, "_auth_method_%s" % auth_method)()
63         return auth_method
64
65     def _handle_404(self, exception):
66         raise exception
67
68     def _handle_403(self, exception):
69         raise exception
70
71     def _handle_500(self, exception):
72         raise exception
73
74     def _dispatch(self):
75         # locate the controller method
76         try:
77             func, arguments = self._find_handler()
78         except werkzeug.exceptions.NotFound, e:
79             return self._handle_404(e)
80
81         # check authentication level
82         try:
83             auth_method = self._authenticate(func, arguments)
84         except werkzeug.exceptions.NotFound, e:
85             return self._handle_403(e)
86
87         # set and execute handler
88         try:
89             request.set_handler(func, arguments, auth_method)
90             result = request.dispatch()
91         except Exception, e:
92             return  self._handle_500(e)
93         return result
94
95 # vim:et: