Skip to content

Commit

Permalink
[ADD] Chapter 14
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Nov 13, 2017
1 parent 1d9d2b0 commit b070d92
Show file tree
Hide file tree
Showing 28 changed files with 619 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Chapter14/r1_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
32 changes: 32 additions & 0 deletions Chapter14/r1_widgets/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name": "Odoo cookbook - Chapter 14, recipe 01",
"version": "11.0.1.0.0",
"author": "Odoo cookbook",
"license": "AGPL-3",
"category": "Odoo cookbook",
"summary": "Create custom widgets",
"depends": [
'web',
],
"data": [
"views/res_partner.xml",
"views/templates.xml",
],
}
4 changes: 4 additions & 0 deletions Chapter14/r1_widgets/static/src/css/r1_widgets.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.oe_form_field_many2one_buttons button
{
margin: 0em .2em .2em 0em;
}
58 changes: 58 additions & 0 deletions Chapter14/r1_widgets/static/src/js/r1_widgets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
odoo.define('r1_widgets', function(require)
{
var registry = require('web.field_registry'),
AbstractField = require('web.AbstractField');

var FieldMany2OneButtons = AbstractField.extend({
className: 'oe_form_field_many2one_buttons',
supportedFieldTypes: ['many2one'],
init: function()
{
this._super.apply(this, arguments);
this.user_list = {
1: {
name: 'Administrator',
},
4: {
name: 'Demo user',
},
};
},
events: {
'click .btn': '_button_clicked',
},
_render: function()
{
var self = this;
this.$el.empty();
_.each(this.user_list, function(description, id)
{
self.$el.append(
jQuery('<button>').attr({
'data-id': id,
'class': 'btn btn-default btn-sm',
})
.text(description.name)
.toggleClass(
'btn-primary',
self.value ? self.value.res_id == id : false
)
);
});
this.$el.find('button').
prop('disabled', this.mode == 'readonly');
},
_button_clicked: function(e)
{
this._setValue(
parseInt(jQuery(e.target).attr('data-id'))
);
},
});

registry.add('many2one_buttons', FieldMany2OneButtons);

return {
FieldMany2OneButtons: FieldMany2OneButtons,
}
});
12 changes: 12 additions & 0 deletions Chapter14/r1_widgets/views/res_partner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_partner_form" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="user_id" position="attributes">
<attribute name="widget">many2one_buttons</attribute>
</field>
</field>
</record>
</odoo>
9 changes: 9 additions & 0 deletions Chapter14/r1_widgets/views/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<link href="/r1_widgets/static/src/css/r1_widgets.css" rel="stylesheet" type="text/css"/>
<script src="/r1_widgets/static/src/js/r1_widgets.js" type="text/javascript" />
</xpath>
</template>
</odoo>
17 changes: 17 additions & 0 deletions Chapter14/r2_clientside_qweb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
35 changes: 35 additions & 0 deletions Chapter14/r2_clientside_qweb/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name": "Odoo cookbook - Chapter 14, recipe 02",
"version": "11.0.1.0.0",
"author": "Odoo cookbook",
"license": "AGPL-3",
"category": "Odoo cookbook",
"summary": "Using client side templates: QWeb",
"depends": [
'web',
],
"qweb": [
'static/src/xml/r2_clientside_qweb.xml',
],
"data": [
"views/res_partner.xml",
"views/templates.xml",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.oe_form_field_many2one_buttons button
{
margin: 0em .2em .2em 0em;
}
42 changes: 42 additions & 0 deletions Chapter14/r2_clientside_qweb/static/src/js/r2_clientside_qweb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
odoo.define('r2_clientside_qweb', function(require)
{
var registry = require('web.field_registry'),
AbstractField = require('web.AbstractField');

var FieldMany2OneButtons = AbstractField.extend({
template: 'FieldMany2OneButtons',
className: 'oe_form_field_many2one_buttons',
supportedFieldTypes: ['many2one'],
init: function()
{
this._super.apply(this, arguments);
this.user_list = {
1: {
name: 'Administrator',
},
4: {
name: 'Demo user',
},
};
},
events: {
'click .btn': '_button_clicked',
},
_render: function()
{
this.renderElement();
},
_button_clicked: function(e)
{
this._setValue(
parseInt(jQuery(e.target).attr('data-id'))
);
},
});

registry.add('many2one_buttons', FieldMany2OneButtons);

return {
FieldMany2OneButtons: FieldMany2OneButtons,
}
});
11 changes: 11 additions & 0 deletions Chapter14/r2_clientside_qweb/static/src/xml/r2_clientside_qweb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<templates>
<t t-name="FieldMany2OneButtons">
<div class="oe_form_field_many2one_buttons">
<t t-foreach="widget.user_list" t-as="user_id">
<button t-att-disabled="widget.mode == 'readonly' ? 'disabled' : False" t-att-data-id="user_id" t-attf-class="btn btn-default btn-sm {{(widget.value and widget.value.res_id == user_id) and 'btn-primary' or ''}}">
<t t-esc="widget.user_list[user_id].name" />
</button>
</t>
</div>
</t>
</templates>
12 changes: 12 additions & 0 deletions Chapter14/r2_clientside_qweb/views/res_partner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_partner_form" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="user_id" position="attributes">
<attribute name="widget">many2one_buttons</attribute>
</field>
</field>
</record>
</odoo>
9 changes: 9 additions & 0 deletions Chapter14/r2_clientside_qweb/views/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<link href="/r2_clientside_qweb/static/src/css/r2_clientside_qweb.css" rel="stylesheet" type="text/css"/>
<script src="/r2_clientside_qweb/static/src/js/r2_clientside_qweb.js" type="text/javascript" />
</xpath>
</template>
</odoo>
17 changes: 17 additions & 0 deletions Chapter14/r3_rpc_calls/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
35 changes: 35 additions & 0 deletions Chapter14/r3_rpc_calls/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name": "Odoo cookbook - Chapter 14, recipe 03",
"version": "11.0.1.0.0",
"author": "Odoo cookbook",
"license": "AGPL-3",
"category": "Odoo cookbook",
"summary": "Making RPC calls to the server",
"depends": [
'web',
],
"qweb": [
'static/src/xml/r3_rpc_calls.xml',
],
"data": [
"views/res_partner.xml",
"views/templates.xml",
],
}
4 changes: 4 additions & 0 deletions Chapter14/r3_rpc_calls/static/src/css/r3_rpc_calls.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.oe_form_field_many2one_buttons button
{
margin: 0em .2em .2em 0em;
}
55 changes: 55 additions & 0 deletions Chapter14/r3_rpc_calls/static/src/js/r3_rpc_calls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
odoo.define('r3_rpc_calls', function(require)
{
var registry = require('web.field_registry'),
AbstractField = require('web.AbstractField');

var FieldMany2OneButtons = AbstractField.extend({
template: 'FieldMany2OneButtons',
className: 'oe_form_field_many2one_buttons',
supportedFieldTypes: ['many2one'],
events: {
'click .btn': '_button_clicked',
},
willStart: function()
{
var deferred = new jQuery.Deferred(),
self = this;
self.user_list = {}
this._rpc({
model: this.field.relation,
method: 'search_read',
fields: ['display_name'],
domain: this.field.domain,
})
.then(function(records)
{
_.each(records, function(record)
{
self.user_list[record.id] = record;
self.user_list[record.id].name = record.display_name;
});
deferred.resolve();
});
return jQuery.when(
this._super.apply(this, arguments),
deferred
);
},
_render: function()
{
this.renderElement();
},
_button_clicked: function(e)
{
this._setValue(
parseInt(jQuery(e.target).attr('data-id'))
);
},
});

registry.add('many2one_buttons', FieldMany2OneButtons);

return {
FieldMany2OneButtons: FieldMany2OneButtons,
}
});
Loading

0 comments on commit b070d92

Please sign in to comment.