Browse Source

cherry-pick 790a853 by pass readonly fields only if context is set with

readonly_by_pass. This context can either be a bool or field name array
pull/162/head
Pierre Verkest 10 years ago
parent
commit
c4bc379de7
  1. 86
      web_readonly_bypass/static/src/js/readonly_bypass.js
  2. 168
      web_readonly_bypass/static/test/web_readonly_bypass.js
  3. 9
      web_readonly_bypass/views/readonly_bypass.xml

86
web_readonly_bypass/static/src/js/readonly_bypass.js

@ -5,34 +5,70 @@
openerp.web_readonly_bypass = function(instance) { openerp.web_readonly_bypass = function(instance) {
var QWeb = instance.web.qweb, _t = instance.web._t; 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 ('readonly_fields' in options && options['readonly_fields'] &&
!('filter_out_readonly' in context && context['filter_out_readonly'] == true )) {
if(mode){
var instance = instance;
instance.web_readonly_bypass = {
/**
* 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 {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
* @param boolean mode: True case of create, false case of write
* @param {Object} context->readonly_by_pass
*/
ignore_readonly: function(data, options, mode, context){
console.log(options );
console.log(context );
var readonly_by_pass_fields = this.retrieve_readonly_by_pass_fields(
options, context);
console.log(readonly_by_pass_fields );
if(mode){
$.each( readonly_by_pass_fields, function( key, value ) {
if(value==false){
delete(readonly_by_pass_fields[key]);
}
});
}
data = $.extend(data,readonly_by_pass_fields);
console.log(data );
},
/**
* retrieve_readonly_by_pass_fields: retrieve readonly fields to save
* according context.
*
* @param {Object} options Dictionary that can contain the following keys:
* - readonly_fields: all values from readonly fields
* @param {Object} context->readonly_by_pass: Can be true if all
* all readonly fields should be saved or an array of field name to
* save ie: ['readonly_field_1', 'readonly_field_2']
* @returns {Object}: readonly key/value fields to save according context
*/
retrieve_readonly_by_pass_fields: function(options, context){
var readonly_by_pass_fields = {};
if (options && 'readonly_fields' in options &&
options['readonly_fields'] && context &&
'readonly_by_pass' in context && context['readonly_by_pass']){
if (_.isArray(context['readonly_by_pass'])){
$.each( options.readonly_fields, function( key, value ) { $.each( options.readonly_fields, function( key, value ) {
if(value==false){
delete(options.readonly_fields[key]);
if(_.contains(context['readonly_by_pass'], key)){
readonly_by_pass_fields[key] = value;
} }
}); });
}else{
readonly_by_pass_fields = options.readonly_fields;
} }
data = $.extend(data,options['readonly_fields'])
} }
}
return readonly_by_pass_fields;
},
}; };
readonly_bypass = instance.web_readonly_bypass;
instance.web.BufferedDataSet.include({ instance.web.BufferedDataSet.include({
init : function() { init : function() {
@ -49,7 +85,7 @@ openerp.web_readonly_bypass = function(instance) {
*/ */
create : function(data, options) { create : function(data, options) {
var self = this; var self = this;
ignore_readonly(data, options, true, self.context);
readonly_bypass.ignore_readonly(data, options, true, self.context);
return self._super(data,options); return self._super(data,options);
}, },
/** /**
@ -63,7 +99,7 @@ openerp.web_readonly_bypass = function(instance) {
*/ */
write : function(id, data, options) { write : function(id, data, options) {
var self = this; var self = this;
ignore_readonly(data, options, false, self.context);
readonly_bypass.ignore_readonly(data, options, false, self.context);
return self._super(id,data,options); return self._super(id,data,options);
}, },
@ -87,7 +123,7 @@ openerp.web_readonly_bypass = function(instance) {
*/ */
create : function(data, options) { create : function(data, options) {
var self = this; var self = this;
ignore_readonly(data, options, true, self.context);
readonly_bypass.ignore_readonly(data, options, true, self.context);
return self._super(data,options); return self._super(data,options);
}, },
/** /**
@ -101,7 +137,7 @@ openerp.web_readonly_bypass = function(instance) {
*/ */
write : function(id, data, options) { write : function(id, data, options) {
var self = this; var self = this;
ignore_readonly(data, options, false, self.context);
readonly_bypass.ignore_readonly(data, options, false, self.context);
return self._super(id,data,options); return self._super(id,data,options);
}, },

168
web_readonly_bypass/static/test/web_readonly_bypass.js

@ -0,0 +1,168 @@
openerp.testing.section( 'web_readonly_bypass', {},
function(test){
test('ignore_readonly', function(instance){
openerp.web_readonly_bypass(instance);
var data = {};
var mode_create = true;
var options = {};
var context = {};
instance.web_readonly_bypass.ignore_readonly(data, options,
mode_create, context);
deepEqual(data,
{},
"Empty context and options mode create"
);
mode_create = false;
data = {};
instance.web_readonly_bypass.ignore_readonly(data, options,
mode_create, context);
deepEqual(data,
{},
"Empty context and options mode write"
);
mode_create = false;
data = {};
context = {'readonly_by_pass': true};
options = {'readonly_fields': {'field_1': 'va1-1',
'field_2': false,
'field_3': 'val-3'}};
instance.web_readonly_bypass.ignore_readonly(data, options,
mode_create, context);
deepEqual(data,
{'field_1': 'va1-1', 'field_2': false, 'field_3': 'val-3'},
"all fields mode write"
);
mode_create = true;
data = {};
context = {'readonly_by_pass': true};
options = {'readonly_fields': {'field_1': 'va1-1',
'field_2': false,
'field_3': 'val-3'}};
instance.web_readonly_bypass.ignore_readonly(data, options,
mode_create, context);
deepEqual(data,
{'field_1': 'va1-1', 'field_3': 'val-3'},
"all fields mode create (false value are escaped)"
);
mode_create = true;
data = {};
context = {};
options = {'readonly_fields': {'field_1': 'va1-1',
'field_2': false,
'field_3': 'val-3'}};
instance.web_readonly_bypass.ignore_readonly(data, options,
mode_create, context);
deepEqual(data,
{},
"without context, default, we won't save readonly fields"
);
});
test('retrieve_readonly_by_pass_fields', function(instance){
openerp.web_readonly_bypass(instance);
var context = {'readonly_by_pass': true}
var options = {'readonly_fields': {'field_1': 'va1-1',
'field_2': 'val-2',
'field_3': 'val-3'}};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{'field_1': 'va1-1', 'field_2': 'val-2', 'field_3': 'val-3'},
"All fields should be accepted!"
);
context = {'readonly_by_pass': ['field_1', 'field_3']};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{'field_1': 'va1-1','field_3': 'val-3'},
"two field s1"
);
context = {'readonly_by_pass': ['field_1',]};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{'field_1': 'va1-1'},
"Only field 1"
);
context = {'readonly_by_pass': []};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"Empty context field"
);
context = null;
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"Null context"
);
context = false;
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"false context"
);
context = {'readonly_by_pass': true}
options = {'readonly_fields': {'field_1': 'va1-1'}};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{'field_1': 'va1-1'},
"Only one option"
);
options = {'readonly_fields': {}};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"Empty readonly_fields option"
);
options = {};
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"Empty option"
);
options = null;
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"null option"
);
options = false;
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"false option"
);
context = false;
deepEqual(
instance.web_readonly_bypass.retrieve_readonly_by_pass_fields(
options, context),
{},
"false option and false context"
);
});
});

9
web_readonly_bypass/views/readonly_bypass.xml

@ -1,12 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- vim:fdn=3:
-->
<openerp> <openerp>
<data> <data>
<template id="assets_backend" name="readonly_bypass" inherit_id="web.assets_backend">
<template id="assets_backend" name="web_readonly_bypass" inherit_id="web.assets_backend">
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<script type="text/javascript" src="/web_readonly_bypass/static/src/js/readonly_bypass.js"></script> <script type="text/javascript" src="/web_readonly_bypass/static/src/js/readonly_bypass.js"></script>
</xpath> </xpath>
</template> </template>
<template id="qunit_suite" name="web_readonly_bypass" inherit_id="web.qunit_suite">
<xpath expr="//head" position="inside">
<script type="text/javascript" src="/web_readonly_bypass/static/test/web_readonly_bypass.js"></script>
</xpath>
</template>
</data> </data>
</openerp> </openerp>
Loading…
Cancel
Save