Web Controller in Odoo

Web Controller in Odoo

Odoo web controller having a facility that can create frontend modules integrate with the backend module.

 

Presently in Odoo, backend modules are accounting, CRM, discuss, helpdesk, hr, sales management, purchase management, inventory, warehouse etc. These modules can be accessed by a user if a user is having permission to access these modules.

 

Frontend modules are those which refer Odoo website functionalities likewise a website, website sale, website blog etc. With the help of these modules, we also can create website pages with both static and dynamic contents.

 

Now we will see how we can also 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 qweb template.

 

So for this firstly we will create odoo module. The structure for module is given below.

 

1]Module-

a]controller – __init__.py ,main.py

b]view – template.xml

c]data – data.xml

d]__openerp__.py

e]__init__.py

 

 

So from a structure, we have to create Odoo module, after that add the below content 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 OpenERP import SUPERUSER_ID

 openerp.addons.web import HTTP

from OpenERP.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 with one required argument. It is meant to handle the request comes to the URL which we have passed in argument.

 

In the body of a method, you can also add your business logic. Here I have added 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 the controller method on the page. Similarly, 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.

 

Finally, By using template directives attribute you can render data the on a website page. This way you can create your own custom web controllers.

 


Author Pic
Author Name: Diwakar Prabhu

If you would like to write to Diwakar Prabhu mail at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *