Odoo is an integrated Open source ERP module with multiple components running from Customer Management, Human Resource Management, Data processing, Warehouse management and much more. Each module is enriched the different set of functionalities and performance. To manage the whole slot of integrations, Odoo Web Controller have been equipped with strong facility that it can create frontend modules (functionalities like a website, website sale, website blog etc. With the help of these modules, we can create website pages with both static and dynamic contents) to integrate with backend modules.
Now we will see how we can create frontend modules with custom web controller that will fetch the data related to company departments and display in the website custom page which we can build using the Odoo web template.
How to create an Odoo Module? Here is the structure for your better understanding.
Module- controller – __init__.py ,main.py view – template.xml data – data.xml __openerp__.py __init__.py After structuring, add the below contents to create a class in main.py file. Basically, the file includes the following content-
1. Classes
2. Required import statements
3. Methods to handle business logic
From Open ERP import SUPERUSER_ID From openerp.addons.web import HTTP From Open ERP. http import request Class Website Demo (http.Controller): @http.route(‘/department’, type=’http’, auth=’user’, website=True) def display_department_data(self): cr, context, pool = request.cr, request.context, request.registry hr_department = pool.get(‘hr.department ‘) hr_department_ids = hr_department .search(cr, SUPERUSER_ID, [], context=context) hr_department_data = hr_department .browse(cr, SUPERUSER_ID, hr_department_ids, context=context) values = { ‘department’ : hr_department_data }
return request.website.render(“website.department “, values)
The @http.route is a decorator used on the method where one required argument is meant to handle the request coming to the URL which we are passed in argument.
In the body of a method, you can add your business logic. Here I have added a code to fetch all of the department data. After that, it will render the page with given page template id and values.
You must create a website page in order to handle data sent by controller method on the page.On the website page, you have to add the following content to the template.XML file.
<?xml version=”1.0″ encoding=”utf-8″?> <openerp> <data> <template name=”Department Details” id=”website.department ” page=”True”> <t t-call=”website.layout”> <div id=”page”> <h3> Department Details</h3> <table class=”table table-hover”> <thead> <tr> <th>Name</th> <th>Employee</th> </tr> </thead> <tbody> <tr> <t t-foreach=”departments” t-as=”department”> <tr> <td><t t-esc=”department .name_related” /></td> <td><t t-esc=”department .working_employee” /></td> </tr> </t> </tr> </tbody> </table> </div> </t> </template> </data> </openerp>
Now website page uses Qweb template engine to render data send by the controller method.
By using template directives attributes you can render data the on a website page. By using this way you can create your own custom web controllers.