Browse Source

[MIG] web_widget_float_formula: Migrate to 11.0

pull/754/head
Minh H.G 7 years ago
parent
commit
d06ef1fb4b
  1. 2
      web_widget_float_formula/__manifest__.py
  2. 178
      web_widget_float_formula/static/src/js/web_widget_float_formula.js
  3. 243
      web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js
  4. 4
      web_widget_float_formula/tests/test_js.py

2
web_widget_float_formula/__manifest__.py

@ -6,7 +6,7 @@
{
'name': 'Web Widget - Formulas in Float Fields',
'summary': 'Allow use of simple formulas in float fields',
'version': '10.0.1.0.0',
'version': '11.0.1.0.0',
'category': 'Web',
'author': 'GRAP, LasLabs, Odoo Community Association (OCA)',
'website': 'http://www.grap.coop',

178
web_widget_float_formula/static/src/js/web_widget_float_formula.js

@ -7,105 +7,97 @@
odoo.define('web_widget_float_formula', function(require) {
"use strict";
var form_view = require('web.FormView');
form_view.include({
// Ensure that formula is computed even if user saves right away and
// clean up '_formula_text' value to avoid bugs in tree view
_process_save: function(save_obj) {
for (var f in this.fields) {
if (!this.fields.hasOwnProperty(f)) { continue; }
f = this.fields[f];
if (f.hasOwnProperty('_formula_text') && f.$el.find('input').length > 0) {
f._compute_result();
f._clean_formula_text();
}
}
var core = require('web.core');
var basic_fields = require('web.basic_fields');
var FieldFloat = basic_fields.FieldFloat;
FieldFloat.include({
start: function() {
this._super();
this.on('blurred', this, this._compute_result);
this.on('focused', this, this._display_formula);
return this._super(save_obj);
this.setUpFocus();
},
});
var core = require('web.core');
core.bus.on('web_client_ready', null, function () {
// Import localization values used to eval formula
var translation_params = core._t.database.parameters;
var decimal_point = translation_params.decimal_point;
var thousands_sep = translation_params.thousands_sep;
var field_float = require('web.form_widgets').FieldFloat;
field_float.include({
start: function() {
this._super();
this.on('blurred', this, this._compute_result);
this.on('focused', this, this._display_formula);
return this;
},
initialize_content: function() {
this._clean_formula_text();
return this._super();
},
_formula_text: '',
_clean_formula_text: function() {
this._formula_text = '';
},
_process_formula: function(formula) {
try{
formula = formula.toString();
} catch (ex) {
return false;
}
var clean_formula = formula.toString().replace(/^\s+|\s+$/g, '');
if (clean_formula[0] == '=') {
clean_formula = clean_formula.substring(1);
var myreg = new RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/', 'g');
if (clean_formula.replace(myreg, '') === '') {
return clean_formula;
}
}
return false;
},
_eval_formula: function(formula) {
var value;
formula = formula.replace(thousands_sep, '').replace(decimal_point, '.');
try {
value = eval(formula);
}
catch(e) {}
if (typeof value != 'undefined') {
return value;
}
setUpFocus: function() {
var self = this;
this.$el.on({
focus: function() { self.trigger('focused'); },
blur: function() { self.trigger('blurred'); }
});
},
commitChanges: function() {
this._compute_result();
},
_formula_text: '',
_clean_formula_text: function() {
this._formula_text = '';
},
_process_formula: function(formula) {
try{
formula = formula.toString();
} catch (ex) {
return false;
},
_compute_result: function() {
this._clean_formula_text();
var input = this.$input.val();
var formula = this._process_formula(input);
if (formula !== false) {
var value = this._eval_formula(formula);
if (value !== false) {
this._formula_text = "=" + formula;
this.set_value(value);
// Force rendering to avoid format loss if there's no change
this.render_value();
}
}
var clean_formula = formula.toString().replace(/^\s+|\s+$/g, '');
if (clean_formula[0] == '=') {
clean_formula = clean_formula.substring(1);
var myreg = new RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/', 'g');
if (clean_formula.replace(myreg, '') === '') {
return clean_formula;
}
},
}
return false;
},
_eval_formula: function(formula) {
// Import localization values used to eval formula
var translation_params = core._t.database.parameters;
var decimal_point = translation_params.decimal_point;
var thousands_sep = translation_params.thousands_sep;
var value;
formula = formula.replace(thousands_sep, '').replace(decimal_point, '.');
try {
value = eval(formula);
}
catch(e) {}
if (typeof value != 'undefined') {
return value;
}
return false;
},
_compute_result: function() {
this._clean_formula_text();
// Display the formula stored in the field to allow modification
_display_formula: function() {
if (this._formula_text !== '') {
this.$input.val(this._formula_text);
var input = this.$input.val();
var formula = this._process_formula(input);
if (formula !== false) {
var value = this._eval_formula(formula);
if (value !== false) {
this._formula_text = "=" + formula;
var decimal_point = core._t.database.parameters.decimal_point;
// _setValue
this._setValue(value.toString().replace(decimal_point, '.'))
this._render();
}
},
});
}
},
// Display the formula stored in the field to allow modification
_display_formula: function() {
if (this._formula_text !== '') {
this.$input.val(this._formula_text);
}
},
});
});

243
web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js

@ -3,159 +3,100 @@
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
**/
odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_widgets', 'web.core'], function(test) {
'use strict';
window.test_setup = function(self, form_common, form_widgets, core) {
core.bus.trigger('web_client_ready');
var field_manager = new form_common.DefaultFieldManager(null, {});
var filler = {'attrs': {}}; // Needed to instantiate FieldFloat
self.field = new form_widgets.FieldFloat(field_manager, filler);
self.field.$input = $('<input>');
self.field.$label = $('<label>');
};
test('Float fields should have a _formula_text property that defaults to an empty string',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.strictEqual(this.field._formula_text, '');
});
test('.initialize_content() on float fields should clear the _formula_text property',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field._formula_text = 'test';
this.field.initialize_content();
assert.strictEqual(this.field._formula_text, '');
});
test('._clean_formula_text() on float fields should clear the _formula_text property',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field._formula_text = 'test';
this.field._clean_formula_text();
assert.strictEqual(this.field._formula_text, '');
});
test('._process_formula() on float fields should return false when given invalid formulas',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.strictEqual(this.field._process_formula('2*3'), false);
assert.strictEqual(this.field._process_formula('=2*3a'), false);
});
test('._process_formula() on float fields should properly process a valid formula',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.strictEqual(this.field._process_formula(' =2*3\n'), '2*3');
});
test('._eval_formula() on float fields should properly evaluate a valid formula',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.equal(this.field._eval_formula('2*3'), 6);
});
test('._eval_formula() on float fields should properly handle alternative decimal points and thousands seps',
function(assert, form_common, form_widgets, core) {
var translation_params = core._t.database.parameters;
translation_params.decimal_point = ',';
translation_params.thousands_sep = '.';
window.test_setup(this, form_common, form_widgets, core);
assert.equal(this.field._eval_formula('2.000*3,5'), 7000);
});
test('._eval_formula() on float fields should return false when given an input that evals to undefined',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.equal(this.field._eval_formula(''), false);
});
test('._eval_formula() on float fields should return false when given an input that cannot be evaluated',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
assert.equal(this.field._eval_formula('*/'), false);
});
test('._compute_result() on float fields should always clean up _formula_text',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field._formula_text = 'test';
this.field._compute_result();
assert.strictEqual(this.field._formula_text, '');
});
test('._compute_result() should not change the value of the associated input when it is not a valid formula',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field.$input.val('=2*3a');
this.field._compute_result();
assert.strictEqual(this.field.$input.val(), '=2*3a');
});
test('._compute_result() should not change the value of the associated input when it cannot be evaled',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field.$input.val('=*/');
this.field._compute_result();
assert.strictEqual(this.field.$input.val(), '=*/');
});
test('._compute_result() should behave properly when the current value of the input element is a valid formula',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field.$input.val('=2*3');
this.field._compute_result();
assert.equal(this.field.$input.val(), '6');
assert.strictEqual(this.field._formula_text, '=2*3');
});
test('._display_formula() should update the value of the input element when there is a stored formula',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field._formula_text = "test";
this.field._display_formula();
assert.equal(this.field.$input.val(), 'test');
});
test('.start() on float fields should add a handler that calls ._compute_result() when the field is blurred',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field.called = false;
this.field._compute_result = function() {
this.called = true;
odoo.define('web_widget_float_formula', function(require) {
"use strict";
var core = require('web.core');
var testUtils = require('web.test_utils');
var FormView = require('web.FormView');
var createView = testUtils.createView;
var triggerKeypressEvent = testUtils.triggerKeypressEvent;
var assertClose = function(assert, actual, expected, message) {
var pass = Math.abs(actual - expected) < 0.00001;
assert.pushResult({
result: pass,
actual: actual,
expected: expected,
message: message
});
}
QUnit.module('web_widget_float_formula', {
beforeEach: function() {
this.data = {
foo: {
fields: {
bar: { string: "Bar", type: "float" }
},
records: [
{ id: 1, bar: 1.2 }
]
}
};
this.field.start();
this.field.trigger('blurred');
assert.strictEqual(this.field.called, true);
}
});
test('.start() on float fields should add a handler that calls ._display_formula() when the field is focused',
function(assert, form_common, form_widgets, core) {
window.test_setup(this, form_common, form_widgets, core);
this.field.called = false;
this.field._display_formula = function() {
this.called = true;
};
this.field.start();
this.field.trigger('focused');
QUnit.test('Float fields in form view', function(assert) {
assert.expect(8);
var form = createView({
View: FormView,
model: 'foo',
data: this.data,
res_id: 1,
arch: '<form><field name="bar"/></form>',
viewOptions: {
mode: 'edit',
},
mockRPC: function (route, args) {
if (args.method === 'write') {
assert.step('save');
}
return this._super.apply(this, arguments);
},
});
assertClose(assert, form.$('input').val(), 1.2);
form.$('input').val('=(1 + 2) / 3').blur();
assertClose(assert, form.$('input').val(), 1,
'blur event should trigger compute event');
form.$('input').focus();
assert.strictEqual(form.$('input').val(), '=(1 + 2) / 3',
'focus event should display the forumla');
form.$('input').val('=(1 * 2x) /').blur();
assert.strictEqual(form.$('input').val(), '=(1 * 2x) /',
'invalid formula should not be calculated');
_.extend(core._t.database.parameters, {
grouping: [3, 0],
decimal_point: ',',
thousands_sep: '.'
});
form.$('input').val('=2.000*3,5').blur();
assert.strictEqual(form.$('input').val(), "7.000,00",
'eval should handle decimal point and thousands separator properly');
_.extend(core._t.database.parameters, {
grouping: [3, 0],
decimal_point: '.',
thousands_sep: ','
});
form.$('input').val('=3-2');
form.$buttons.find('.o_form_button_save').click();
assert.verifySteps(['save'], 'should have saved');
assertClose(assert, form.$('.o_field_widget').text(), 1,
'save should also trigger compute result')
form.destroy();
});
assert.strictEqual(this.field.called, true);
});
});

4
web_widget_float_formula/tests/test_js.py

@ -5,12 +5,12 @@
import odoo.tests
class TestJS(odoo.tests.HttpCase):
class FloatFormulaSuite(odoo.tests.HttpCase):
def test_js(self):
self.phantom_js(
"/web/tests?module=web_widget_float_formula",
"console.log('ok')",
"",
"",
login="admin"
)
Loading…
Cancel
Save