Merge lp:~ajite/web-addons/7.0-web-addons-add-0001 into lp:~webaddons-core-editors/web-addons/7.0

Status: Merged
Merged at revision: 38
Proposed branch: lp:~ajite/web-addons/7.0-web-addons-add-0001
Merge into: lp:~webaddons-core-editors/web-addons/7.0
Diff against target: 152 lines (+134/-0)
3 files modified
web_polymorphic_many2one/__init__.py (+21/-0)
web_polymorphic_many2one/__openerp__.py (+49/-0)
web_polymorphic_many2one/static/src/js/view_form.js (+64/-0)
To merge this branch: bzr merge lp:~ajite/web-addons/7.0-web-addons-add-0001
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp code review Approve
Stefan Rijnhart (Opener) Approve
Holger Brunn (Therp) code review Approve
Review via email: mp+210620@code.launchpad.net

Description of the change

Added a polymorphic widget based on many2one widget (different from reference field).
Use case available at https://www.openerp.com/apps/7.0/mail_organizer/

To post a comment you must log in.
Revision history for this message
Sylvain LE GAL (GRAP) (sylvain-legal) wrote :

Hi,

Thanks for your MP.

2 little thinks :
1/ maybe it could be great to rename the module with the type of the field. Something like : 'web_many2one_polymorphic'.

2/ I tested your code with the mail_organizer module available on lp.

In this module,if new_model is 'none' and I click to select a new ressource, it's raise an Error. Maybe the widget have to manage this case.

Log below :

Traceback (most recent call last):
  File "~/ocb-server/openerp/netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "~/ocb-server/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "~/ocb-server/openerp/osv/osv.py", line 190, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "~/ocb-server/openerp/osv/osv.py", line 132, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "~/ocb-server/openerp/osv/osv.py", line 199, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "~/ocb-server/openerp/osv/osv.py", line 186, in execute_cr
    raise except_osv('Object Error', 'Object %s doesn\'t exist' % str(obj))

Regards.

review: Needs Fixing (code review, test)
24. By Augustin Cisterne-Kaas - www.elico-corp.com

[FIX] polymorphic widget is set to readonly if linked model value is none

Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Hi Sylvain,

Thank you for your feedback.

Indeed 'web_many2one_polymorphic' is more accurate.

I found a fix using javascript but I am not sure it's the best solution.

Revision history for this message
Sylvain LE GAL (GRAP) (sylvain-legal) wrote :

Thanks for the changes.

I'll test the module again next week.

Regards.

Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Thanks,

Don't forget to rename the module dependence web_polymorphic to web_many2one_polymorphic in the mail organizer __openerp__.py

Regards

Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Would not web_polymorphic_many2one be a better name (more generic) ?

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

> Would not web_polymorphic_many2one be a better name (more generic) ?

I think so.

Could you document in the manifest how the field should be declared in python (in `_columns`)?

Thanks

review: Needs Information
Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Hi Guewen,

Thanks for your feedback.

Do you mean by manifest the "__openerp__.py" ?

Kind regards,

Augustin Cisterne-Kaas

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

On 04/02/2014 03:40 AM, Augustin Cisterne-Kaas - www.elico-corp.com wrote:
> Hi Guewen,
>
> Thanks for your feedback.
>
> Do you mean by manifest the "__openerp__.py" ?
>
> Kind regards,
>
> Augustin Cisterne-Kaas
>

Yes

25. By Augustin Cisterne-Kaas - www.elico-corp.com

[ADD] Fields declaration in manifest and renamed the module.

Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Dear all,

I have made the requested modifications.

Kind Regards,

Augustin Cisterne-Kaas

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Is there a good reason why you did not make this a widget for the standard reference field type itself, instead of storing the model and the resource id separately?

review: Needs Information
Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Hi Stefan,

Thank you for your feedback,

Yes there is.

The standard reference field end result is serialized and therefore does not work for standard model such as "mail.message".

Kind Regards,

Augustin Cisterne-Kaas

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Hi Augustin, thank you for your response. I know reference field values are stored in the form of '<model>,<id>'. How does this not work for mail.message?

Revision history for this message
Holger Brunn (Therp) (hbrunn) wrote :

Having model and id in separate fields allows you to do nifty stuff like
'my_mails': fields.one2many('mail.message', 'res_id', domain=['model', '=', 'my.model'])
and joins are a lot more efficient of course, which is why they used the split approach for mail.message.

You can of course wrap model and res_id in a function field which is a reference field, but then the UI to pick the correct model is unusable (remember all the trickery I had to suffer through for fetchmail_inbox).

With this, we can simply set the model in the context or in on_change (judging from the code, that should work too)

review: Approve (code review)
Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Yes, Exactly !

Thanks for answering :)

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

OK, thanks for the explanation! LGTM.

review: Approve
Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Hi,

Can that module be approved or should I change something ?

Regards,

Augustin Cisterne-Kaas

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Hi Augustin,

LGTM finally!

Thanks for you work.

Since Sylvain's comments have been addressed, I'll merge it.

review: Approve (code review)
Revision history for this message
Sylvain LE GAL (GRAP) (sylvain-legal) wrote :

Hi.

Sorry, I totally forgot to review again that MP.

review: Abstain
Revision history for this message
Augustin Cisterne-Kaas - www.elico-corp.com (ajite) wrote :

