Skip to content

Commit

Permalink
[FIX] server_action_mass_edit : Fixed the wizard errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzappdev committed Apr 23, 2024
1 parent 88aaf44 commit 6ae0372
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
6 changes: 6 additions & 0 deletions server_action_mass_edit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@
"views/ir_actions_server.xml",
"wizard/mass_editing_wizard.xml",
],
"assets": {
"web.assets_backend": [
"/server_action_mass_edit/static/src/js/record.esm.js",
"/server_action_mass_edit/static/src/js/static_list.esm.js",
]
},
"demo": ["demo/mass_editing.xml"],
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class IrActionsServerMassEditLine(models.Model):
("name", "not in", %s),
("ttype", "not in", ["reference", "function"]),
("model_id", "=", model_id),
("readonly", "!=", True),
]
"""
% str(MAGIC_FIELDS),
Expand Down
29 changes: 29 additions & 0 deletions server_action_mass_edit/static/src/js/record.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @odoo-module **/
import {Record} from "@web/model/relational_model/record";
import {patch} from "@web/core/utils/patch";

patch(Record.prototype, {
_createStaticListDatapoint(data, fieldName) {
const {related, limit, defaultOrderBy} = this.activeFields[fieldName];
const config = {
resModel: this.fields[fieldName].relation,
activeFields: (related && related.activeFields) || {},
fields: (related && related.fields) || {},
relationField: this.fields[fieldName].relation_field || false,
offset: 0,
// To prevent the TypeError: data.map is not a function while set/remove/add
// value in wizard
resIds: Array.isArray(data) ? data.map((r) => r.id || null) : [],
orderBy: defaultOrderBy || [],
limit: limit || Number.MAX_SAFE_INTEGER,
currentCompanyId: this.currentCompanyId,
context: {},
};
const options = {
onUpdate: ({withoutOnchange} = {}) =>
this._update({[fieldName]: []}, {withoutOnchange}),
parent: this,
};
return new this.model.constructor.StaticList(this.model, config, data, options);
},
});
31 changes: 31 additions & 0 deletions server_action_mass_edit/static/src/js/static_list.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @odoo-module **/
import {StaticList} from "@web/model/relational_model/static_list";
import {markRaw} from "@odoo/owl";
import {patch} from "@web/core/utils/patch";

patch(StaticList.prototype, {
setup(config, data, options = {}) {
this._parent = options.parent;
this._onUpdate = options.onUpdate;

this._cache = markRaw({});
this._commands = [];
this._savePoint = undefined;
this._unknownRecordCommands = {};
this._currentIds = [...this.resIds];
this._needsReordering = false;
this._tmpIncreaseLimit = 0;
this._extendedRecords = new Set();
// To prevent the TypeError: data.slice is not a function" while set/remove/add
// value in wizard
this.records = Array.isArray(data)
? data
.slice(this.offset, this.limit)
.map((r) => this._createRecordDatapoint(r))
: [];
this.count = this.resIds.length;
this.handleField = Object.keys(this.activeFields).find(
(fieldName) => this.activeFields[fieldName].isHandle
);
},
});
26 changes: 9 additions & 17 deletions server_action_mass_edit/wizard/mass_editing_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def default_get(self, fields):
res = super().default_get(fields)
server_action_id = self.env.context.get("server_action_id")
server_action = self.env["ir.actions.server"].sudo().browse(server_action_id)
active_ids = self.env.context.get("active_ids")

if not server_action:
return res

active_ids = self.env.context.get("active_ids") or self.env["ir.actions.server"]
original_active_ids = self.env.context.get("original_active_ids", active_ids)
operation_description_info = False
operation_description_warning = False
Expand Down Expand Up @@ -121,7 +121,7 @@ def _prepare_fields(self, line, field, field_info):
if field.ttype == "many2many":
selection = [
("ignore", _("Don't touch")),
("set", _("Set")),
("set_m2m", _("Set")),
("remove_m2m", _("Remove")),
("add", _("Add")),
]
Expand Down Expand Up @@ -181,9 +181,9 @@ def _insert_field_in_arch(self, line, field, main_xml_group):
dummy, tree_view = comodel._get_view(view_type="tree")
field_context = {}
if form_view:
field_context["form_view_ref"] = form_view.id
field_context["form_view_ref"] = form_view.xml_id
if tree_view:
field_context["tree_view_ref"] = tree_view.id
field_context["tree_view_ref"] = tree_view.xml_id
if field_context:
field_element.attrib["context"] = json.dumps(field_context)
else:
Expand All @@ -204,9 +204,7 @@ def _insert_field_in_arch(self, line, field, main_xml_group):
def _get_field_options(self, field):
return {
"name": field.name,
"modifiers": '{"invisible": "\
"[["selection__%s", "in", ["ignore", "remove"]]]}'
% field.name,
"invisible": 'selection__%s in ["ignore", "remove", False]' % field.name,
"class": "w-75",
}

Expand Down Expand Up @@ -284,11 +282,9 @@ def create(self, vals_list):
if key.startswith("selection_"):
split_key = key.split("__", 1)[1]
if val == "set" or val == "add_o2m":
if val == "set":
vals[split_key] = vals[split_key][0][1:]
values.update({split_key: vals.get(split_key, False)})

elif val == "set_o2m":
elif val == "set_o2m" or val == "set_m2m":
values.update(
{split_key: [(6, 0, [])] + vals.get(split_key, [])}
)
Expand All @@ -299,19 +295,15 @@ def create(self, vals_list):
elif val == "remove_m2m":
m2m_list = []
if vals.get(split_key):
vals[split_key][0] = vals[split_key][0][1:]
for m2m_id in vals.get(split_key, False)[0]:
m2m_list.append((3, m2m_id))
for m2m_id in vals.get(split_key, False):
m2m_list.append((3, m2m_id[1]))
if m2m_list:
values.update({split_key: m2m_list})
else:
values.update({split_key: [(5, 0, [])]})

elif val == "add":
m2m_list = []
for m2m_id in vals.get(split_key, False):
m2m_list.append(m2m_id)
values.update({split_key: m2m_list})
values.update({split_key: vals.get(split_key, False)})

if values:
self.env[server_action.model_id.model].browse(
Expand Down

0 comments on commit 6ae0372

Please sign in to comment.