5 Web controllers are classes in OpenERP able to catch the http requests sent by any browser. They allow to generate
6 html pages to be served like any web server, implement new methods to be used by the Javascript client, etc...
11 By convention the controllers should be placed in the controllers directory of the module. Example:
18 │ └── my_controllers.py
22 In ``__init__.py`` you must add:
28 And here is the content of ``controllers/__init__.py``:
34 Now you can put the following content in ``controllers/my_controllers.py``:
38 import openerp.addons.web.http as http
39 from openerp.addons.web.http import request
42 Controller Declaration
43 ----------------------
45 In your controllers file, you can now declare a controller this way:
49 class MyController(http.Controller):
51 @http.route('/my_url/some_html', type="http")
53 return "<h1>This is a test</h1>"
55 @http.route('/my_url/some_json', type="json")
57 return {"sample_dictionary": "This is a sample JSON dictionary"}
59 A controller must inherit from ``http.Controller``. Each time you define a method with ``@http.route()`` it defines a
60 url to match. As example, the ``some_html()`` method will be called a client query the ``/my_url/some_html`` url.
65 You can define methods to get any normal http requests by passing ``'http'`` to the ``type`` argument of
66 ``http.route()``. When doing so, you get the HTTP parameters as named parameters of the method:
70 @http.route('/say_hello', type="http")
71 def say_hello(self, name):
72 return "<h1>Hello %s</h1>" % name
74 This url could be contacted by typing this url in a browser: ``http://localhost:8069/say_hello?name=Nicolas``.
79 Methods that received JSON can be defined by passing ``'json'`` to the ``type`` argument of ``http.route()``. The
80 OpenERP Javascript client can contact these methods using the JSON-RPC protocol. JSON methods must return JSON. Like the
81 HTTP methods they receive arguments as named parameters (except these arguments are JSON-RPC parameters).
85 @http.route('/division', type="json")
86 def division(self, i, j):
87 return i / j # returns a number
92 Any URL passed to ``http.route()`` can contain patterns. Example:
96 @http.route('/files/<path:file_path>', type="http")
97 def files(self, file_path):
98 ... # return a file identified by the path store in the 'my_path' variable
100 When such patterns are used, the method will received additional parameters that correspond to the parameters defined in
101 the url. For exact documentation about url patterns, see Werkzeug's documentation:
102 http://werkzeug.pocoo.org/docs/routing/ .
104 Also note you can pass multiple urls to ``http.route()``:
109 @http.route(['/files/<path:file_path>', '/other_url/<path:file_path>'], type="http")
110 def files(self, file_path):
116 To use the database you must access the OpenERP models. The global ``request`` object provides the necessary objects:
120 @http.route('/my_name', type="http")
122 my_user_record = request.registry.get("res.users").browse(request.cr, request.uid, request.uid)
123 return "<h1>Your name is %s</h1>" % my_user_record.name
125 ``request.registry`` is the registry that gives you access to the models. It is the equivalent of ``self.pool`` when
126 working inside OpenERP models.
128 ``request.cr`` is the cursor object. This is the ``cr`` parameter you have to pass as first argument of every model
131 ``request.uid`` is the id of the current logged in user. This is the ``uid`` parameter you have to pass as second
132 argument of every model method in OpenERP.
137 By default, all methods can only be used by users logged into OpenERP (OpenERP uses cookies to track logged users).
138 There are some cases when you need to enable not-logged in users to access some methods. To do so, add the ``'db'``
139 value to the ``auth`` parameter of ``http.route()``:
143 @http.route('/hello', type="http", auth="db")
145 return "<div>Hello unknown user!</div>"
147 Please note the ``request.uid`` user id will be ``None`` inside this method call. This is due to the fact no user was
150 Overriding Controllers
151 ----------------------
153 Existing routes can be overridden. To do so, create a controller that inherit the controller containing the route you
154 want to override. Example that redefine the home page of your OpenERP application.
158 import openerp.addons.web.controllers.main as main
160 class Home2(main.Home):
161 @http.route('/', type="http", auth="db")
163 return "<div>This is my new home page.</div>"
165 By re-defining the ``index()`` method, you change the behavior of the original ``Home`` class. Now the ``'/'`` route
166 will match the new ``index()`` method in ``Home2``.