[FIX] use AbstractModel, use session.db to ir_http dispatch for none controllers
[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 RequestUID(object):
17     pass
18
19 class ModelConverter(werkzeug.routing.BaseConverter):
20
21     def __init__(self, url_map, model=False):
22         super(ModelConverter, self).__init__(url_map)
23         self.model = model
24         # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
25         self.regex = '([0-9]+)'
26
27     def to_python(self, value):
28         # TODO:
29         # - raise routing.ValidationError() if no browse record can be createdm
30         # - support slug 
31         return request.registry[self.model].browse(request.cr, RequestUID(), int(value), context=request.context)
32
33     def to_url(self, value):
34         return value.id
35
36 class ModelsConverter(werkzeug.routing.BaseConverter):
37
38     def __init__(self, url_map, model=False):
39         super(ModelsConverter, self).__init__(url_map)
40         self.model = model
41         # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
42         self.regex = '([0-9,]+)'
43
44     def to_python(self, value):
45         # TODO:
46         # - raise routing.ValidationError() if no browse record can be createdm
47         # - support slug
48         return request.registry[self.model].browse(request.cr, RequestUID(), [int(i) for i in value.split(',')], context=request.context)
49
50     def to_url(self, value):
51         return ",".join([i.id for i in value])
52
53 class ir_http(osv.AbstractModel):
54     _name = 'ir.http'
55     
56     _description = "HTTP routing"
57
58     def __init__(self, registry, cr):
59         osv.osv.__init__(self, registry, cr)
60
61     def _find_handler(self):
62         # TODO move to __init__(self, registry, cr)
63         if not hasattr(self, 'routing_map'):
64             _logger.info("Generating routing map")
65             cr = request.cr
66             m = request.registry.get('ir.module.module')
67             ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')])
68             installed = set(x['name'] for x in m.read(cr, 1, ids, ['name']))
69             mods = ['', "web"] + sorted(installed)
70             self.routing_map = http.routing_map(mods, False, converters={'model': ModelConverter, 'models': ModelsConverter})
71
72         # fallback to non-db handlers
73         path = request.httprequest.path
74         urls = self.routing_map.bind_to_environ(request.httprequest.environ)
75
76         return urls.match(path)
77
78     def _auth_method_user(self):
79         request.uid = request.session.uid
80         if not request.uid:
81             raise SessionExpiredException("Session expired")
82
83     def _auth_method_admin(self):
84         if not request.db:
85             raise SessionExpiredException("No valid database for request %s" % request.httprequest)
86         request.uid = openerp.SUPERUSER_ID
87
88     def _auth_method_none(self):
89         request.disable_db = True
90         request.uid = None
91
92     def _authenticate(self, func, arguments):
93         auth_method = getattr(func, "auth", "user")
94         if request.session.uid:
95             try:
96                 request.session.check_security()
97             except SessionExpiredException, e:
98                 request.session.logout()
99                 raise SessionExpiredException("Session expired for request %s" % request.httprequest)
100         getattr(self, "_auth_method_%s" % auth_method)()
101         return auth_method
102
103     def _handle_404(self, exception):
104         raise exception
105
106     def _handle_403(self, exception):
107         raise exception
108
109     def _handle_500(self, exception):
110         raise exception
111
112     def _dispatch(self):
113         # locate the controller method
114         try:
115             func, arguments = self._find_handler()
116         except werkzeug.exceptions.NotFound, e:
117             return self._handle_404(e)
118
119         # check authentication level
120         try:
121             auth_method = self._authenticate(func, arguments)
122         except werkzeug.exceptions.NotFound, e:
123             return self._handle_403(e)
124
125         # post process arg to set uid on browse records
126         for arg in arguments:
127             if isinstance(arg, openerp.osv.orm.browse_record) and isinstance(arg._uid, RequestUID):
128                 arg._uid = request.uid
129
130         # set and execute handler
131         try:
132             request.set_handler(func, arguments, auth_method)
133             result = request.dispatch()
134         except Exception, e:
135             return  self._handle_500(e)
136         return result
137
138 # vim:et: