[IMP] http improvement
[odoo/odoo.git] / openerp / addons / base / ir / ir_http.py
1 #----------------------------------------------------------
2 # ir_http modular http routing
3 #----------------------------------------------------------
4 import logging
5 import re
6 import sys
7
8 import werkzeug.exceptions
9 import werkzeug.routing
10
11 import openerp
12 from openerp import http
13 from openerp.http import request
14 from openerp.osv import osv, orm
15
16 _logger = logging.getLogger(__name__)
17
18
19 # FIXME: replace by proxy on request.uid?
20 _uid = object()
21
22 class ModelConverter(werkzeug.routing.BaseConverter):
23
24     def __init__(self, url_map, model=False):
25         super(ModelConverter, self).__init__(url_map)
26         self.model = model
27         self.regex = '([0-9]+)'
28
29     def to_python(self, value):
30         m = re.match(self.regex, value)
31         return request.registry[self.model].browse(
32             request.cr, _uid, int(m.group(1)), context=request.context)
33
34     def to_url(self, value):
35         return value.id
36
37 class ModelsConverter(werkzeug.routing.BaseConverter):
38
39     def __init__(self, url_map, model=False):
40         super(ModelsConverter, self).__init__(url_map)
41         self.model = model
42         # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
43         self.regex = '([0-9,]+)'
44
45     def to_python(self, value):
46         # TODO:
47         # - raise routing.ValidationError() if no browse record can be createdm
48         # - support slug
49         return request.registry[self.model].browse(request.cr, _uid, [int(i) for i in value.split(',')], context=request.context)
50
51     def to_url(self, value):
52         return ",".join(i.id for i in value)
53
54 class ir_http(osv.AbstractModel):
55     _name = 'ir.http'
56     _description = "HTTP routing"
57
58     def _get_converters(self):
59         return {'model': ModelConverter, 'models': ModelsConverter}
60
61     def _find_handler(self):
62         return self.routing_map().bind_to_environ(request.httprequest.environ).match()
63
64     def _auth_method_user(self):
65         request.uid = request.session.uid
66         if not request.uid:
67             raise http.SessionExpiredException("Session expired")
68
69     def _auth_method_admin(self):
70         if not request.db:
71             raise http.SessionExpiredException("No valid database for request %s" % request.httprequest)
72         request.uid = openerp.SUPERUSER_ID
73
74     def _auth_method_none(self):
75         request.disable_db = True
76         request.uid = None
77
78     def _authenticate(self, auth_method='user'):
79         if request.session.uid:
80             try:
81                 request.session.check_security()
82                 # what if error in security.check()
83                 #   -> res_users.check()
84                 #   -> res_users.check_credentials()
85             except http.SessionExpiredException:
86                 request.session.logout()
87                 raise http.SessionExpiredException("Session expired for request %s" % request.httprequest)
88         getattr(self, "_auth_method_%s" % auth_method)()
89         return auth_method
90
91     def _handle_exception(self, exception):
92         if isinstance(exception, openerp.exceptions.AccessError):
93             code = 403
94         else:
95             code = getattr(exception, 'code', 500)
96
97         fn = getattr(self, '_handle_%d' % code, self._handle_unknown_exception)
98         return fn(exception)
99
100     def _handle_unknown_exception(self, exception):
101         raise exception
102
103     def _dispatch(self):
104         # locate the controller method
105         try:
106             func, arguments = self._find_handler()
107         except werkzeug.exceptions.NotFound, e:
108             return self._handle_exception(e)
109
110         # check authentication level
111         try:
112             auth_method = self._authenticate(getattr(func, "auth", None))
113         except Exception:
114             # force a Forbidden exception with the original traceback
115             return self._handle_exception(
116                 convert_exception_to(
117                     werkzeug.exceptions.Forbidden))
118
119         # post process arg to set uid on browse records
120         for arg in arguments.itervalues():
121             if isinstance(arg, orm.browse_record) and arg._uid is _uid:
122                 arg._uid = request.uid
123
124         # set and execute handler
125         try:
126             request.set_handler(func, arguments, auth_method)
127             result = request.dispatch()
128             if isinstance(result, Exception):
129                 raise result
130         except Exception, e:
131             return self._handle_exception(e)
132
133         return result
134
135     def routing_map(self):
136         if not hasattr(self, '_routing_map'):
137             _logger.info("Generating routing map")
138             cr = request.cr
139             m = request.registry.get('ir.module.module')
140             ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')], context=request.context)
141             installed = set(x['name'] for x in m.read(cr, 1, ids, ['name'], context=request.context))
142             mods = ['', "web"] + sorted(installed)
143             self._routing_map = http.routing_map(mods, False, converters=self._get_converters())
144
145         return self._routing_map
146
147 def convert_exception_to(to_type, with_message=False):
148     """ Should only be called from an exception handler. Fetches the current
149     exception data from sys.exc_info() and creates a new exception of type
150     ``to_type`` with the original traceback.
151
152     If ``with_message`` is ``True``, sets the new exception's message to be
153     the stringification of the original exception. If ``False``, does not
154     set the new exception's message. Otherwise, uses ``with_message`` as the
155     new exception's message.
156
157     :type with_message: str|bool
158     """
159     etype, original, tb = sys.exc_info()
160     try:
161         if with_message is False:
162             message = None
163         elif with_message is True:
164             message = str(original)
165         else:
166             message = str(with_message)
167
168         raise to_type, message, tb
169     except to_type, e:
170         return e
171
172 # vim:et: