From fcf2c9f8d82a58720f34759bf6e34f52b7458f13 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Tue, 11 Jul 2023 07:52:58 +0200 Subject: [PATCH 01/69] cms_form: apply pre-commit --- cms_form/controllers/main.py | 9 +- cms_form/models/cms_form.py | 5 +- cms_form/models/cms_form_mixin.py | 9 +- cms_form/static/src/js/ajax.js | 16 +-- cms_form/static/src/js/date_widget.js | 16 +-- cms_form/static/src/js/lock_copy_paste.js | 10 +- cms_form/static/src/js/master_slave.js | 28 ++--- cms_form/static/src/js/select2widgets.js | 20 ++-- cms_form/static/src/js/textarea_widget.js | 6 +- cms_form/templates/form.xml | 3 +- cms_form/tests/test_controllers.py | 8 +- cms_form/tests/test_form_base.py | 12 ++- cms_form/tests/test_form_cms.py | 13 ++- cms_form/tests/test_form_permission.py | 9 +- cms_form/tests/test_form_search.py | 31 ++++-- cms_form/tests/widgets/test_widget_base.py | 4 +- cms_form/tests/widgets/test_widget_binary.py | 101 ++++++++++++++---- cms_form/tests/widgets/test_widget_boolean.py | 5 +- cms_form/tests/widgets/test_widget_date.py | 16 ++- cms_form/tests/widgets/test_widget_float.py | 5 +- cms_form/tests/widgets/test_widget_hidden.py | 43 ++++++-- cms_form/tests/widgets/test_widget_integer.py | 5 +- .../tests/widgets/test_widget_many2one.py | 44 ++++++-- cms_form/tests/widgets/test_widget_radio.py | 18 +++- .../tests/widgets/test_widget_selection.py | 46 ++++++-- cms_form/tests/widgets/test_widget_text.py | 29 +++-- cms_form/tests/widgets/test_widget_x2many.py | 50 +++++++-- 27 files changed, 398 insertions(+), 163 deletions(-) diff --git a/cms_form/controllers/main.py b/cms_form/controllers/main.py index 0f57ffd9..6d8feb65 100644 --- a/cms_form/controllers/main.py +++ b/cms_form/controllers/main.py @@ -136,8 +136,7 @@ class CMSFormController(http.Controller, FormControllerMixin): website=True, ) def cms_form(self, model, model_id=None, **kw): - """Handle a `form` route. - """ + """Handle a `form` route.""" return self.make_response(model, model_id=model_id, **kw) @@ -171,8 +170,7 @@ class CMSWizardFormController(http.Controller, WizardFormControllerMixin): website=True, ) def cms_wiz(self, wiz_model, model_id=None, **kw): - """Handle a wizard route. - """ + """Handle a wizard route.""" return self.make_response(wiz_model, model_id=model_id, **kw) @@ -199,8 +197,7 @@ class CMSSearchFormController(http.Controller, SearchFormControllerMixin): website=True, ) def cms_form(self, model, **kw): - """Handle a search `form` route. - """ + """Handle a search `form` route.""" response = self.make_response(model, **kw) return response diff --git a/cms_form/models/cms_form.py b/cms_form/models/cms_form.py index 9d322b57..adaa79f1 100644 --- a/cms_form/models/cms_form.py +++ b/cms_form/models/cms_form.py @@ -138,11 +138,9 @@ def form_get_validator(self, fname, field): def form_before_create_or_update(self, values, extra_values): """Pre create/update hook.""" - pass def form_after_create_or_update(self, values, extra_values): """Post create/update hook.""" - pass def _form_purge_non_model_fields(self, values): """Purge fields that are not in `form_model` schema and return them.""" @@ -151,7 +149,8 @@ def _form_purge_non_model_fields(self, values): return extra_values _model_fields = list( self.form_model.fields_get( - self._form_model_fields, attributes=self._form_fields_attributes, + self._form_model_fields, + attributes=self._form_fields_attributes, ).keys() ) submitted_keys = list(values.keys()) diff --git a/cms_form/models/cms_form_mixin.py b/cms_form/models/cms_form_mixin.py index 57a58df1..cf0a1c79 100644 --- a/cms_form/models/cms_form_mixin.py +++ b/cms_form/models/cms_form_mixin.py @@ -262,7 +262,8 @@ def _form_fields(self): _model_fields = {} if self._form_model: _model_fields = self.form_model.fields_get( - self._form_model_fields, attributes=self._form_fields_attributes, + self._form_model_fields, + attributes=self._form_fields_attributes, ) # inject defaults defaults = self.form_model.default_get(self._form_model_fields) @@ -588,7 +589,11 @@ def _form_json_info(self): { "master_slave": self._form_master_slave_info(), "model": self._form_model, - "form_content_selector": getattr(self, "_form_content_selector", None,), + "form_content_selector": getattr( + self, + "_form_content_selector", + None, + ), } ) return info diff --git a/cms_form/static/src/js/ajax.js b/cms_form/static/src/js/ajax.js index c9ea0f57..d390fb61 100644 --- a/cms_form/static/src/js/ajax.js +++ b/cms_form/static/src/js/ajax.js @@ -1,4 +1,4 @@ -odoo.define("cms_form.ajax", function(require) { +odoo.define("cms_form.ajax", function (require) { "use strict"; var core = require("web.core"), @@ -9,7 +9,7 @@ odoo.define("cms_form.ajax", function(require) { events: { submit: "submit_form", }, - start: function() { + start: function () { this.data = this.$el.data("form"); if (this.$el.data("ajax-onchange")) { this.$el.on("change", this.proxy("submit_form")); @@ -20,7 +20,7 @@ odoo.define("cms_form.ajax", function(require) { .find(".pagination a[href]") .on("click", this.proxy("pager")); }, - ajax_submit: function(additional_data) { + ajax_submit: function (additional_data) { return jQuery.ajax(_.str.sprintf("/cms/ajax/search/%s", this.data.model), { data: this.$el.serialize() + @@ -32,7 +32,7 @@ odoo.define("cms_form.ajax", function(require) { error: this.proxy("error"), }); }, - submit_form: function(ev) { + submit_form: function (ev) { var $container = this.$container(); jQuery.blockUI(); @@ -41,17 +41,17 @@ odoo.define("cms_form.ajax", function(require) { return this.ajax_submit(); }, - success: function(data) { + success: function (data) { jQuery.unblockUI(); var $container = this.$container(); $container.html(data.content); $container.find(".pagination a[href]").on("click", this.proxy("pager")); }, - error: function() { + error: function () { jQuery.unblockUI(); }, - pager: function(ev) { + pager: function (ev) { var $a = jQuery(ev.currentTarget); jQuery.blockUI(); @@ -59,7 +59,7 @@ odoo.define("cms_form.ajax", function(require) { return this.ajax_submit("&page=" + $a.data("page")); }, - $container: function() { + $container: function () { return this.$el .parents(".cms_form_wrapper") .find(this.data.form_content_selector); diff --git a/cms_form/static/src/js/date_widget.js b/cms_form/static/src/js/date_widget.js index 0057e6c7..c1bf0d23 100644 --- a/cms_form/static/src/js/date_widget.js +++ b/cms_form/static/src/js/date_widget.js @@ -1,4 +1,4 @@ -odoo.define("cms_form.date_widget", function(require) { +odoo.define("cms_form.date_widget", function (require) { "use strict"; var sAnimation = require("website.content.snippets.animation"); @@ -6,7 +6,7 @@ odoo.define("cms_form.date_widget", function(require) { sAnimation.registry.CMSDateWidget = sAnimation.Class.extend({ selector: ".cms_form_wrapper form input.js_datepicker", - start: function() { + start: function () { // The datepicker is attached to $fname_display field. // The real value is held by the real field input (hidden). this.$realField = this.$el.next( @@ -15,7 +15,7 @@ odoo.define("cms_form.date_widget", function(require) { this.load_options(); this.setup_datepicker(); }, - load_options: function() { + load_options: function () { // global options this.options = _.omit(this.$el.data("params"), "dp"); // Datepicker specific ones @@ -37,7 +37,7 @@ odoo.define("cms_form.date_widget", function(require) { } ); }, - setup_datepicker: function() { + setup_datepicker: function () { var self = this; var placeholder = this.$el.attr("placeholder"); // Placeholder empty: set default via lang format @@ -56,7 +56,7 @@ odoo.define("cms_form.date_widget", function(require) { // Init bootstrap-datetimepicker this.$el.datetimepicker(this.picker_options); this.picker = this.$el.data("DateTimePicker"); - this.$el.on("dp.change", function(e) { + this.$el.on("dp.change", function (e) { // Update real value field. // WARNING: this format should not be touched, it matches server side. var real_val = ""; @@ -71,11 +71,11 @@ odoo.define("cms_form.date_widget", function(require) { this.$el .closest(".input-group") .find(".js_datepicker_trigger") - .click(function() { + .click(function () { self.picker.show(); }); }, - _init_date: function() { + _init_date: function () { var self = this; // Retrieve current date from real field var defaultDate = self.$realField.val(); @@ -88,7 +88,7 @@ odoo.define("cms_form.date_widget", function(require) { this.picker.date(defaultDate); } }, - destroy: function() { + destroy: function () { this.picker.destroy(); this._super.apply(this, arguments); }, diff --git a/cms_form/static/src/js/lock_copy_paste.js b/cms_form/static/src/js/lock_copy_paste.js index 512a9c97..6973716b 100644 --- a/cms_form/static/src/js/lock_copy_paste.js +++ b/cms_form/static/src/js/lock_copy_paste.js @@ -1,18 +1,18 @@ -odoo.define("cms_form.lock_copy_paste", function(require) { +odoo.define("cms_form.lock_copy_paste", function (require) { "use strict"; var sAnimation = require("website.content.snippets.animation"); sAnimation.registry.CMSFormLockCopyPaste = sAnimation.Class.extend({ selector: ".cms_form_wrapper form .lock_copy_paste", - start: function() { + start: function () { this.setup_handlers(); }, - setup_handlers: function() { - this.$el.bind("cut copy paste", function(e) { + setup_handlers: function () { + this.$el.bind("cut copy paste", function (e) { e.preventDefault(); }); - this.$el.on("contextmenu", function() { + this.$el.on("contextmenu", function () { return false; }); }, diff --git a/cms_form/static/src/js/master_slave.js b/cms_form/static/src/js/master_slave.js index dde4cc00..76313464 100644 --- a/cms_form/static/src/js/master_slave.js +++ b/cms_form/static/src/js/master_slave.js @@ -1,4 +1,4 @@ -odoo.define("cms_form.master_slave", function(require) { +odoo.define("cms_form.master_slave", function (require) { "use strict"; /* Handle master / slave fields automatically. @@ -11,12 +11,12 @@ odoo.define("cms_form.master_slave", function(require) { sAnimation.registry.CMSFormMasterSlave = sAnimation.Class.extend({ selector: ".cms_form_wrapper form", - start: function() { + start: function () { this.data = this.$el.data("form"); this.setup_handlers(); this.load_master_slave(); }, - setup_handlers: function() { + setup_handlers: function () { this.handlers = { hide: $.proxy(this.handle_hide, this), show: $.proxy(this.handle_show, this), @@ -26,22 +26,22 @@ odoo.define("cms_form.master_slave", function(require) { no_required: $.proxy(this.handle_no_required, this), }; }, - load_master_slave: function() { + load_master_slave: function () { var self = this; - $.each(this.data.master_slave, function(master, slaves) { + $.each(this.data.master_slave, function (master, slaves) { var $master_input = $('[name="' + master + '"]'); - $.each(slaves, function(action, mapping) { + $.each(slaves, function (action, mapping) { var handler = self.handlers[action]; if (handler) { $master_input - .on("change", function() { + .on("change", function () { var $input = $(this), val = $input.val(); if ($input.is(":checkbox")) { // Value == 'on' => true/false val = $input.is(":checked"); } - $.each(mapping, function(slave_fname, values) { + $.each(mapping, function (slave_fname, values) { if (_.contains(values, val)) { handler(slave_fname); } @@ -56,32 +56,32 @@ odoo.define("cms_form.master_slave", function(require) { }); }); }, - handle_hide: function(slave_fname) { + handle_hide: function (slave_fname) { $('[name="' + slave_fname + '"]') .closest(".form-group") .hide(); }, - handle_show: function(slave_fname) { + handle_show: function (slave_fname) { $('[name="' + slave_fname + '"]') .closest(".form-group") .show(); }, - handle_readonly: function(slave_fname) { + handle_readonly: function (slave_fname) { $('[name="' + slave_fname + '"]') .attr("disabled", "disabled") .closest(".form-group") .addClass("disabled"); }, - handle_no_readonly: function(slave_fname) { + handle_no_readonly: function (slave_fname) { $('[name="' + slave_fname + '"]') .attr("disabled", null) .closest(".form-group") .removeClass("disabled"); }, - handle_required: function(slave_fname) { + handle_required: function (slave_fname) { $('[name="' + slave_fname + '"]').attr("required", "required"); }, - handle_no_required: function(slave_fname) { + handle_no_required: function (slave_fname) { $('[name="' + slave_fname + '"]').attr("required", null); }, }); diff --git a/cms_form/static/src/js/select2widgets.js b/cms_form/static/src/js/select2widgets.js index 3a1c0f30..caf30bbf 100644 --- a/cms_form/static/src/js/select2widgets.js +++ b/cms_form/static/src/js/select2widgets.js @@ -1,4 +1,4 @@ -odoo.define("cms_form.select2widgets", function(require) { +odoo.define("cms_form.select2widgets", function (require) { "use strict"; var ajax = require("web.ajax"); @@ -10,14 +10,14 @@ odoo.define("cms_form.select2widgets", function(require) { return $.Deferred().reject("DOM doesn't contain '.js_select2_m2m_widget'"); } - $(document).ready(function() { - $("input.js_select2_m2m_widget").each(function() { + $(document).ready(function () { + $("input.js_select2_m2m_widget").each(function () { var $input = $(this); $input.select2({ multiple: true, tags: true, tokenSeparators: [",", " ", "_"], - formatResult: function(term) { + formatResult: function (term) { var formatted = _.escape(term.text); if (term.isNew) { formatted = @@ -25,7 +25,7 @@ odoo.define("cms_form.select2widgets", function(require) { } return formatted; }, - query: function(options) { + query: function (options) { var domain = []; if (options.term) { domain.push([ @@ -46,15 +46,15 @@ odoo.define("cms_form.select2widgets", function(require) { fields: $input.data("fields"), context: weContext.get(), }, - }).then(function(data) { + }).then(function (data) { var display_name = $input.data("display_name"); - data.sort(function(a, b) { + data.sort(function (a, b) { return a[display_name].localeCompare(b[display_name]); }); var res = { results: [], }; - _.each(data, function(x) { + _.each(data, function (x) { res.results.push({ id: x.id, text: x[display_name], @@ -65,9 +65,9 @@ odoo.define("cms_form.select2widgets", function(require) { }); }, // Default tags from the input value - initSelection: function(element, callback) { + initSelection: function (element, callback) { var data = []; - _.each(element.data("init-value"), function(x) { + _.each(element.data("init-value"), function (x) { data.push({ id: x.id, text: x.name, diff --git a/cms_form/static/src/js/textarea_widget.js b/cms_form/static/src/js/textarea_widget.js index 92424201..c3b6e075 100644 --- a/cms_form/static/src/js/textarea_widget.js +++ b/cms_form/static/src/js/textarea_widget.js @@ -1,11 +1,11 @@ -odoo.define("cms_form.textarea_widget", function(require) { +odoo.define("cms_form.textarea_widget", function (require) { "use strict"; require("web.dom_ready"); - $(document).ready(function() { + $(document).ready(function () { $("textarea[maxlength]") - .bind("input propertychange", function() { + .bind("input propertychange", function () { var $self = $(this), maxlength = parseInt($self.attr("maxlength"), 10), length = $self.val().length, diff --git a/cms_form/templates/form.xml b/cms_form/templates/form.xml index 4ae67d3e..ce16e366 100644 --- a/cms_form/templates/form.xml +++ b/cms_form/templates/form.xml @@ -126,7 +126,8 @@ License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).