From 6966470f9da94cb5e839a8d1ef1fd9ce8ad1c686 Mon Sep 17 00:00:00 2001 From: Jonathan Nemry Date: Fri, 11 Apr 2014 14:11:45 +0200 Subject: [PATCH] [ADD] web_readonly_bypass: allow to save an onchange modifications on readonly fields --- web_readonly_bypass/__init__.py | 28 +++++ web_readonly_bypass/__openerp__.py | 61 ++++++++++ .../static/src/js/readonly_bypass.js | 109 ++++++++++++++++++ web_readonly_bypass/views/readonly_bypass.xml | 12 ++ 4 files changed, 210 insertions(+) create mode 100644 web_readonly_bypass/__init__.py create mode 100644 web_readonly_bypass/__openerp__.py create mode 100644 web_readonly_bypass/static/src/js/readonly_bypass.js create mode 100644 web_readonly_bypass/views/readonly_bypass.xml diff --git a/web_readonly_bypass/__init__.py b/web_readonly_bypass/__init__.py new file mode 100644 index 00000000..5fa4a2b1 --- /dev/null +++ b/web_readonly_bypass/__init__.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Authors: Nemry Jonathan & Laetitia Gangloff +# Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu) +# All Rights Reserved +# +# WARNING: This program as such is intended to be used by professional +# programmers who take the whole responsibility of assessing all potential +# consequences resulting from its eventual inadequacies and bugs. +# End users who are looking for a ready-to-use solution with commercial +# guarantees and support are strongly advised to contact a Free Software +# Service Company. +# +# 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 . +# +############################################################################## diff --git a/web_readonly_bypass/__openerp__.py b/web_readonly_bypass/__openerp__.py new file mode 100644 index 00000000..7f6410e1 --- /dev/null +++ b/web_readonly_bypass/__openerp__.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Authors: Nemry Jonathan & Laetitia Gangloff +# Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu) +# All Rights Reserved +# +# WARNING: This program as such is intended to be used by professional +# programmers who take the whole responsibility of assessing all potential +# consequences resulting from its eventual inadequacies and bugs. +# End users who are looking for a ready-to-use solution with commercial +# guarantees and support are strongly advised to contact a Free Software +# Service Company. +# +# 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 . +# +############################################################################## +{ + 'name': 'Read Only ByPass', + 'version': '1.0', + "author": "ACSONE SA/NV", + "maintainer": "ACSONE SA/NV", + "website": "http://www.acsone.eu", + 'category': 'Technical Settings', + 'depends': [ + 'base', + 'web', + ], + 'description': """ +Read Only ByPass +================ +This Module provides a solution to the problem of the interaction between +'readonly' attribute and 'on_change' attribute when used together + +Behavior: add `readonly_fields` changed by `on_change` into the fields passing +into an update or create. If `filter_out_readonly` is into the context and set +True then apply native behavior. + """, + 'images': [], + 'data': [ + 'views/readonly_bypass.xml', + ], + 'qweb': [ + ], + 'css': [], + 'demo': [], + 'test': [], + 'installable': True, + 'auto_install': False, +} diff --git a/web_readonly_bypass/static/src/js/readonly_bypass.js b/web_readonly_bypass/static/src/js/readonly_bypass.js new file mode 100644 index 00000000..d8581f3e --- /dev/null +++ b/web_readonly_bypass/static/src/js/readonly_bypass.js @@ -0,0 +1,109 @@ +/* + * Allow to bypass readonly fi the value is changed + */ + +openerp.readonly_bypass = function(instance) { + + var QWeb = instance.web.qweb, _t = instance.web._t; + /** + * ignore readonly: place options['readonly_fields'] into the data + * if nothing is specified into the context + * + * create mode: remove read-only keys having a 'false' value + * + * @param boolean mode: True case of create, false case of write + * @param {Object} context->filter_out_readonly + * @param {Object} data field values to possibly be updated + * @param {Object} options Dictionary that can contain the following keys: + * - readonly_fields: Values from readonly fields to merge into the data object + */ + function ignore_readonly(data, options, mode, context){ + if (options){ + if (!('filter_out_readonly' in context && context['filter_out_readonly'] == true + && 'readonly_fields' in options && options['readonly_fields'])) { + if(mode){ + $.each( options.readonly_fields, function( key, value ) { + if(value==false){ + delete(options.readonly_fields[key]); + } + }); + } + data = $.extend(data,options['readonly_fields']) + } + } + }; + + instance.web.BufferedDataSet.include({ + + init : function() { + this._super.apply(this, arguments); + }, + /** + * Creates Overriding + * + * @param {Object} data field values to set on the new record + * @param {Object} options Dictionary that can contain the following keys: + * - readonly_fields: Values from readonly fields that were updated by + * on_changes. Only used by the BufferedDataSet to make the o2m work correctly. + * @returns super {$.Deferred} + */ + create : function(data, options) { + var self = this; + ignore_readonly(data, options, true, self.context); + return self._super(data,options); + }, + /** + * Creates Overriding + * + * @param {Object} data field values to set on the new record + * @param {Object} options Dictionary that can contain the following keys: + * - readonly_fields: Values from readonly fields that were updated by + * on_changes. Only used by the BufferedDataSet to make the o2m work correctly. + * @returns super {$.Deferred} + */ + write : function(id, data, options) { + var self = this; + ignore_readonly(data, options, false, self.context); + return self._super(id,data,options); + }, + + }); + + instance.web.DataSet.include({ + /* + BufferedDataSet: case of 'add an item' into a form view + */ + init : function() { + this._super.apply(this, arguments); + }, + /** + * Creates Overriding + * + * @param {Object} data field values to set on the new record + * @param {Object} options Dictionary that can contain the following keys: + * - readonly_fields: Values from readonly fields that were updated by + * on_changes. Only used by the BufferedDataSet to make the o2m work correctly. + * @returns super {$.Deferred} + */ + create : function(data, options) { + var self = this; + ignore_readonly(data, options, true, self.context); + return self._super(data,options); + }, + /** + * Creates Overriding + * + * @param {Object} data field values to set on the new record + * @param {Object} options Dictionary that can contain the following keys: + * - readonly_fields: Values from readonly fields that were updated by + * on_changes. Only used by the BufferedDataSet to make the o2m work correctly. + * @returns super {$.Deferred} + */ + write : function(id, data, options) { + var self = this; + ignore_readonly(data, options, false, self.context); + return self._super(id,data,options); + }, + + }); +}; diff --git a/web_readonly_bypass/views/readonly_bypass.xml b/web_readonly_bypass/views/readonly_bypass.xml new file mode 100644 index 00000000..2bad7ace --- /dev/null +++ b/web_readonly_bypass/views/readonly_bypass.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file