Merge lp:~openerp-dev/openerp-web/7.0-controller-extension-xmo into lp:openerp-web/7.0
- 7.0-controller-extension-xmo
- Merge into 7.0
Proposed by
Antony Lesuisse (OpenERP)
Status: | Merged |
---|---|
Merged at revision: | 3904 |
Proposed branch: | lp:~openerp-dev/openerp-web/7.0-controller-extension-xmo |
Merge into: | lp:openerp-web/7.0 |
Diff against target: |
584 lines (+446/-23) 5 files modified
addons/web/controllers/main.py (+15/-9) addons/web/http.py (+51/-9) addons/web/tests/__init__.py (+2/-1) addons/web/tests/test_dispatch.py (+373/-0) addons/web_diagram/controllers/main.py (+5/-4) |
To merge this branch: | bzr merge lp:~openerp-dev/openerp-web/7.0-controller-extension-xmo |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
OpenERP Core Team | Pending | ||
Review via email: mp+157151@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 3880. By Xavier (Open ERP)
-
[FIX] Extension of controller in-place with explicit spec of same _cp_path
When extending a controller in-place (e.g. A(Controller), B(A)) and
providing the exact same _cp_path as parent (no-op) execution path
would go into handler for _cp_path overwriting and raise an assertion
error for overwriting of existing controller.Except this is allowed (if ugly) pattern, so warn & ignore behavior
(it is harmless). - 3881. By Xavier (Open ERP)
-
[IMP] only use name of class being created: module is incorrect for dynamically created classes
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'addons/web/controllers/main.py' | |||
2 | --- addons/web/controllers/main.py 2013-04-05 13:47:47 +0000 | |||
3 | +++ addons/web/controllers/main.py 2013-04-18 09:47:37 +0000 | |||
4 | @@ -1,11 +1,9 @@ | |||
5 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
6 | 2 | |||
7 | 3 | import ast | 2 | import ast |
8 | 4 | import base64 | 3 | import base64 |
9 | 5 | import csv | 4 | import csv |
10 | 6 | import glob | 5 | import glob |
11 | 7 | import itertools | 6 | import itertools |
12 | 8 | import logging | ||
13 | 9 | import operator | 7 | import operator |
14 | 10 | import datetime | 8 | import datetime |
15 | 11 | import hashlib | 9 | import hashlib |
16 | @@ -30,7 +28,6 @@ | |||
17 | 30 | xlwt = None | 28 | xlwt = None |
18 | 31 | 29 | ||
19 | 32 | import openerp | 30 | import openerp |
20 | 33 | import openerp.modules.registry | ||
21 | 34 | from openerp.tools.translate import _ | 31 | from openerp.tools.translate import _ |
22 | 35 | 32 | ||
23 | 36 | from .. import http | 33 | from .. import http |
24 | @@ -1439,7 +1436,8 @@ | |||
25 | 1439 | else: | 1436 | else: |
26 | 1440 | return False | 1437 | return False |
27 | 1441 | 1438 | ||
29 | 1442 | class Export(View): | 1439 | |
30 | 1440 | class Export(openerpweb.Controller): | ||
31 | 1443 | _cp_path = "/web/export" | 1441 | _cp_path = "/web/export" |
32 | 1444 | 1442 | ||
33 | 1445 | @openerpweb.jsonrequest | 1443 | @openerpweb.jsonrequest |
34 | @@ -1580,7 +1578,13 @@ | |||
35 | 1580 | (prefix + '/' + k, prefix_string + '/' + v) | 1578 | (prefix + '/' + k, prefix_string + '/' + v) |
36 | 1581 | for k, v in self.fields_info(req, model, export_fields).iteritems()) | 1579 | for k, v in self.fields_info(req, model, export_fields).iteritems()) |
37 | 1582 | 1580 | ||
39 | 1583 | #noinspection PyPropertyDefinition | 1581 | |
40 | 1582 | class ExportFormat(object): | ||
41 | 1583 | """ | ||
42 | 1584 | Superclass for export formats, should probably be an abc and have a way to | ||
43 | 1585 | generate _cp_path from fmt but it's a pain to deal with conflicts with | ||
44 | 1586 | ControllerType | ||
45 | 1587 | """ | ||
46 | 1584 | @property | 1588 | @property |
47 | 1585 | def content_type(self): | 1589 | def content_type(self): |
48 | 1586 | """ Provides the format's content type """ | 1590 | """ Provides the format's content type """ |
49 | @@ -1621,14 +1625,14 @@ | |||
50 | 1621 | else: | 1625 | else: |
51 | 1622 | columns_headers = [val['label'].strip() for val in fields] | 1626 | columns_headers = [val['label'].strip() for val in fields] |
52 | 1623 | 1627 | ||
53 | 1624 | |||
54 | 1625 | return req.make_response(self.from_data(columns_headers, import_data), | 1628 | return req.make_response(self.from_data(columns_headers, import_data), |
55 | 1626 | headers=[('Content-Disposition', | 1629 | headers=[('Content-Disposition', |
56 | 1627 | content_disposition(self.filename(model), req)), | 1630 | content_disposition(self.filename(model), req)), |
57 | 1628 | ('Content-Type', self.content_type)], | 1631 | ('Content-Type', self.content_type)], |
58 | 1629 | cookies={'fileToken': int(token)}) | 1632 | cookies={'fileToken': int(token)}) |
59 | 1630 | 1633 | ||
61 | 1631 | class CSVExport(Export): | 1634 | |
62 | 1635 | class CSVExport(ExportFormat, http.Controller): | ||
63 | 1632 | _cp_path = '/web/export/csv' | 1636 | _cp_path = '/web/export/csv' |
64 | 1633 | fmt = {'tag': 'csv', 'label': 'CSV'} | 1637 | fmt = {'tag': 'csv', 'label': 'CSV'} |
65 | 1634 | 1638 | ||
66 | @@ -1663,7 +1667,8 @@ | |||
67 | 1663 | fp.close() | 1667 | fp.close() |
68 | 1664 | return data | 1668 | return data |
69 | 1665 | 1669 | ||
71 | 1666 | class ExcelExport(Export): | 1670 | |
72 | 1671 | class ExcelExport(ExportFormat, http.Controller): | ||
73 | 1667 | _cp_path = '/web/export/xls' | 1672 | _cp_path = '/web/export/xls' |
74 | 1668 | fmt = { | 1673 | fmt = { |
75 | 1669 | 'tag': 'xls', | 1674 | 'tag': 'xls', |
76 | @@ -1702,7 +1707,8 @@ | |||
77 | 1702 | fp.close() | 1707 | fp.close() |
78 | 1703 | return data | 1708 | return data |
79 | 1704 | 1709 | ||
81 | 1705 | class Reports(View): | 1710 | |
82 | 1711 | class Reports(openerpweb.Controller): | ||
83 | 1706 | _cp_path = "/web/report" | 1712 | _cp_path = "/web/report" |
84 | 1707 | POLLING_DELAY = 0.25 | 1713 | POLLING_DELAY = 0.25 |
85 | 1708 | TYPES_MAPPING = { | 1714 | TYPES_MAPPING = { |
86 | 1709 | 1715 | ||
87 | === modified file 'addons/web/http.py' | |||
88 | --- addons/web/http.py 2013-03-01 17:16:16 +0000 | |||
89 | +++ addons/web/http.py 2013-04-18 09:47:37 +0000 | |||
90 | @@ -354,18 +354,64 @@ | |||
91 | 354 | #---------------------------------------------------------- | 354 | #---------------------------------------------------------- |
92 | 355 | addons_module = {} | 355 | addons_module = {} |
93 | 356 | addons_manifest = {} | 356 | addons_manifest = {} |
96 | 357 | controllers_class = [] | 357 | controllers_class = {} |
95 | 358 | controllers_object = {} | ||
97 | 359 | controllers_path = {} | 358 | controllers_path = {} |
98 | 360 | 359 | ||
99 | 361 | class ControllerType(type): | 360 | class ControllerType(type): |
100 | 362 | def __init__(cls, name, bases, attrs): | 361 | def __init__(cls, name, bases, attrs): |
101 | 363 | super(ControllerType, cls).__init__(name, bases, attrs) | 362 | super(ControllerType, cls).__init__(name, bases, attrs) |
103 | 364 | controllers_class.append(("%s.%s" % (cls.__module__, cls.__name__), cls)) | 363 | # Only for root "Controller" |
104 | 364 | if bases == (object,): | ||
105 | 365 | assert name == 'Controller' | ||
106 | 366 | return | ||
107 | 367 | |||
108 | 368 | path = attrs.get('_cp_path') | ||
109 | 369 | if Controller in bases: | ||
110 | 370 | assert path, "Controller subclass %s missing a _cp_path" % name | ||
111 | 371 | else: | ||
112 | 372 | parent_paths = set(base._cp_path for base in bases | ||
113 | 373 | if issubclass(base, Controller)) | ||
114 | 374 | assert len(parent_paths) == 1,\ | ||
115 | 375 | "%s inheriting from multiple controllers is not supported" % ( | ||
116 | 376 | name) | ||
117 | 377 | [parent_path] = parent_paths | ||
118 | 378 | [parent] = [ | ||
119 | 379 | controller for controller in controllers_class.itervalues() | ||
120 | 380 | if controller._cp_path == parent_path] | ||
121 | 381 | |||
122 | 382 | # inherit from a Controller subclass | ||
123 | 383 | if path: | ||
124 | 384 | # if extending in place with same URL, ignore URL | ||
125 | 385 | if parent_path == path: | ||
126 | 386 | _logger.warn( | ||
127 | 387 | "Controller %s extending %s in-place should not " | ||
128 | 388 | "explicitly specify URL", name, parent) | ||
129 | 389 | return | ||
130 | 390 | _logger.warn("Re-exposing %s at %s.\n" | ||
131 | 391 | "\tThis usage is unsupported.", | ||
132 | 392 | parent.__name__, | ||
133 | 393 | attrs['_cp_path']) | ||
134 | 394 | |||
135 | 395 | if path: | ||
136 | 396 | assert path not in controllers_class,\ | ||
137 | 397 | "Trying to expose %s at the same URL as %s" % ( | ||
138 | 398 | name, controllers_class[path]) | ||
139 | 399 | controllers_class[path] = cls | ||
140 | 400 | |||
141 | 365 | 401 | ||
142 | 366 | class Controller(object): | 402 | class Controller(object): |
143 | 367 | __metaclass__ = ControllerType | 403 | __metaclass__ = ControllerType |
144 | 368 | 404 | ||
145 | 405 | def __new__(cls, *args, **kwargs): | ||
146 | 406 | subclasses = [c for c in cls.__subclasses__() | ||
147 | 407 | if c._cp_path == cls._cp_path] | ||
148 | 408 | if subclasses: | ||
149 | 409 | name = "%s (+%s)" % ( | ||
150 | 410 | cls.__name__, | ||
151 | 411 | '+'.join(sub.__name__ for sub in subclasses)) | ||
152 | 412 | cls = type(name, tuple(reversed(subclasses)), {}) | ||
153 | 413 | return object.__new__(cls) | ||
154 | 414 | |||
155 | 369 | #---------------------------------------------------------- | 415 | #---------------------------------------------------------- |
156 | 370 | # Session context manager | 416 | # Session context manager |
157 | 371 | #---------------------------------------------------------- | 417 | #---------------------------------------------------------- |
158 | @@ -558,12 +604,8 @@ | |||
159 | 558 | addons_manifest[module] = manifest | 604 | addons_manifest[module] = manifest |
160 | 559 | self.statics['/%s/static' % module] = path_static | 605 | self.statics['/%s/static' % module] = path_static |
161 | 560 | 606 | ||
168 | 561 | for k, v in controllers_class: | 607 | for c in controllers_class.itervalues(): |
169 | 562 | if k not in controllers_object: | 608 | controllers_path[c._cp_path] = c() |
164 | 563 | o = v() | ||
165 | 564 | controllers_object[k] = o | ||
166 | 565 | if hasattr(o, '_cp_path'): | ||
167 | 566 | controllers_path[o._cp_path] = o | ||
170 | 567 | 609 | ||
171 | 568 | app = werkzeug.wsgi.SharedDataMiddleware(self.dispatch, self.statics) | 610 | app = werkzeug.wsgi.SharedDataMiddleware(self.dispatch, self.statics) |
172 | 569 | self.dispatch = DisableCacheMiddleware(app) | 611 | self.dispatch = DisableCacheMiddleware(app) |
173 | 570 | 612 | ||
174 | === modified file 'addons/web/tests/__init__.py' | |||
175 | --- addons/web/tests/__init__.py 2012-11-26 14:05:25 +0000 | |||
176 | +++ addons/web/tests/__init__.py 2013-04-18 09:47:37 +0000 | |||
177 | @@ -1,9 +1,10 @@ | |||
178 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
180 | 2 | from . import test_dataset, test_menu, test_serving_base, test_js | 2 | from . import test_dataset, test_menu, test_serving_base, test_js, test_dispatch |
181 | 3 | 3 | ||
182 | 4 | fast_suite = [] | 4 | fast_suite = [] |
183 | 5 | checks = [ | 5 | checks = [ |
184 | 6 | test_dataset, | 6 | test_dataset, |
185 | 7 | test_menu, | 7 | test_menu, |
186 | 8 | test_serving_base, | 8 | test_serving_base, |
187 | 9 | test_dispatch, | ||
188 | 9 | ] | 10 | ] |
189 | 10 | 11 | ||
190 | === added file 'addons/web/tests/test_dispatch.py' | |||
191 | --- addons/web/tests/test_dispatch.py 1970-01-01 00:00:00 +0000 | |||
192 | +++ addons/web/tests/test_dispatch.py 2013-04-18 09:47:37 +0000 | |||
193 | @@ -0,0 +1,373 @@ | |||
194 | 1 | # -*- coding: utf-8 -*- | ||
195 | 2 | import contextlib | ||
196 | 3 | import json | ||
197 | 4 | import logging | ||
198 | 5 | import logging.handlers | ||
199 | 6 | import types | ||
200 | 7 | import unittest2 | ||
201 | 8 | |||
202 | 9 | from .. import http | ||
203 | 10 | import werkzeug.test | ||
204 | 11 | |||
205 | 12 | |||
206 | 13 | def setUpModule(): | ||
207 | 14 | """ | ||
208 | 15 | Force load_addons once to import all the crap we don't care for as this | ||
209 | 16 | thing is full of side-effects | ||
210 | 17 | """ | ||
211 | 18 | http.Root().load_addons() | ||
212 | 19 | |||
213 | 20 | class DispatchCleanup(unittest2.TestCase): | ||
214 | 21 | """ | ||
215 | 22 | Cleans up controllers registries in the web client so it's possible to | ||
216 | 23 | test controllers registration and dispatching in isolation. | ||
217 | 24 | """ | ||
218 | 25 | def setUp(self): | ||
219 | 26 | self.classes = http.controllers_class | ||
220 | 27 | self.paths = http.controllers_path | ||
221 | 28 | |||
222 | 29 | http.controllers_class = {} | ||
223 | 30 | http.controllers_path = {} | ||
224 | 31 | |||
225 | 32 | def tearDown(self): | ||
226 | 33 | http.controllers_path = self.paths | ||
227 | 34 | http.controllers_class = self.classes | ||
228 | 35 | |||
229 | 36 | |||
230 | 37 | def jsonrpc_body(params=None): | ||
231 | 38 | """ | ||
232 | 39 | Builds and dumps the body of a JSONRPC request with params ``params`` | ||
233 | 40 | """ | ||
234 | 41 | return json.dumps({ | ||
235 | 42 | 'jsonrpc': '2.0', | ||
236 | 43 | 'method': 'call', | ||
237 | 44 | 'id': None, | ||
238 | 45 | 'params': params or {}, | ||
239 | 46 | }) | ||
240 | 47 | |||
241 | 48 | |||
242 | 49 | def jsonrpc_response(result=None): | ||
243 | 50 | """ | ||
244 | 51 | Builds a JSONRPC response (as a Python dict) with result ``result`` | ||
245 | 52 | """ | ||
246 | 53 | return { | ||
247 | 54 | u'jsonrpc': u'2.0', | ||
248 | 55 | u'id': None, | ||
249 | 56 | u'result': result, | ||
250 | 57 | } | ||
251 | 58 | |||
252 | 59 | |||
253 | 60 | class TestHandler(logging.handlers.BufferingHandler): | ||
254 | 61 | def __init__(self): | ||
255 | 62 | logging.handlers.BufferingHandler.__init__(self, 0) | ||
256 | 63 | |||
257 | 64 | def shouldFlush(self, record): | ||
258 | 65 | return False | ||
259 | 66 | |||
260 | 67 | @contextlib.contextmanager | ||
261 | 68 | def capture_logging(logger, level=logging.DEBUG): | ||
262 | 69 | logger = logging.getLogger(logger) | ||
263 | 70 | old_level = logger.level | ||
264 | 71 | old_handlers = logger.handlers | ||
265 | 72 | old_propagate = logger.propagate | ||
266 | 73 | |||
267 | 74 | test_handler = TestHandler() | ||
268 | 75 | logger.handlers = [test_handler] | ||
269 | 76 | logger.setLevel(level) | ||
270 | 77 | logger.propagate = False | ||
271 | 78 | |||
272 | 79 | try: | ||
273 | 80 | yield test_handler | ||
274 | 81 | finally: | ||
275 | 82 | logger.propagate = old_propagate | ||
276 | 83 | logger.setLevel(old_level) | ||
277 | 84 | logger.handlers = old_handlers | ||
278 | 85 | |||
279 | 86 | |||
280 | 87 | class TestDispatching(DispatchCleanup): | ||
281 | 88 | def setUp(self): | ||
282 | 89 | super(TestDispatching, self).setUp() | ||
283 | 90 | self.app = http.Root() | ||
284 | 91 | self.client = werkzeug.test.Client(self.app) | ||
285 | 92 | |||
286 | 93 | def test_not_exposed(self): | ||
287 | 94 | class CatController(http.Controller): | ||
288 | 95 | _cp_path = '/cat' | ||
289 | 96 | |||
290 | 97 | def index(self): | ||
291 | 98 | return 'Blessid iz da feline' | ||
292 | 99 | |||
293 | 100 | self.app.load_addons() | ||
294 | 101 | |||
295 | 102 | body, status, headers = self.client.get('/cat') | ||
296 | 103 | self.assertEqual('404 NOT FOUND', status) | ||
297 | 104 | |||
298 | 105 | def test_basic_http(self): | ||
299 | 106 | class CatController(http.Controller): | ||
300 | 107 | _cp_path = '/cat' | ||
301 | 108 | |||
302 | 109 | @http.httprequest | ||
303 | 110 | def index(self, req): | ||
304 | 111 | return 'no walk in counsil of wickid,' | ||
305 | 112 | |||
306 | 113 | self.app.load_addons() | ||
307 | 114 | |||
308 | 115 | body, status, headers = self.client.get('/cat') | ||
309 | 116 | self.assertEqual('200 OK', status) | ||
310 | 117 | self.assertEqual('no walk in counsil of wickid,', ''.join(body)) | ||
311 | 118 | |||
312 | 119 | def test_basic_jsonrpc(self): | ||
313 | 120 | class CatController(http.Controller): | ||
314 | 121 | _cp_path = '/cat' | ||
315 | 122 | |||
316 | 123 | @http.jsonrequest | ||
317 | 124 | def index(self, req): | ||
318 | 125 | return 'no place paws in path of da sinnerz,' | ||
319 | 126 | self.app.load_addons() | ||
320 | 127 | |||
321 | 128 | body, status, headers = self.client.post('/cat', data=jsonrpc_body()) | ||
322 | 129 | |||
323 | 130 | self.assertEqual('200 OK', status) | ||
324 | 131 | self.assertEqual( | ||
325 | 132 | jsonrpc_response('no place paws in path of da sinnerz,'), | ||
326 | 133 | json.loads(''.join(body))) | ||
327 | 134 | |||
328 | 135 | |||
329 | 136 | class TestSubclassing(DispatchCleanup): | ||
330 | 137 | def setUp(self): | ||
331 | 138 | super(TestSubclassing, self).setUp() | ||
332 | 139 | self.app = http.Root() | ||
333 | 140 | self.client = werkzeug.test.Client(self.app) | ||
334 | 141 | |||
335 | 142 | def test_add_method(self): | ||
336 | 143 | class CatController(http.Controller): | ||
337 | 144 | _cp_path = '/cat' | ||
338 | 145 | |||
339 | 146 | @http.httprequest | ||
340 | 147 | def index(self, req): | ||
341 | 148 | return 'no sit and purr with da mockerz.' | ||
342 | 149 | |||
343 | 150 | class CeilingController(CatController): | ||
344 | 151 | @http.httprequest | ||
345 | 152 | def lol(self, req): | ||
346 | 153 | return 'But der delightz in lawz of Ceiling Cat,' | ||
347 | 154 | |||
348 | 155 | self.app.load_addons() | ||
349 | 156 | |||
350 | 157 | body, status, headers = self.client.get('/cat') | ||
351 | 158 | self.assertEqual('200 OK', status) | ||
352 | 159 | self.assertEqual('no sit and purr with da mockerz.', ''.join(body)) | ||
353 | 160 | body, status, headers = self.client.get('/cat/lol') | ||
354 | 161 | self.assertEqual('200 OK', status) | ||
355 | 162 | self.assertEqual('But der delightz in lawz of Ceiling Cat,', | ||
356 | 163 | ''.join(body)) | ||
357 | 164 | |||
358 | 165 | def test_override_method(self): | ||
359 | 166 | class CatController(http.Controller): | ||
360 | 167 | _cp_path = '/cat' | ||
361 | 168 | |||
362 | 169 | @http.httprequest | ||
363 | 170 | def index(self, req): | ||
364 | 171 | return 'an ponderz' | ||
365 | 172 | |||
366 | 173 | class CeilingController(CatController): | ||
367 | 174 | @http.httprequest | ||
368 | 175 | def index(self, req): | ||
369 | 176 | return '%s much.' % super(CeilingController, self).index(req) | ||
370 | 177 | |||
371 | 178 | self.app.load_addons() | ||
372 | 179 | |||
373 | 180 | body, status, headers = self.client.get('/cat') | ||
374 | 181 | self.assertEqual('200 OK', status) | ||
375 | 182 | self.assertEqual('an ponderz much.', ''.join(body)) | ||
376 | 183 | |||
377 | 184 | def test_make_invisible(self): | ||
378 | 185 | class CatController(http.Controller): | ||
379 | 186 | _cp_path = '/cat' | ||
380 | 187 | |||
381 | 188 | @http.httprequest | ||
382 | 189 | def index(self, req): | ||
383 | 190 | return 'Tehy liek treez bai teh waterz,' | ||
384 | 191 | |||
385 | 192 | class CeilingController(CatController): | ||
386 | 193 | def index(self, req): | ||
387 | 194 | return super(CeilingController, self).index(req) | ||
388 | 195 | |||
389 | 196 | self.app.load_addons() | ||
390 | 197 | |||
391 | 198 | body, status, headers = self.client.get('/cat') | ||
392 | 199 | self.assertEqual('404 NOT FOUND', status) | ||
393 | 200 | |||
394 | 201 | def test_make_json_invisible(self): | ||
395 | 202 | class CatController(http.Controller): | ||
396 | 203 | _cp_path = '/cat' | ||
397 | 204 | |||
398 | 205 | @http.jsonrequest | ||
399 | 206 | def index(self, req): | ||
400 | 207 | return 'Tehy liek treez bai teh waterz,' | ||
401 | 208 | |||
402 | 209 | class CeilingController(CatController): | ||
403 | 210 | def index(self, req): | ||
404 | 211 | return super(CeilingController, self).index(req) | ||
405 | 212 | |||
406 | 213 | self.app.load_addons() | ||
407 | 214 | |||
408 | 215 | body, status, headers = self.client.post('/cat') | ||
409 | 216 | self.assertEqual('404 NOT FOUND', status) | ||
410 | 217 | |||
411 | 218 | def test_extends(self): | ||
412 | 219 | """ | ||
413 | 220 | When subclassing an existing Controller new classes are "merged" into | ||
414 | 221 | the base one | ||
415 | 222 | """ | ||
416 | 223 | class A(http.Controller): | ||
417 | 224 | _cp_path = '/foo' | ||
418 | 225 | @http.httprequest | ||
419 | 226 | def index(self, req): | ||
420 | 227 | return '1' | ||
421 | 228 | |||
422 | 229 | class B(A): | ||
423 | 230 | @http.httprequest | ||
424 | 231 | def index(self, req): | ||
425 | 232 | return "%s 2" % super(B, self).index(req) | ||
426 | 233 | |||
427 | 234 | class C(A): | ||
428 | 235 | @http.httprequest | ||
429 | 236 | def index(self, req): | ||
430 | 237 | return "%s 3" % super(C, self).index(req) | ||
431 | 238 | |||
432 | 239 | self.app.load_addons() | ||
433 | 240 | |||
434 | 241 | body, status, headers = self.client.get('/foo') | ||
435 | 242 | self.assertEqual('200 OK', status) | ||
436 | 243 | self.assertEqual('1 2 3', ''.join(body)) | ||
437 | 244 | |||
438 | 245 | def test_extends_same_path(self): | ||
439 | 246 | """ | ||
440 | 247 | When subclassing an existing Controller and specifying the same | ||
441 | 248 | _cp_path as the parent, ??? | ||
442 | 249 | """ | ||
443 | 250 | class A(http.Controller): | ||
444 | 251 | _cp_path = '/foo' | ||
445 | 252 | @http.httprequest | ||
446 | 253 | def index(self, req): | ||
447 | 254 | return '1' | ||
448 | 255 | |||
449 | 256 | class B(A): | ||
450 | 257 | _cp_path = '/foo' | ||
451 | 258 | @http.httprequest | ||
452 | 259 | def index(self, req): | ||
453 | 260 | return '2' | ||
454 | 261 | |||
455 | 262 | self.app.load_addons() | ||
456 | 263 | |||
457 | 264 | body, status, headers = self.client.get('/foo') | ||
458 | 265 | self.assertEqual('200 OK', status) | ||
459 | 266 | self.assertEqual('2', ''.join(body)) | ||
460 | 267 | |||
461 | 268 | def test_re_expose(self): | ||
462 | 269 | """ | ||
463 | 270 | An existing Controller should not be extended with a new cp_path | ||
464 | 271 | (re-exposing somewhere else) | ||
465 | 272 | """ | ||
466 | 273 | class CatController(http.Controller): | ||
467 | 274 | _cp_path = '/cat' | ||
468 | 275 | |||
469 | 276 | @http.httprequest | ||
470 | 277 | def index(self, req): | ||
471 | 278 | return '[%s]' % self.speak() | ||
472 | 279 | |||
473 | 280 | def speak(self): | ||
474 | 281 | return 'Yu ordered cheezburgerz,' | ||
475 | 282 | |||
476 | 283 | with capture_logging('openerp.addons.web.http') as handler: | ||
477 | 284 | class DogController(CatController): | ||
478 | 285 | _cp_path = '/dog' | ||
479 | 286 | |||
480 | 287 | def speak(self): | ||
481 | 288 | return 'Woof woof woof woof' | ||
482 | 289 | |||
483 | 290 | [record] = handler.buffer | ||
484 | 291 | self.assertEqual(logging.WARN, record.levelno) | ||
485 | 292 | self.assertEqual("Re-exposing CatController at /dog.\n" | ||
486 | 293 | "\tThis usage is unsupported.", | ||
487 | 294 | record.getMessage()) | ||
488 | 295 | |||
489 | 296 | def test_fail_redefine(self): | ||
490 | 297 | """ | ||
491 | 298 | An existing Controller can't be overwritten by a new one on the same | ||
492 | 299 | path (? or should this generate a warning and still work as if it was | ||
493 | 300 | an extend?) | ||
494 | 301 | """ | ||
495 | 302 | class FooController(http.Controller): | ||
496 | 303 | _cp_path = '/foo' | ||
497 | 304 | |||
498 | 305 | with self.assertRaises(AssertionError): | ||
499 | 306 | class BarController(http.Controller): | ||
500 | 307 | _cp_path = '/foo' | ||
501 | 308 | |||
502 | 309 | def test_fail_no_path(self): | ||
503 | 310 | """ | ||
504 | 311 | A Controller must have a path (and thus be exposed) | ||
505 | 312 | """ | ||
506 | 313 | with self.assertRaises(AssertionError): | ||
507 | 314 | class FooController(http.Controller): | ||
508 | 315 | pass | ||
509 | 316 | |||
510 | 317 | def test_mixin(self): | ||
511 | 318 | """ | ||
512 | 319 | Can mix "normal" python classes into a controller directly | ||
513 | 320 | """ | ||
514 | 321 | class Mixin(object): | ||
515 | 322 | @http.httprequest | ||
516 | 323 | def index(self, req): | ||
517 | 324 | return 'ok' | ||
518 | 325 | |||
519 | 326 | class FooController(http.Controller, Mixin): | ||
520 | 327 | _cp_path = '/foo' | ||
521 | 328 | |||
522 | 329 | class BarContoller(Mixin, http.Controller): | ||
523 | 330 | _cp_path = '/bar' | ||
524 | 331 | |||
525 | 332 | self.app.load_addons() | ||
526 | 333 | |||
527 | 334 | body, status, headers = self.client.get('/foo') | ||
528 | 335 | self.assertEqual('200 OK', status) | ||
529 | 336 | self.assertEqual('ok', ''.join(body)) | ||
530 | 337 | |||
531 | 338 | body, status, headers = self.client.get('/bar') | ||
532 | 339 | self.assertEqual('200 OK', status) | ||
533 | 340 | self.assertEqual('ok', ''.join(body)) | ||
534 | 341 | |||
535 | 342 | def test_mixin_extend(self): | ||
536 | 343 | """ | ||
537 | 344 | Can mix "normal" python class into a controller by extension | ||
538 | 345 | """ | ||
539 | 346 | class FooController(http.Controller): | ||
540 | 347 | _cp_path = '/foo' | ||
541 | 348 | |||
542 | 349 | class M1(object): | ||
543 | 350 | @http.httprequest | ||
544 | 351 | def m1(self, req): | ||
545 | 352 | return 'ok 1' | ||
546 | 353 | |||
547 | 354 | class M2(object): | ||
548 | 355 | @http.httprequest | ||
549 | 356 | def m2(self, req): | ||
550 | 357 | return 'ok 2' | ||
551 | 358 | |||
552 | 359 | class AddM1(FooController, M1): | ||
553 | 360 | pass | ||
554 | 361 | |||
555 | 362 | class AddM2(M2, FooController): | ||
556 | 363 | pass | ||
557 | 364 | |||
558 | 365 | self.app.load_addons() | ||
559 | 366 | |||
560 | 367 | body, status, headers = self.client.get('/foo/m1') | ||
561 | 368 | self.assertEqual('200 OK', status) | ||
562 | 369 | self.assertEqual('ok 1', ''.join(body)) | ||
563 | 370 | |||
564 | 371 | body, status, headers = self.client.get('/foo/m2') | ||
565 | 372 | self.assertEqual('200 OK', status) | ||
566 | 373 | self.assertEqual('ok 2', ''.join(body)) | ||
567 | 0 | 374 | ||
568 | === modified file 'addons/web_diagram/controllers/main.py' | |||
569 | --- addons/web_diagram/controllers/main.py 2012-12-06 09:51:23 +0000 | |||
570 | +++ addons/web_diagram/controllers/main.py 2013-04-18 09:47:37 +0000 | |||
571 | @@ -1,9 +1,10 @@ | |||
575 | 1 | import openerp | 1 | from openerp.addons.web.http import Controller, jsonrequest |
576 | 2 | 2 | ||
577 | 3 | class DiagramView(openerp.addons.web.controllers.main.View): | 3 | |
578 | 4 | class Diagram(Controller): | ||
579 | 4 | _cp_path = "/web_diagram/diagram" | 5 | _cp_path = "/web_diagram/diagram" |
580 | 5 | 6 | ||
582 | 6 | @openerp.addons.web.http.jsonrequest | 7 | @jsonrequest |
583 | 7 | def get_diagram_info(self, req, id, model, node, connector, | 8 | def get_diagram_info(self, req, id, model, node, connector, |
584 | 8 | src_node, des_node, label, **kw): | 9 | src_node, des_node, label, **kw): |
585 | 9 | 10 |