Web Controller in Odoo

Web Controller in Odoo

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

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 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.

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 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 creat a class in main.py file.

Basically, file includes following content-

1]Classes

2]Required import statements

3]Methods to handle business logic

from OpenERP import SUPERUSER_ID

from 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 are meant to handle the request comes to the URL which we have passed in argument.

In the body of a method, you can 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 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.