Thanks a lot :)!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'web_polymorphic_many2one'
2=== added file 'web_polymorphic_many2one/__init__.py'
3--- web_polymorphic_many2one/__init__.py 1970-01-01 00:00:00 +0000
4+++ web_polymorphic_many2one/__init__.py 2014-04-11 03:02:09 +0000
5@@ -0,0 +1,21 @@
6+# -*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# OpenERP, Open Source Management Solution
10+# Copyright (c) 2010-2014 Elico Corp. All Rights Reserved.
11+# Augustin Cisterne-Kaas <augustin.cisterne-kaas@elico-corp.com>
12+#
13+# This program is free software: you can redistribute it and/or modify
14+# it under the terms of the GNU Affero General Public License as
15+# published by the Free Software Foundation, either version 3 of the
16+# License, or (at your option) any later version.
17+#
18+# This program is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU Affero General Public License for more details.
22+#
23+# You should have received a copy of the GNU Affero General Public License
24+# along with this program. If not, see <http://www.gnu.org/licenses/>.
25+#
26+##############################################################################
27
28=== added file 'web_polymorphic_many2one/__openerp__.py'
29--- web_polymorphic_many2one/__openerp__.py 1970-01-01 00:00:00 +0000
30+++ web_polymorphic_many2one/__openerp__.py 2014-04-11 03:02:09 +0000
31@@ -0,0 +1,49 @@
32+# -*- coding: utf-8 -*-
33+##############################################################################
34+#
35+# OpenERP, Open Source Management Solution
36+# Copyright (c) 2010-2014 Elico Corp. All Rights Reserved.
37+# Augustin Cisterne-Kaas <augustin.cisterne-kaas@elico-corp.com>
38+#
39+# This program is free software: you can redistribute it and/or modify
40+# it under the terms of the GNU Affero General Public License as
41+# published by the Free Software Foundation, either version 3 of the
42+# License, or (at your option) any later version.
43+#
44+# This program is distributed in the hope that it will be useful,
45+# but WITHOUT ANY WARRANTY; without even the implied warranty of
46+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47+# GNU Affero General Public License for more details.
48+#
49+# You should have received a copy of the GNU Affero General Public License
50+# along with this program. If not, see <http://www.gnu.org/licenses/>.
51+#
52+##############################################################################
53+{'name': 'Web Polymorphic Many2One',
54+ 'version': '0.2',
55+ 'category': 'Web',
56+ 'depends': ['web'],
57+ 'author': 'Elico Corp',
58+ 'license': 'AGPL-3',
59+ 'website': 'https://www.elico-corp.com',
60+ 'description': """
61+Add a new widget named "polymorphic"
62+The polymorphic field allow to dynamically store an id linked to any model in
63+OpenERP instead of the usual fixed one in the view definition
64+
65+
66+Python fields declaration:
67+
68+ 'model': fields.many2one('ir.model', string='Model'),
69+ 'object_id': fields.integer("Resource")
70+
71+XML fields declaration:
72+
73+ <field name="model" invisible="1" />
74+ <field name="object_id" widget="polymorphic" polymorphic="model" />
75+""",
76+ 'js': [
77+ 'static/src/js/view_form.js'
78+ ],
79+ 'installable': True,
80+ 'application': False}
81
82=== added directory 'web_polymorphic_many2one/static'
83=== added directory 'web_polymorphic_many2one/static/src'
84=== added directory 'web_polymorphic_many2one/static/src/js'
85=== added file 'web_polymorphic_many2one/static/src/js/view_form.js'
86--- web_polymorphic_many2one/static/src/js/view_form.js 1970-01-01 00:00:00 +0000
87+++ web_polymorphic_many2one/static/src/js/view_form.js 2014-04-11 03:02:09 +0000
88@@ -0,0 +1,64 @@
89+/******************************************************************************
90+*
91+* OpenERP, Open Source Management Solution
92+* Copyright (c) 2010-2014 Elico Corp. All Rights Reserved.
93+* Augustin Cisterne-Kaas <augustin.cisterne-kaas@elico-corp.com>
94+*
95+* This program is free software: you can redistribute it and/or modify
96+* it under the terms of the GNU Affero General Public License as
97+* published by the Free Software Foundation, either version 3 of the
98+* License, or (at your option) any later version.
99+*
100+* This program is distributed in the hope that it will be useful,
101+* but WITHOUT ANY WARRANTY; without even the implied warranty of
102+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103+* GNU Affero General Public License for more details.
104+*
105+* You should have received a copy of the GNU Affero General Public License
106+* along with this program. If not, see <http://www.gnu.org/licenses/>.
107+*
108+******************************************************************************/
109+openerp.web_polymorphic = function (instance) {
110+ instance.web.form.FieldPolymorphic = instance.web.form.FieldMany2One.extend( {
111+ template: "FieldMany2One",
112+ events: {
113+ 'focus input': function(e) {
114+ this.field.relation = this.field_manager.get_field_value(this.polymorphic);
115+ },
116+ 'click input': function(e) {
117+ this.field.relation = this.field_manager.get_field_value(this.polymorphic);
118+ }
119+ },
120+ init: function(field_manager, node) {
121+ this._super(field_manager, node);
122+ this.polymorphic = this.node.attrs.polymorphic;
123+ },
124+ render_editable: function() {
125+ var self = this;
126+ this.$drop_down = this.$el.find(".oe_m2o_drop_down_button");
127+ this.$drop_down.click(function() {
128+ self.polymorphic = self.node.attrs.polymorphic;
129+ self.field.relation = self.field_manager.get_field_value(self.polymorphic);
130+ });
131+ this._super();
132+ this.set_polymorphic_event();
133+ this.set({
134+ readonly: true
135+ });
136+
137+ },
138+ set_polymorphic_event: function() {
139+ self = this;
140+ this.field_manager.fields[this.polymorphic].$el.on(
141+ 'change', function(){
142+ field_value = self.field_manager.get_field_value(self.polymorphic);
143+ if(field_value !== false)
144+ self.set("effective_readonly", false);
145+ else
146+ self.set("effective_readonly", true);
147+ }
148+ );
149+ }
150+ });
151+ instance.web.form.widgets.add('polymorphic', 'instance.web.form.FieldPolymorphic')
152+};