| 1 |
|
| 2 |
|
| 3 |
import { assets } from "@web/core/assets"; |
| 4 |
import { browser } from "@web/core/browser/browser"; |
| 5 |
import { Domain } from "@web/core/domain"; |
| 6 |
import { |
| 7 |
deserializeDate, |
| 8 |
deserializeDateTime, |
| 9 |
parseDateTime, |
| 10 |
serializeDate, |
| 11 |
serializeDateTime, |
| 12 |
} from "@web/core/l10n/dates"; |
| 13 |
import { registry } from "@web/core/registry"; |
| 14 |
import { intersection, unique } from "@web/core/utils/arrays"; |
| 15 |
import { deepCopy, pick } from "@web/core/utils/objects"; |
| 16 |
import { patchRPCWithCleanup, makeMockFetch } from "./mock_services"; |
| 17 |
import { patchWithCleanup } from "./utils"; |
| 18 |
import { makeErrorFromResponse } from "@web/core/network/rpc"; |
| 19 |
import { registerCleanup } from "./cleanup"; |
| 20 |
|
| 21 |
const domParser = new DOMParser(); |
| 22 |
const xmlSerializer = new XMLSerializer(); |
| 23 |
|
| 24 |
const regex_field_agg = /(\w+):(\w+)/; |
| 25 |
|
| 26 |
const VALID_AGGREGATE_FUNCTIONS = [ |
| 27 |
"array_agg", |
| 28 |
"count", |
| 29 |
"count_distinct", |
| 30 |
"bool_and", |
| 31 |
"bool_or", |
| 32 |
"max", |
| 33 |
"min", |
| 34 |
"avg", |
| 35 |
"sum", |
| 36 |
]; |
| 37 |
let logId = 1; |
| 38 |
|
| 39 |
const DEFAULT_FIELD_VALUES = { |
| 40 |
char: false, |
| 41 |
many2one: false, |
| 42 |
one2many: [], |
| 43 |
many2many: [], |
| 44 |
monetary: 0, |
| 45 |
binary: false, |
| 46 |
integer: 0, |
| 47 |
float: 0, |
| 48 |
boolean: false, |
| 49 |
date: false, |
| 50 |
datetime: false, |
| 51 |
html: false, |
| 52 |
text: false, |
| 53 |
selection: false, |
| 54 |
reference: false, |
| 55 |
properties: [], |
| 56 |
json: false, |
| 57 |
}; |
| 58 |
|
| 59 |
const READ_GROUP_NUMBER_GRANULARITY = [ |
| 60 |
"year_number", |
| 61 |
"quarter_number", |
| 62 |
"month_number", |
| 63 |
"iso_week_number", |
| 64 |
"day_of_year", |
| 65 |
"day_of_month", |
| 66 |
"day_of_week", |
| 67 |
"hour_number", |
| 68 |
"minute_number", |
| 69 |
"second_number", |
| 70 |
]; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
function makeLogger(prefix, title) { |
| 81 |
const log = (bullet, colorValue, ...data) => { |
| 82 |
const color = `color: ${colorValue}`; |
| 83 |
const styles = [[color, "font-weight: bold"].join(";")]; |
| 84 |
|
| 85 |
let msg = `${bullet} %c[${prefix}:${id}]`; |
| 86 |
if (title) { |
| 87 |
msg += `%c ${title}`; |
| 88 |
styles.push(color); |
| 89 |
} |
| 90 |
|
| 91 |
console.log(msg, ...styles, ...data); |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
const request = (...data) => { |
| 99 |
hasCalledRequest = true; |
| 100 |
log("->", "#66e", ...data); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
const response = (...data) => { |
| 108 |
if (!hasCalledRequest) { |
| 109 |
console.warn(`Response logged before request.`); |
| 110 |
} |
| 111 |
log("<-", "#f80", ...data); |
| 112 |
hasCalledRequest = false; |
| 113 |
}; |
| 114 |
|
| 115 |
const id = logId++; |
| 116 |
let hasCalledRequest = false; |
| 117 |
|
| 118 |
return { request, response }; |
| 119 |
} |
| 120 |
|
| 121 |
export function makeServerError({ |
| 122 |
code, |
| 123 |
context, |
| 124 |
description, |
| 125 |
message, |
| 126 |
subType, |
| 127 |
errorName, |
| 128 |
type, |
| 129 |
args, |
| 130 |
} = {}) { |
| 131 |
return makeErrorFromResponse({ |
| 132 |
code: code || 0, |
| 133 |
message: message || "Odoo Server Error", |
| 134 |
data: { |
| 135 |
name: errorName || `odoo.exceptions.${type || "UserError"}`, |
| 136 |
debug: "traceback", |
| 137 |
arguments: args || [], |
| 138 |
context: context || {}, |
| 139 |
subType, |
| 140 |
message: description, |
| 141 |
}, |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
function traverseElementTree(tree, cb) { |
| 150 |
if (cb(tree)) { |
| 151 |
Array.from(tree.children).forEach((c) => traverseElementTree(c, cb)); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
export class MockServer { |
| 160 |
active = true; |
| 161 |
constructor(data, options = {}) { |
| 162 |
this.init(data, options); |
| 163 |
} |
| 164 |
|
| 165 |
init(data, options) { |
| 166 |
this.models = data.models || {}; |
| 167 |
this.actions = data.actions || {}; |
| 168 |
this.menus = data.menus || null; |
| 169 |
this.archs = data.views || {}; |
| 170 |
this.debug = options.debug || false; |
| 171 |
Object.entries(this.models).forEach(([modelName, model]) => { |
| 172 |
model.fields = { |
| 173 |
id: { string: "ID", type: "integer" }, |
| 174 |
display_name: { string: "Display Name", type: "char" }, |
| 175 |
name: { string: "Name", type: "char", default: "name" }, |
| 176 |
write_date: { string: "Last Modified on", type: "datetime" }, |
| 177 |
...model.fields, |
| 178 |
}; |
| 179 |
for (const fieldName in model.fields) { |
| 180 |
model.fields[fieldName].name = fieldName; |
| 181 |
} |
| 182 |
}); |
| 183 |
Object.entries(this.models).forEach(([modelName, model]) => { |
| 184 |
model.records = model.records || []; |
| 185 |
for (var i = 0; i < model.records.length; i++) { |
| 186 |
const values = model.records[i]; |
| 187 |
|
| 188 |
const id = values.id === undefined ? this.getUnusedID(modelName) : values.id; |
| 189 |
|
| 190 |
model.records[i] = { id }; |
| 191 |
|
| 192 |
this.applyDefaults(model, values); |
| 193 |
this.writeRecord(modelName, values, id, { ensureIntegrity: false }); |
| 194 |
} |
| 195 |
model.onchanges = model.onchanges || {}; |
| 196 |
model.methods = model.methods || {}; |
| 197 |
}); |
| 198 |
|
| 199 |
|
| 200 |
for (const modelName in this.models) { |
| 201 |
const records = this.models[modelName].records; |
| 202 |
if (!Array.isArray(this.models[modelName].records)) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
records.forEach((record) => this.updateComodelRelationalFields(modelName, record)); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
async performRPC(route, args) { |
| 217 |
const logger = makeLogger("RPC", route); |
| 218 |
args = JSON.parse(JSON.stringify(args)); |
| 219 |
if (this.debug) { |
| 220 |
logger.request(args); |
| 221 |
} |
| 222 |
const result = await this._performRPC(route, args); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
const actualResult = JSON.parse(JSON.stringify(result !== undefined ? result : false)); |
| 237 |
if (this.debug) { |
| 238 |
logger.response(actualResult); |
| 239 |
} |
| 240 |
return actualResult; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
_getViewFields(modelName, viewType, models) { |
| 254 |
if (["kanban", "list", "form"].includes(viewType)) { |
| 255 |
for (const fieldNames of Object.values(models)) { |
| 256 |
fieldNames.add("id"); |
| 257 |
fieldNames.add("write_date"); |
| 258 |
} |
| 259 |
} else if (viewType === "search") { |
| 260 |
models[modelName] = Object.keys(this.models[modelName].fields); |
| 261 |
} else if (viewType === "graph") { |
| 262 |
for (const [fieldName, field] of Object.entries(this.models[modelName].fields)) { |
| 263 |
if (["integer", "float"].includes(field.type)) { |
| 264 |
models[modelName].add(fieldName); |
| 265 |
} |
| 266 |
} |
| 267 |
} else if (viewType === "pivot") { |
| 268 |
for (const [fieldName, field] of Object.entries(this.models[modelName].fields)) { |
| 269 |
if ( |
| 270 |
[ |
| 271 |
"many2one", |
| 272 |
"many2many", |
| 273 |
"char", |
| 274 |
"boolean", |
| 275 |
"selection", |
| 276 |
"date", |
| 277 |
"datetime", |
| 278 |
].includes(field.type) |
| 279 |
) { |
| 280 |
models[modelName].add(fieldName); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
return models; |
| 285 |
} |
| 286 |
|
| 287 |
getView(modelName, args, kwargs) { |
| 288 |
if (!(modelName in this.models)) { |
| 289 |
throw new Error(`Model ${modelName} was not defined in mock server data`); |
| 290 |
} |
| 291 |
|
| 292 |
let [viewId, viewType] = args; |
| 293 |
if (!viewId) { |
| 294 |
const contextKey = viewType + "_view_ref"; |
| 295 |
if (contextKey in kwargs.context) { |
| 296 |
viewId = kwargs.context[contextKey]; |
| 297 |
} |
| 298 |
} |
| 299 |
const key = [modelName, viewId, viewType].join(","); |
| 300 |
let arch = this.archs[key]; |
| 301 |
if (!arch) { |
| 302 |
const genericViewKey = Object.keys(this.archs).find((fullKey) => { |
| 303 |
const [_model, , _viewType] = fullKey.split(","); |
| 304 |
return _model === modelName && _viewType === viewType; |
| 305 |
}); |
| 306 |
if (genericViewKey) { |
| 307 |
arch = this.archs[genericViewKey]; |
| 308 |
viewId = parseInt(genericViewKey.split(",")[1], 10) || false; |
| 309 |
} |
| 310 |
} |
| 311 |
if (!arch) { |
| 312 |
throw new Error("No arch found for key " + key); |
| 313 |
} |
| 314 |
|
| 315 |
const fields = Object.assign({}, this.models[modelName].fields); |
| 316 |
for (const fieldName in fields) { |
| 317 |
fields[fieldName].name = fieldName; |
| 318 |
} |
| 319 |
|
| 320 |
const view = this._getView({ |
| 321 |
arch, |
| 322 |
modelName, |
| 323 |
fields, |
| 324 |
context: kwargs.context || {}, |
| 325 |
models: this.models, |
| 326 |
}); |
| 327 |
if (kwargs.options.toolbar) { |
| 328 |
view.toolbar = this.models[modelName].toolbar || {}; |
| 329 |
} |
| 330 |
if (viewId !== undefined) { |
| 331 |
view.id = viewId; |
| 332 |
} |
| 333 |
return view; |
| 334 |
} |
| 335 |
|
| 336 |
_getView(params) { |
| 337 |
const processedNodes = params.processedNodes || []; |
| 338 |
const { arch, context, modelName } = params; |
| 339 |
const level = params.level || 0; |
| 340 |
const editable = params.editable || true; |
| 341 |
const fields = deepCopy(params.fields); |
| 342 |
function isNodeProcessed(node) { |
| 343 |
return processedNodes.findIndex((n) => n.isSameNode(node)) > -1; |
| 344 |
} |
| 345 |
const onchanges = params.models[modelName].onchanges || {}; |
| 346 |
const fieldNodes = {}; |
| 347 |
const groupbyNodes = {}; |
| 348 |
const relatedModels = { [modelName]: new Set() }; |
| 349 |
let doc; |
| 350 |
if (typeof arch === "string") { |
| 351 |
doc = domParser.parseFromString(arch, "text/xml").documentElement; |
| 352 |
} else { |
| 353 |
doc = arch; |
| 354 |
} |
| 355 |
const editableView = editable && this._editableNode(doc, modelName); |
| 356 |
const onchangeAbleView = this._onchangeAbleView(doc); |
| 357 |
const inFormView = doc.tagName === "form"; |
| 358 |
|
| 359 |
traverseElementTree(doc, (node) => { |
| 360 |
if (node.nodeType === Node.TEXT_NODE) { |
| 361 |
return false; |
| 362 |
} |
| 363 |
["required", "readonly", "invisible", "column_invisible"].forEach((attr) => { |
| 364 |
const value = node.getAttribute(attr); |
| 365 |
if (value === "1" || value === "true") { |
| 366 |
node.setAttribute(attr, "True"); |
| 367 |
} |
| 368 |
}); |
| 369 |
const isField = node.tagName === "field"; |
| 370 |
const isGroupby = node.tagName === "groupby"; |
| 371 |
if (isField) { |
| 372 |
const fieldName = node.getAttribute("name"); |
| 373 |
fieldNodes[fieldName] = { |
| 374 |
node, |
| 375 |
isInvisible: node.getAttribute("invisible") === "True", |
| 376 |
isEditable: editableView && this._editableNode(node, modelName), |
| 377 |
}; |
| 378 |
const field = fields[fieldName]; |
| 379 |
if (!field) { |
| 380 |
throw new Error("Field " + fieldName + " does not exist"); |
| 381 |
} |
| 382 |
} else if (isGroupby && !isNodeProcessed(node)) { |
| 383 |
const groupbyName = node.getAttribute("name"); |
| 384 |
fieldNodes[groupbyName] = { node }; |
| 385 |
groupbyNodes[groupbyName] = node; |
| 386 |
} |
| 387 |
if (isGroupby && !isNodeProcessed(node)) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
return !isField; |
| 391 |
}); |
| 392 |
Object.keys(fieldNodes).forEach((field) => relatedModels[modelName].add(field)); |
| 393 |
let relModel, relFields; |
| 394 |
Object.entries(fieldNodes).forEach(([name, { node, isInvisible, isEditable }]) => { |
| 395 |
const field = fields[name]; |
| 396 |
if (isEditable && (field.type === "many2one" || field.type === "many2many")) { |
| 397 |
const canCreate = node.getAttribute("can_create"); |
| 398 |
node.setAttribute("can_create", canCreate || "true"); |
| 399 |
const canWrite = node.getAttribute("can_write"); |
| 400 |
node.setAttribute("can_write", canWrite || "true"); |
| 401 |
} |
| 402 |
if (field.type === "one2many" || field.type === "many2many") { |
| 403 |
relModel = field.relation; |
| 404 |
|
| 405 |
if (inFormView && level === 0 && !node.getAttribute("widget") && !isInvisible) { |
| 406 |
const inlineViewTypes = Array.from(node.children).map((c) => c.tagName); |
| 407 |
const missingViewtypes = []; |
| 408 |
const mode = node.getAttribute("mode") || "kanban,list"; |
| 409 |
if (!intersection(inlineViewTypes, mode.split(",")).length) { |
| 410 |
|
| 411 |
missingViewtypes.push((node.getAttribute("mode") || "list").split(",")[0]); |
| 412 |
} |
| 413 |
for (const type of missingViewtypes) { |
| 414 |
let key = `${field.relation},false,${type}`; |
| 415 |
if (!this.archs[key]) { |
| 416 |
const regexp = new RegExp(`${field.relation},[a-z._0-9]+,${type}`); |
| 417 |
key = Object.keys(this.archs).find((k) => regexp.test(k)); |
| 418 |
} |
| 419 |
|
| 420 |
const arch = this.archs[key] || (type === "form" ? "<form/>" : null); |
| 421 |
if (!arch) { |
| 422 |
throw new Error(`Can't find ${type} view to inline`); |
| 423 |
} |
| 424 |
node.appendChild( |
| 425 |
domParser.parseFromString(arch, "text/xml").documentElement |
| 426 |
); |
| 427 |
} |
| 428 |
} |
| 429 |
Array.from(node.children).forEach((childNode) => { |
| 430 |
if (childNode.tagName) { |
| 431 |
relFields = Object.assign({}, params.models[relModel].fields); |
| 432 |
|
| 433 |
const { models } = this._getView({ |
| 434 |
models: params.models, |
| 435 |
arch: childNode, |
| 436 |
modelName: relModel, |
| 437 |
fields: relFields, |
| 438 |
context, |
| 439 |
processedNodes, |
| 440 |
level: level + 1, |
| 441 |
editable: editableView, |
| 442 |
}); |
| 443 |
Object.entries(models).forEach(([modelName, fields]) => { |
| 444 |
relatedModels[modelName] = relatedModels[modelName] || new Set(); |
| 445 |
fields.forEach((field) => relatedModels[modelName].add(field)); |
| 446 |
}); |
| 447 |
} |
| 448 |
}); |
| 449 |
} |
| 450 |
|
| 451 |
if (onchangeAbleView && name in onchanges) { |
| 452 |
node.setAttribute("on_change", "1"); |
| 453 |
} |
| 454 |
}); |
| 455 |
Object.entries(groupbyNodes).forEach(([name, node]) => { |
| 456 |
const field = fields[name]; |
| 457 |
if (field.type !== "many2one") { |
| 458 |
throw new Error("groupby can only target many2one"); |
| 459 |
} |
| 460 |
field.views = {}; |
| 461 |
relModel = field.relation; |
| 462 |
relFields = Object.assign({}, params.models[relModel].fields); |
| 463 |
processedNodes.push(node); |
| 464 |
|
| 465 |
const { models } = this._getView({ |
| 466 |
models: params.models, |
| 467 |
arch: node, |
| 468 |
modelName: relModel, |
| 469 |
fields: relFields, |
| 470 |
context, |
| 471 |
processedNodes, |
| 472 |
editable: false, |
| 473 |
}); |
| 474 |
Object.entries(models).forEach(([modelName, fields]) => { |
| 475 |
relatedModels[modelName] = relatedModels[modelName] || new Set(); |
| 476 |
fields.forEach((field) => relatedModels[modelName].add(field)); |
| 477 |
}); |
| 478 |
}); |
| 479 |
const processedArch = xmlSerializer.serializeToString(doc); |
| 480 |
const fieldsInView = {}; |
| 481 |
Object.entries(fields).forEach(([fname, field]) => { |
| 482 |
if (fname in fieldNodes) { |
| 483 |
fieldsInView[fname] = field; |
| 484 |
} |
| 485 |
}); |
| 486 |
const viewType = doc.tagName; |
| 487 |
return { |
| 488 |
arch: processedArch, |
| 489 |
model: modelName, |
| 490 |
type: viewType, |
| 491 |
models: this._getViewFields(modelName, viewType, relatedModels), |
| 492 |
}; |
| 493 |
} |
| 494 |
|
| 495 |
_editableNode(node, modelName) { |
| 496 |
switch (node.tagName) { |
| 497 |
case "form": |
| 498 |
return true; |
| 499 |
case "list": |
| 500 |
return node.getAttribute("editable") || node.getAttribute("multi_edit"); |
| 501 |
case "field": { |
| 502 |
const fname = node.getAttribute("name"); |
| 503 |
const field = this.models[modelName].fields[fname]; |
| 504 |
return ( |
| 505 |
!field.readonly && |
| 506 |
node.getAttribute("readonly") !== "True" && |
| 507 |
node.getAttribute("readonly") !== "1" |
| 508 |
); |
| 509 |
} |
| 510 |
default: |
| 511 |
return false; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
_onchangeAbleView(node) { |
| 516 |
if (node.tagName === "form") { |
| 517 |
return true; |
| 518 |
} else if (node.tagName === "list") { |
| 519 |
return true; |
| 520 |
} else if (node.tagName === "kanban") { |
| 521 |
return true; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
convertToOnChange(modelName, values, specification) { |
| 538 |
Object.entries(values).forEach(([fname, val]) => { |
| 539 |
const field = this.models[modelName].fields[fname]; |
| 540 |
if (field.type === "many2one" && typeof val === "number") { |
| 541 |
values[fname] = this.mockWebRead(field.relation, [[val]], { |
| 542 |
specification: specification[fname].fields || {}, |
| 543 |
})[0]; |
| 544 |
} else if (field.type === "one2many" || field.type === "many2many") { |
| 545 |
val.forEach((cmd) => { |
| 546 |
switch (cmd[0]) { |
| 547 |
case 0: |
| 548 |
case 1: |
| 549 |
cmd[2] = this.convertToOnChange( |
| 550 |
field.relation, |
| 551 |
cmd[2], |
| 552 |
specification[fname].fields || {} |
| 553 |
); |
| 554 |
break; |
| 555 |
case 4: |
| 556 |
cmd[2] = this.mockWebRead(field.relation, [[cmd[1]]], { |
| 557 |
specification: specification[fname].fields || {}, |
| 558 |
})[0]; |
| 559 |
} |
| 560 |
}); |
| 561 |
} else if (field.type === "reference") { |
| 562 |
if (val) { |
| 563 |
const [model, i] = val.split(","); |
| 564 |
const id = parseInt(i, 10); |
| 565 |
const result = this.mockWebRead(model, [[id]], { |
| 566 |
specification: specification[fname].fields || {}, |
| 567 |
}); |
| 568 |
values[fname] = { |
| 569 |
...result[0], |
| 570 |
id: { id, model }, |
| 571 |
}; |
| 572 |
} |
| 573 |
} |
| 574 |
}); |
| 575 |
return values; |
| 576 |
} |
| 577 |
|
| 578 |
async _performRPC(route, args) { |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
const methodName = args.method || route; |
| 583 |
const mockFunction = |
| 584 |
registry.category("mock_server").get(`${args.model}/${methodName}`, null) || |
| 585 |
registry.category("mock_server").get(methodName, null); |
| 586 |
if (mockFunction) { |
| 587 |
return mockFunction.call(this, route, args); |
| 588 |
} |
| 589 |
|
| 590 |
switch (route) { |
| 591 |
case "/web/webclient/load_menus": |
| 592 |
return this.mockLoadMenus(); |
| 593 |
case "/web/action/load": |
| 594 |
return this.mockLoadAction(args); |
| 595 |
case "/web/action/load_breadcrumbs": |
| 596 |
return this.mockLoadBreadcrumbs(args); |
| 597 |
} |
| 598 |
if ( |
| 599 |
route.indexOf("/web/image") >= 0 || |
| 600 |
[".png", ".jpg"].includes(route.substr(route.length - 4)) |
| 601 |
) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
switch (args.method) { |
| 606 |
case "render_public_asset": { |
| 607 |
return true; |
| 608 |
} |
| 609 |
case "action_archive": |
| 610 |
return this.mockWrite(args.model, [args.args[0], { active: false }]); |
| 611 |
case "action_unarchive": |
| 612 |
return this.mockWrite(args.model, [args.args[0], { active: true }]); |
| 613 |
case "copy": |
| 614 |
return this.mockCopy(args.model, args.args, args.kwargs); |
| 615 |
case "create": |
| 616 |
return this.mockCreate(args.model, args.args[0], args.kwargs); |
| 617 |
case "fields_get": |
| 618 |
return this.mockFieldsGet(args.model, args.fields); |
| 619 |
case "get_views": |
| 620 |
return this.mockGetViews(args.model, args.kwargs); |
| 621 |
case "name_create": |
| 622 |
return this.mockNameCreate(args.model, args.args[0], args.kwargs); |
| 623 |
case "name_search": |
| 624 |
return this.mockNameSearch(args.model, args.args, args.kwargs); |
| 625 |
case "onchange": |
| 626 |
return this.mockOnchange(args.model, args.args, args.kwargs); |
| 627 |
case "read": |
| 628 |
return this.mockRead(args.model, args.args); |
| 629 |
case "search": |
| 630 |
return this.mockSearch(args.model, args.args, args.kwargs); |
| 631 |
case "search_count": |
| 632 |
return this.mockSearchCount(args.model, args.args, args.kwargs); |
| 633 |
case "search_panel_select_range": |
| 634 |
return this.mockSearchPanelSelectRange(args.model, args.args, args.kwargs); |
| 635 |
case "search_panel_select_multi_range": |
| 636 |
return this.mockSearchPanelSelectMultiRange(args.model, args.args, args.kwargs); |
| 637 |
case "search_read": |
| 638 |
return this.mockSearchRead(args.model, args.args, args.kwargs); |
| 639 |
case "unlink": |
| 640 |
return this.mockUnlink(args.model, args.args); |
| 641 |
case "web_name_search": |
| 642 |
return this.mockWebNameSearch(args.model, args.args, args.kwargs); |
| 643 |
case "web_read": |
| 644 |
return this.mockWebRead(args.model, args.args, args.kwargs); |
| 645 |
case "web_save": |
| 646 |
return this.mockWebSave(args.model, args.args, args.kwargs); |
| 647 |
case "formatted_read_group": |
| 648 |
return this.mockFormattedReadGroup(args.model, args.kwargs); |
| 649 |
case "web_search_read": |
| 650 |
return this.mockWebSearchReadUnity(args.model, args.args, args.kwargs); |
| 651 |
case "read_progress_bar": |
| 652 |
return this.mockReadProgressBar(args.model, args.kwargs); |
| 653 |
case "write": |
| 654 |
return this.mockWrite(args.model, args.args); |
| 655 |
} |
| 656 |
const model = this.models[args.model]; |
| 657 |
const method = model && model.methods[args.method]; |
| 658 |
if (method) { |
| 659 |
return method(args.model, args.args, args.kwargs); |
| 660 |
} |
| 661 |
throw new Error(`Unimplemented route: ${route}`); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
mockCopy(modelName, [ids], kwargs = {}) { |
| 676 |
const model = this.models[modelName]; |
| 677 |
const newIDs = []; |
| 678 |
for (const id of ids) { |
| 679 |
const newID = this.getUnusedID(modelName); |
| 680 |
const originalRecord = model.records.find((record) => record.id === id); |
| 681 |
const duplicatedRecord = { ...originalRecord, ...kwargs.default, id: newID }; |
| 682 |
duplicatedRecord.display_name = `${originalRecord.display_name} (copy)`; |
| 683 |
model.records.push(duplicatedRecord); |
| 684 |
newIDs.push(newID); |
| 685 |
} |
| 686 |
return newIDs; |
| 687 |
} |
| 688 |
|
| 689 |
mockCreate(modelName, valsList, kwargs = {}) { |
| 690 |
let returnArrayOfIds = true; |
| 691 |
if (!Array.isArray(valsList)) { |
| 692 |
valsList = [valsList]; |
| 693 |
returnArrayOfIds = false; |
| 694 |
} |
| 695 |
const model = this.models[modelName]; |
| 696 |
const ids = []; |
| 697 |
for (const values of valsList) { |
| 698 |
if ("id" in values) { |
| 699 |
throw new Error("Cannot create a record with a predefinite id"); |
| 700 |
} |
| 701 |
const id = this.getUnusedID(modelName); |
| 702 |
ids.push(id); |
| 703 |
const record = { id }; |
| 704 |
model.records.push(record); |
| 705 |
this.applyDefaults(model, values, kwargs.context); |
| 706 |
this.writeRecord(modelName, values, id); |
| 707 |
this.updateComodelRelationalFields(modelName, record); |
| 708 |
} |
| 709 |
return returnArrayOfIds ? ids : ids[0]; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
mockDefaultGet(modelName, args, kwargs = {}) { |
| 721 |
const fields = args[0]; |
| 722 |
const model = this.models[modelName]; |
| 723 |
const result = {}; |
| 724 |
for (const fieldName of fields) { |
| 725 |
if (fieldName === "id") { |
| 726 |
continue; |
| 727 |
} |
| 728 |
const field = model.fields[fieldName]; |
| 729 |
const key = "default_" + fieldName; |
| 730 |
if (kwargs.context && key in kwargs.context) { |
| 731 |
if (field.type === "one2many" || field.type === "many2many") { |
| 732 |
const ids = kwargs.context[key] || []; |
| 733 |
result[fieldName] = ids.map((id) => [4, id]); |
| 734 |
} else { |
| 735 |
result[fieldName] = kwargs.context[key]; |
| 736 |
} |
| 737 |
continue; |
| 738 |
} |
| 739 |
if ("default" in field) { |
| 740 |
result[fieldName] = field.default; |
| 741 |
continue; |
| 742 |
} else { |
| 743 |
if (!(field.type in DEFAULT_FIELD_VALUES)) { |
| 744 |
throw new Error(`Missing default value for type '${field.type}'`); |
| 745 |
} |
| 746 |
result[fieldName] = DEFAULT_FIELD_VALUES[field.type]; |
| 747 |
} |
| 748 |
} |
| 749 |
for (const fieldName in result) { |
| 750 |
const field = model.fields[fieldName]; |
| 751 |
if (field.type === "many2one" && result[fieldName]) { |
| 752 |
const recordExists = this.models[field.relation].records.some( |
| 753 |
(r) => r.id === result[fieldName] |
| 754 |
); |
| 755 |
if (!recordExists) { |
| 756 |
delete result[fieldName]; |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
return result; |
| 761 |
} |
| 762 |
|
| 763 |
mockFieldsGet(modelName, fieldNames) { |
| 764 |
let fields = this.models[modelName].fields; |
| 765 |
if (fieldNames) { |
| 766 |
fields = pick(this.models[modelName].fields, ...fieldNames); |
| 767 |
} |
| 768 |
return fields; |
| 769 |
} |
| 770 |
|
| 771 |
mockLoadAction({ action_id } = {}) { |
| 772 |
const action = |
| 773 |
this.actions[action_id] || |
| 774 |
Object.values(this.actions).find((action) => action.xml_id === action_id) || |
| 775 |
Object.values(this.actions).find((a) => a.path === action_id); |
| 776 |
if (!action) { |
| 777 |
throw makeServerError({ |
| 778 |
errorName: "odoo.addons.web.controllers.action.MissingActionError", |
| 779 |
message: `The action ${JSON.stringify(action_id)} does not exist`, |
| 780 |
}); |
| 781 |
} |
| 782 |
return action; |
| 783 |
} |
| 784 |
|
| 785 |
mockLoadBreadcrumbs({ actions }) { |
| 786 |
return actions.map(({ action: action_id, model, resId }) => { |
| 787 |
if (action_id) { |
| 788 |
const act = this.mockLoadAction({ action_id }); |
| 789 |
if (resId) { |
| 790 |
return { |
| 791 |
display_name: this.mockRead(act.res_model, [[resId], ["display_name"]])[0] |
| 792 |
.display_name, |
| 793 |
}; |
| 794 |
} |
| 795 |
return { display_name: act.name }; |
| 796 |
} else if (model) { |
| 797 |
if (resId) { |
| 798 |
return { display_name: this.models[model].records[resId].display_name }; |
| 799 |
} |
| 800 |
throw new Error("Actions with a model should also have a resId"); |
| 801 |
} |
| 802 |
throw new Error("Actions should have either an action (id or path) or a model"); |
| 803 |
}); |
| 804 |
} |
| 805 |
|
| 806 |
mockLoadMenus() { |
| 807 |
let menus = this.menus; |
| 808 |
if (!menus) { |
| 809 |
menus = { |
| 810 |
root: { id: "root", children: [1], name: "root", appID: "root" }, |
| 811 |
1: { id: 1, children: [], name: "App0", appID: 1 }, |
| 812 |
}; |
| 813 |
} |
| 814 |
return menus; |
| 815 |
} |
| 816 |
|
| 817 |
mockGetViews(modelName, kwargs) { |
| 818 |
const views = {}; |
| 819 |
const models = {}; |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
const modelFields = {}; |
| 824 |
kwargs.views.forEach(([viewId, viewType]) => { |
| 825 |
views[viewType] = this.getView(modelName, [viewId, viewType], kwargs); |
| 826 |
Object.entries(views[viewType].models).forEach(([modelName, fields]) => { |
| 827 |
modelFields[modelName] = modelFields[modelName] || new Set(); |
| 828 |
fields.forEach((field) => modelFields[modelName].add(field)); |
| 829 |
}); |
| 830 |
delete views[viewType].models; |
| 831 |
}); |
| 832 |
|
| 833 |
|
| 834 |
Object.entries(modelFields).forEach(([modelName, fields]) => { |
| 835 |
models[modelName] = { fields: this.mockFieldsGet(modelName, [...fields]) }; |
| 836 |
}); |
| 837 |
|
| 838 |
if (kwargs.options.load_filters && "search" in views) { |
| 839 |
views["search"].filters = this.models[modelName].filters || []; |
| 840 |
} |
| 841 |
return { models, views }; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
mockNameCreate(modelName, name, kwargs) { |
| 855 |
const values = { |
| 856 |
name: name, |
| 857 |
display_name: name, |
| 858 |
}; |
| 859 |
const [id] = this.mockCreate(modelName, [values], kwargs); |
| 860 |
return [id, name]; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
mockNameSearch(model, args, kwargs) { |
| 875 |
const str = args && typeof args[0] === "string" ? args[0] : kwargs.name; |
| 876 |
const limit = kwargs.limit || 100; |
| 877 |
const domain = (args && args[1]) || kwargs.domain || []; |
| 878 |
const { records } = this.models[model]; |
| 879 |
const result = []; |
| 880 |
for (const r of records) { |
| 881 |
const isInDomain = this.evaluateDomain(domain, r); |
| 882 |
if (isInDomain && (!str.length || (r.display_name && r.display_name.includes(str)))) { |
| 883 |
result.push([r.id, r.display_name]); |
| 884 |
} |
| 885 |
} |
| 886 |
return result.slice(0, limit); |
| 887 |
} |
| 888 |
|
| 889 |
mockOnchange(modelName, args, kwargs) { |
| 890 |
const resId = args[0][0]; |
| 891 |
const changes = args[1]; |
| 892 |
const specification = args[3]; |
| 893 |
let fields = args[2] ? (Array.isArray(args[2]) ? args[2] : [args[2]]) : []; |
| 894 |
|
| 895 |
const onchanges = this.models[modelName].onchanges || {}; |
| 896 |
const firstOnChange = !fields.length; |
| 897 |
const fieldsFromView = new Set(Object.keys(specification)); |
| 898 |
|
| 899 |
let serverValues = {}; |
| 900 |
const onchangeValues = {}; |
| 901 |
for (const fieldName in changes) { |
| 902 |
if (!(fieldName in this.models[modelName].fields)) { |
| 903 |
throw makeServerError({ |
| 904 |
type: "ValidationError", |
| 905 |
message: `Field ${fieldName} does not exist`, |
| 906 |
}); |
| 907 |
} |
| 908 |
} |
| 909 |
if (resId) { |
| 910 |
serverValues = this.mockRead(modelName, [args[0], [...fieldsFromView]], kwargs)[0]; |
| 911 |
} else if (firstOnChange) { |
| 912 |
|
| 913 |
|
| 914 |
fields = [...fieldsFromView]; |
| 915 |
fields |
| 916 |
.filter((fName) => !Object.keys(serverValues).includes(fName) && fName !== "id") |
| 917 |
.forEach((fName) => { |
| 918 |
onchangeValues[fName] = false; |
| 919 |
}); |
| 920 |
const defaultValues = this.mockDefaultGet(modelName, [[...fieldsFromView]], kwargs); |
| 921 |
for (const fieldName in defaultValues) { |
| 922 |
const fieldType = this.models[modelName].fields[fieldName].type; |
| 923 |
if (["one2many", "many2many"].includes(fieldType)) { |
| 924 |
const subSpec = specification[fieldName]; |
| 925 |
for (const command of defaultValues[fieldName]) { |
| 926 |
if (command[0] === 0 || command[0] === 1) { |
| 927 |
command[2] = pick(command[2], ...Object.keys(subSpec.fields)); |
| 928 |
} |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
Object.assign(onchangeValues, defaultValues); |
| 933 |
} |
| 934 |
fields.forEach((field) => { |
| 935 |
if (field in onchanges) { |
| 936 |
const target = Object.assign({}, serverValues, onchangeValues, changes); |
| 937 |
const handler = { |
| 938 |
set(_, key, val) { |
| 939 |
if (target[key] !== val) { |
| 940 |
onchangeValues[key] = val; |
| 941 |
target[key] = val; |
| 942 |
} |
| 943 |
return true; |
| 944 |
}, |
| 945 |
}; |
| 946 |
onchanges[field](new Proxy(target, handler)); |
| 947 |
} |
| 948 |
}); |
| 949 |
for (const fieldName in onchangeValues) { |
| 950 |
if (!fieldsFromView.has(fieldName)) { |
| 951 |
delete onchangeValues[fieldName]; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
return { |
| 956 |
value: this.convertToOnChange(modelName, onchangeValues, specification), |
| 957 |
}; |
| 958 |
} |
| 959 |
|
| 960 |
mockRead(modelName, args) { |
| 961 |
const model = this.models[modelName]; |
| 962 |
let fields; |
| 963 |
if (args[1] && args[1].length) { |
| 964 |
fields = [...new Set(args[1].concat(["id"]))]; |
| 965 |
} else { |
| 966 |
fields = Object.keys(model.fields); |
| 967 |
} |
| 968 |
const ids = Array.isArray(args[0]) ? args[0] : [args[0]]; |
| 969 |
const records = []; |
| 970 |
|
| 971 |
|
| 972 |
const modelMap = { |
| 973 |
[modelName]: {}, |
| 974 |
}; |
| 975 |
for (const record of model.records) { |
| 976 |
modelMap[modelName][record.id] = record; |
| 977 |
} |
| 978 |
for (const fieldName of fields) { |
| 979 |
const field = model.fields[fieldName]; |
| 980 |
if (!field) { |
| 981 |
continue; |
| 982 |
} |
| 983 |
const { relation, type } = field; |
| 984 |
if (type === "many2one" && !modelMap[relation]) { |
| 985 |
modelMap[relation] = {}; |
| 986 |
for (const record of this.models[relation].records) { |
| 987 |
modelMap[relation][record.id] = record; |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
for (const id of ids) { |
| 993 |
if (!id) { |
| 994 |
throw new Error( |
| 995 |
"mock read: falsy value given as id, would result in an access error in actual server !" |
| 996 |
); |
| 997 |
} |
| 998 |
const record = modelMap[modelName][id]; |
| 999 |
if (!record) { |
| 1000 |
continue; |
| 1001 |
} |
| 1002 |
const result = { id: record.id }; |
| 1003 |
for (const fieldName of fields) { |
| 1004 |
const field = model.fields[fieldName]; |
| 1005 |
if (!field) { |
| 1006 |
continue; |
| 1007 |
} |
| 1008 |
if (["float", "integer", "monetary"].includes(field.type)) { |
| 1009 |
|
| 1010 |
result[fieldName] = record[fieldName] || 0; |
| 1011 |
} else if (field.type === "many2one") { |
| 1012 |
const relRecord = modelMap[field.relation][record[fieldName]]; |
| 1013 |
if (relRecord) { |
| 1014 |
result[fieldName] = [record[fieldName], relRecord.display_name]; |
| 1015 |
} else { |
| 1016 |
result[fieldName] = false; |
| 1017 |
} |
| 1018 |
} else if (field.type === "one2many" || field.type === "many2many") { |
| 1019 |
result[fieldName] = record[fieldName] || []; |
| 1020 |
} else { |
| 1021 |
result[fieldName] = record[fieldName] !== undefined ? record[fieldName] : false; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
records.push(result); |
| 1025 |
} |
| 1026 |
|
| 1027 |
return records; |
| 1028 |
} |
| 1029 |
|
| 1030 |
mockFormattedReadGroup(modelName, kwargs) { |
| 1031 |
const fields = this.models[modelName].fields; |
| 1032 |
const aggregateFields = (group, records) => { |
| 1033 |
for (const { fieldName, func, name } of aggregatedFields) { |
| 1034 |
if (["sum", "avg"].includes(func)) { |
| 1035 |
if (!records.length) { |
| 1036 |
group[name] = false; |
| 1037 |
} else { |
| 1038 |
group[name] = 0; |
| 1039 |
for (const record of records) { |
| 1040 |
group[name] += record[fieldName]; |
| 1041 |
} |
| 1042 |
|
| 1043 |
} |
| 1044 |
} else if (func === "array_agg") { |
| 1045 |
if (fields[fieldName].type === "many2one") { |
| 1046 |
group[name] = records.map((r) => r[fieldName] || null); |
| 1047 |
} else { |
| 1048 |
group[name] = records.map((r) => (fieldName in r ? r[fieldName] : null)); |
| 1049 |
} |
| 1050 |
} else if (func === "__count") { |
| 1051 |
group[name] = records.length; |
| 1052 |
} else if (func === "count_distinct") { |
| 1053 |
group[name] = unique(records.map((r) => r[fieldName])).filter(Boolean).length; |
| 1054 |
} else if (func === "bool_or") { |
| 1055 |
group[name] = records.some((r) => Boolean(r[fieldName])); |
| 1056 |
} else if (func === "bool_and") { |
| 1057 |
group[name] = records.every((r) => Boolean(r[fieldName])); |
| 1058 |
} else { |
| 1059 |
throw new Error(`Aggregate "${func}" not implemented in MockServer`); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
}; |
| 1063 |
const { domain, groupby, aggregates, offset, limit, order } = kwargs; |
| 1064 |
const records = this.getRecords(modelName, domain); |
| 1065 |
const aggregatedFields = aggregates.map((fspec) => { |
| 1066 |
if (fspec === "__count") { |
| 1067 |
return { fieldName: "__count", func: "__count", name: "__count" }; |
| 1068 |
} |
| 1069 |
const [, fieldName, func] = fspec.match(regex_field_agg); |
| 1070 |
if (func && !VALID_AGGREGATE_FUNCTIONS.includes(func)) { |
| 1071 |
throw new Error(`invalid aggregation function "${func}"`); |
| 1072 |
} |
| 1073 |
if (!fields[fieldName]) { |
| 1074 |
throw new Error(`invalid field in "${fspec}"`); |
| 1075 |
} |
| 1076 |
return { fieldName, func, name: fspec }; |
| 1077 |
}); |
| 1078 |
|
| 1079 |
if (!groupby.length) { |
| 1080 |
const group = { __extra_domain: [] }; |
| 1081 |
aggregateFields(group, records); |
| 1082 |
return [group]; |
| 1083 |
} |
| 1084 |
|
| 1085 |
function formatValue(groupByField, val) { |
| 1086 |
if (val === false || val === undefined) { |
| 1087 |
return false; |
| 1088 |
} |
| 1089 |
const [fieldName, aggregateFunction] = groupByField.split(":"); |
| 1090 |
const { type } = fields[fieldName]; |
| 1091 |
if (["date", "datetime"].includes(type)) { |
| 1092 |
const date = type === "date" ? deserializeDate(val) : deserializeDateTime(val); |
| 1093 |
let format = null; |
| 1094 |
switch (aggregateFunction) { |
| 1095 |
|
| 1096 |
|
| 1097 |
case "second_number": |
| 1098 |
return date.second; |
| 1099 |
case "minute_number": |
| 1100 |
return date.minute; |
| 1101 |
case "hour_number": |
| 1102 |
return date.hour; |
| 1103 |
case "day_of_week": |
| 1104 |
return date.weekday % 7; |
| 1105 |
case "day_of_month": |
| 1106 |
return date.day; |
| 1107 |
case "day_of_year": |
| 1108 |
return date.ordinal; |
| 1109 |
case "iso_week_number": |
| 1110 |
return date.weekNumber; |
| 1111 |
case "month_number": |
| 1112 |
return date.month; |
| 1113 |
case "quarter_number": |
| 1114 |
return date.quarter; |
| 1115 |
case "year_number": |
| 1116 |
return date.year; |
| 1117 |
case "hour": |
| 1118 |
format = "HH:00 dd MMM yyyy"; |
| 1119 |
break; |
| 1120 |
case "day": |
| 1121 |
format = "yyyy-MM-dd"; |
| 1122 |
break; |
| 1123 |
case "week": |
| 1124 |
format = "'W'WW kkkk"; |
| 1125 |
break; |
| 1126 |
case "month": |
| 1127 |
format = "MMMM yyyy"; |
| 1128 |
break; |
| 1129 |
case "quarter": |
| 1130 |
format = "'Q'q yyyy"; |
| 1131 |
break; |
| 1132 |
case "year": |
| 1133 |
format = "yyyy"; |
| 1134 |
break; |
| 1135 |
default: |
| 1136 |
throw new Error(`Unknown granularity: ${aggregateFunction}`); |
| 1137 |
} |
| 1138 |
const label = date.toFormat(format); |
| 1139 |
const start_date = parseDateTime(label, { format }); |
| 1140 |
const raw_value = |
| 1141 |
type === "date" ? serializeDate(start_date) : serializeDateTime(start_date); |
| 1142 |
return [raw_value, label]; |
| 1143 |
} else { |
| 1144 |
return val; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
const groups = {}; |
| 1149 |
for (const record of records) { |
| 1150 |
const recordGroupsValues = [{}]; |
| 1151 |
for (const groupbySpec of groupby) { |
| 1152 |
const [fieldName] = String(groupbySpec).split(":"); |
| 1153 |
const value = formatValue(groupbySpec, record[fieldName]); |
| 1154 |
|
| 1155 |
if (fields[fieldName].type == "many2many" && value) { |
| 1156 |
|
| 1157 |
|
| 1158 |
for (const group of [...recordGroupsValues]) { |
| 1159 |
for (const [index, id] of Object.entries(value)) { |
| 1160 |
if (index == 0) { |
| 1161 |
group[groupbySpec] = id; |
| 1162 |
} else { |
| 1163 |
const new_group = { ...group }; |
| 1164 |
new_group[groupbySpec] = id; |
| 1165 |
recordGroupsValues.push(new_group); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
} |
| 1169 |
} else { |
| 1170 |
for (const group of recordGroupsValues) { |
| 1171 |
group[groupbySpec] = value; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
for (const group of recordGroupsValues) { |
| 1176 |
const valueKey = JSON.stringify(group); |
| 1177 |
groups[valueKey] = groups[valueKey] || []; |
| 1178 |
groups[valueKey].push(record); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
let readGroupResult = []; |
| 1183 |
for (const [groupKey, groupRecords] of Object.entries(groups)) { |
| 1184 |
|
| 1185 |
const group = { |
| 1186 |
...JSON.parse(groupKey), |
| 1187 |
__extra_domain: [], |
| 1188 |
}; |
| 1189 |
for (const groupbySpec of groupby) { |
| 1190 |
const [fieldName, granularity] = String(groupbySpec).split(":"); |
| 1191 |
const value = Number.isInteger(group[groupbySpec]) |
| 1192 |
? group[groupbySpec] |
| 1193 |
: group[groupbySpec] || false; |
| 1194 |
const { relation, type } = fields[fieldName]; |
| 1195 |
|
| 1196 |
if (relation && !Array.isArray(value)) { |
| 1197 |
const relatedRecord = this.models[relation].records.find( |
| 1198 |
({ id }) => id === value |
| 1199 |
); |
| 1200 |
if (relatedRecord) { |
| 1201 |
group[groupbySpec] = [value, relatedRecord.display_name]; |
| 1202 |
} else { |
| 1203 |
group[groupbySpec] = false; |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (["date", "datetime"].includes(type)) { |
| 1208 |
if (value) { |
| 1209 |
if (!READ_GROUP_NUMBER_GRANULARITY.includes(granularity)) { |
| 1210 |
let startDate, endDate; |
| 1211 |
startDate = |
| 1212 |
type === "date" |
| 1213 |
? deserializeDate(value[0]) |
| 1214 |
: deserializeDateTime(value[0]); |
| 1215 |
let label = value[1]; |
| 1216 |
switch (granularity) { |
| 1217 |
case "hour": { |
| 1218 |
endDate = startDate.plus({ hours: 1 }); |
| 1219 |
|
| 1220 |
|
| 1221 |
label = startDate.toFormat("HH:00 dd MMM"); |
| 1222 |
break; |
| 1223 |
} |
| 1224 |
case "day": { |
| 1225 |
endDate = startDate.plus({ days: 1 }); |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
case "week": { |
| 1229 |
endDate = startDate.plus({ weeks: 1 }); |
| 1230 |
break; |
| 1231 |
} |
| 1232 |
case "quarter": { |
| 1233 |
endDate = startDate.plus({ quarters: 1 }); |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
case "year": { |
| 1237 |
endDate = startDate.plus({ years: 1 }); |
| 1238 |
break; |
| 1239 |
} |
| 1240 |
case "month": |
| 1241 |
default: { |
| 1242 |
endDate = startDate.plus({ months: 1 }); |
| 1243 |
break; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
const serialize = type === "date" ? serializeDate : serializeDateTime; |
| 1247 |
const from = serialize(startDate); |
| 1248 |
const to = serialize(endDate); |
| 1249 |
group.__extra_domain = [ |
| 1250 |
[fieldName, ">=", from], |
| 1251 |
[fieldName, "<", to], |
| 1252 |
...group.__extra_domain, |
| 1253 |
]; |
| 1254 |
group[groupbySpec] = [from, label]; |
| 1255 |
} else { |
| 1256 |
group.__extra_domain = [ |
| 1257 |
[`${fieldName}.${granularity}`, "=", value], |
| 1258 |
...group.__extra_domain, |
| 1259 |
]; |
| 1260 |
} |
| 1261 |
} else { |
| 1262 |
group.__extra_domain = [[fieldName, "=", value], ...group.__extra_domain]; |
| 1263 |
} |
| 1264 |
} else { |
| 1265 |
group.__extra_domain = [[fieldName, "=", value], ...group.__extra_domain]; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
aggregateFields(group, groupRecords); |
| 1269 |
readGroupResult.push(group); |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
this.sortByField(readGroupResult, modelName, order || groupby.join(",")); |
| 1274 |
|
| 1275 |
|
| 1276 |
if (limit) { |
| 1277 |
readGroupResult = readGroupResult.slice(offset || 0, limit + (offset || 0)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
return readGroupResult; |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
mockUnlink(modelName, [ids]) { |
| 1289 |
ids = Array.isArray(ids) ? ids : [ids]; |
| 1290 |
this.models[modelName].records = this.models[modelName].records.filter( |
| 1291 |
(record) => !ids.includes(record.id) |
| 1292 |
); |
| 1293 |
|
| 1294 |
|
| 1295 |
for (const { fields, records } of Object.values(this.models)) { |
| 1296 |
for (const [fieldName, field] of Object.entries(fields)) { |
| 1297 |
if (field.relation === modelName) { |
| 1298 |
for (const record of records) { |
| 1299 |
if (Array.isArray(record[fieldName])) { |
| 1300 |
record[fieldName] = record[fieldName].filter((id) => !ids.includes(id)); |
| 1301 |
} else if (ids.includes(record[fieldName])) { |
| 1302 |
record[fieldName] = false; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
return true; |
| 1310 |
} |
| 1311 |
|
| 1312 |
mockReadProgressBar(modelName, kwargs) { |
| 1313 |
const { domain, group_by: groupBy, progress_bar: progressBar } = kwargs; |
| 1314 |
const groups = this.mockFormattedReadGroup(modelName, { |
| 1315 |
domain, |
| 1316 |
groupby: [groupBy, progressBar.field], |
| 1317 |
aggregates: ["__count"], |
| 1318 |
}); |
| 1319 |
|
| 1320 |
const data = {}; |
| 1321 |
for (const group of groups) { |
| 1322 |
let groupByValue = group[groupBy]; |
| 1323 |
if (Array.isArray(groupByValue)) { |
| 1324 |
groupByValue = groupByValue[0]; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
if (!(groupByValue in data)) { |
| 1329 |
if (groupByValue === true) { |
| 1330 |
groupByValue = "True"; |
| 1331 |
} else if (groupByValue === false) { |
| 1332 |
groupByValue = "False"; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (!(groupByValue in data)) { |
| 1337 |
data[groupByValue] = {}; |
| 1338 |
for (const key in progressBar.colors) { |
| 1339 |
data[groupByValue][key] = 0; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
data[groupByValue][group[progressBar.field]] += group.__count; |
| 1343 |
} |
| 1344 |
return data; |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
mockSearchPanelFieldImage(model, fieldName, kwargs) { |
| 1357 |
const enableCounters = kwargs.enable_counters; |
| 1358 |
const onlyCounters = kwargs.only_counters; |
| 1359 |
const extraDomain = kwargs.extra_domain || []; |
| 1360 |
const normalizedExtra = new Domain(extraDomain).toList(); |
| 1361 |
const noExtra = JSON.stringify(normalizedExtra) === "[]"; |
| 1362 |
const modelDomain = kwargs.model_domain || []; |
| 1363 |
const countDomain = new Domain([...modelDomain, ...extraDomain]).toList(); |
| 1364 |
|
| 1365 |
const limit = kwargs.limit; |
| 1366 |
const setLimit = kwargs.set_limit; |
| 1367 |
|
| 1368 |
if (onlyCounters) { |
| 1369 |
return this.mockSearchPanelDomainImage(model, fieldName, countDomain, true); |
| 1370 |
} |
| 1371 |
|
| 1372 |
const modelDomainImage = this.mockSearchPanelDomainImage( |
| 1373 |
model, |
| 1374 |
fieldName, |
| 1375 |
modelDomain, |
| 1376 |
enableCounters && noExtra, |
| 1377 |
setLimit && limit |
| 1378 |
); |
| 1379 |
if (enableCounters && !noExtra) { |
| 1380 |
const countDomainImage = this.mockSearchPanelDomainImage( |
| 1381 |
model, |
| 1382 |
fieldName, |
| 1383 |
countDomain, |
| 1384 |
true |
| 1385 |
); |
| 1386 |
for (const [id, values] of modelDomainImage.entries()) { |
| 1387 |
const element = countDomainImage.get(id); |
| 1388 |
values.__count = element ? element.__count : 0; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
return modelDomainImage; |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
mockSearchPanelDomainImage(model, fieldName, domain, setCount = false, limit = false) { |
| 1406 |
const field = this.models[model].fields[fieldName]; |
| 1407 |
let groupIdName; |
| 1408 |
if (field.type === "many2one") { |
| 1409 |
groupIdName = (value) => value || [false, undefined]; |
| 1410 |
|
| 1411 |
|
| 1412 |
} else if (field.type === "selection") { |
| 1413 |
const selection = {}; |
| 1414 |
for (const [value, label] of this.models[model].fields[fieldName].selection) { |
| 1415 |
selection[value] = label; |
| 1416 |
} |
| 1417 |
groupIdName = (value) => [value, selection[value]]; |
| 1418 |
} |
| 1419 |
domain = new Domain([...domain, [fieldName, "!=", false]]).toList(); |
| 1420 |
const groups = this.mockFormattedReadGroup(model, { |
| 1421 |
domain, |
| 1422 |
aggregates: ["__count"], |
| 1423 |
groupby: [fieldName], |
| 1424 |
limit, |
| 1425 |
}); |
| 1426 |
const domainImage = new Map(); |
| 1427 |
for (const group of groups) { |
| 1428 |
const [id, display_name] = groupIdName(group[fieldName]); |
| 1429 |
const values = { id, display_name }; |
| 1430 |
if (setCount) { |
| 1431 |
values.__count = group.__count; |
| 1432 |
} |
| 1433 |
domainImage.set(id, values); |
| 1434 |
} |
| 1435 |
return domainImage; |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
mockSearchPanelGlobalCounters(valuesRange, parentName) { |
| 1445 |
const localCounters = [...valuesRange.keys()].map((id) => valuesRange.get(id).__count); |
| 1446 |
for (let [id, values] of valuesRange.entries()) { |
| 1447 |
const count = localCounters[id]; |
| 1448 |
if (count) { |
| 1449 |
let parent_id = values[parentName]; |
| 1450 |
while (parent_id) { |
| 1451 |
values = valuesRange.get(parent_id); |
| 1452 |
values.__count += count; |
| 1453 |
parent_id = values[parentName]; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
mockSearchPanelSanitizedParentHierarchy(records, parentName, ids) { |
| 1468 |
const getParentId = (record) => record[parentName] && record[parentName][0]; |
| 1469 |
const allowedRecords = {}; |
| 1470 |
for (const record of records) { |
| 1471 |
allowedRecords[record.id] = record; |
| 1472 |
} |
| 1473 |
const recordsToKeep = {}; |
| 1474 |
for (const id of ids) { |
| 1475 |
const ancestorChain = {}; |
| 1476 |
let recordId = id; |
| 1477 |
let chainIsFullyIncluded = true; |
| 1478 |
while (chainIsFullyIncluded && recordId) { |
| 1479 |
const knownStatus = recordsToKeep[recordId]; |
| 1480 |
if (knownStatus !== undefined) { |
| 1481 |
chainIsFullyIncluded = knownStatus; |
| 1482 |
break; |
| 1483 |
} |
| 1484 |
const record = allowedRecords[recordId]; |
| 1485 |
if (record) { |
| 1486 |
ancestorChain[recordId] = record; |
| 1487 |
recordId = getParentId(record); |
| 1488 |
} else { |
| 1489 |
chainIsFullyIncluded = false; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
for (const id in ancestorChain) { |
| 1493 |
recordsToKeep[id] = chainIsFullyIncluded; |
| 1494 |
} |
| 1495 |
} |
| 1496 |
return records.filter((rec) => recordsToKeep[rec.id]); |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
mockSearchPanelSelectionRange(model, fieldName, kwargs) { |
| 1508 |
const enableCounters = kwargs.enable_counters; |
| 1509 |
const expand = kwargs.expand; |
| 1510 |
let domainImage; |
| 1511 |
if (enableCounters || !expand) { |
| 1512 |
const newKwargs = Object.assign({}, kwargs, { |
| 1513 |
only_counters: expand, |
| 1514 |
}); |
| 1515 |
domainImage = this.mockSearchPanelFieldImage(model, fieldName, newKwargs); |
| 1516 |
} |
| 1517 |
if (!expand) { |
| 1518 |
return [...domainImage.values()]; |
| 1519 |
} |
| 1520 |
const selection = this.models[model].fields[fieldName].selection; |
| 1521 |
const selectionRange = []; |
| 1522 |
for (const [value, label] of selection) { |
| 1523 |
const values = { |
| 1524 |
id: value, |
| 1525 |
display_name: label, |
| 1526 |
}; |
| 1527 |
if (enableCounters) { |
| 1528 |
values.__count = domainImage.get(value) ? domainImage.get(value).__count : 0; |
| 1529 |
} |
| 1530 |
selectionRange.push(values); |
| 1531 |
} |
| 1532 |
return selectionRange; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
mockSearchPanelSelectRange(model, [fieldName], kwargs) { |
| 1555 |
const field = this.models[model].fields[fieldName]; |
| 1556 |
const supportedTypes = ["many2one", "selection"]; |
| 1557 |
if (!supportedTypes.includes(field.type)) { |
| 1558 |
throw new Error( |
| 1559 |
`Only types ${supportedTypes} are supported for category (found type ${field.type})` |
| 1560 |
); |
| 1561 |
} |
| 1562 |
|
| 1563 |
const modelDomain = kwargs.search_domain || []; |
| 1564 |
const extraDomain = new Domain([ |
| 1565 |
...(kwargs.category_domain || []), |
| 1566 |
...(kwargs.filter_domain || []), |
| 1567 |
]).toList(); |
| 1568 |
|
| 1569 |
if (field.type === "selection") { |
| 1570 |
const newKwargs = Object.assign({}, kwargs, { |
| 1571 |
model_domain: modelDomain, |
| 1572 |
extra_domain: extraDomain, |
| 1573 |
}); |
| 1574 |
kwargs.model_domain = modelDomain; |
| 1575 |
return { |
| 1576 |
parent_field: false, |
| 1577 |
values: this.mockSearchPanelSelectionRange(model, fieldName, newKwargs), |
| 1578 |
}; |
| 1579 |
} |
| 1580 |
|
| 1581 |
const fieldNames = ["display_name"]; |
| 1582 |
let hierarchize = "hierarchize" in kwargs ? kwargs.hierarchize : true; |
| 1583 |
let getParentId; |
| 1584 |
let parentName = false; |
| 1585 |
if (hierarchize && this.models[field.relation].fields.parent_id) { |
| 1586 |
parentName = "parent_id"; |
| 1587 |
fieldNames.push(parentName); |
| 1588 |
getParentId = (record) => record.parent_id && record.parent_id[0]; |
| 1589 |
} else { |
| 1590 |
hierarchize = false; |
| 1591 |
} |
| 1592 |
let comodelDomain = kwargs.comodel_domain || []; |
| 1593 |
const enableCounters = kwargs.enable_counters; |
| 1594 |
const expand = kwargs.expand; |
| 1595 |
const limit = kwargs.limit; |
| 1596 |
let domainImage; |
| 1597 |
if (enableCounters || !expand) { |
| 1598 |
const newKwargs = Object.assign({}, kwargs, { |
| 1599 |
model_domain: modelDomain, |
| 1600 |
extra_domain: extraDomain, |
| 1601 |
only_counters: expand, |
| 1602 |
set_limit: limit && !(expand || hierarchize || comodelDomain), |
| 1603 |
}); |
| 1604 |
domainImage = this.mockSearchPanelFieldImage(model, fieldName, newKwargs); |
| 1605 |
} |
| 1606 |
if (!expand && !hierarchize && !comodelDomain.length) { |
| 1607 |
if (limit && domainImage.size === limit) { |
| 1608 |
return { error_msg: "Too many items to display." }; |
| 1609 |
} |
| 1610 |
return { |
| 1611 |
parent_field: parentName, |
| 1612 |
values: [...domainImage.values()], |
| 1613 |
}; |
| 1614 |
} |
| 1615 |
let imageElementIds; |
| 1616 |
if (!expand) { |
| 1617 |
imageElementIds = [...domainImage.keys()].map(Number); |
| 1618 |
let condition; |
| 1619 |
if (hierarchize) { |
| 1620 |
const records = this.models[field.relation].records; |
| 1621 |
const ancestorIds = new Set(); |
| 1622 |
for (const id of imageElementIds) { |
| 1623 |
let recordId = id; |
| 1624 |
let record; |
| 1625 |
while (recordId) { |
| 1626 |
ancestorIds.add(recordId); |
| 1627 |
record = records.find((rec) => rec.id === recordId); |
| 1628 |
recordId = record[parentName]; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
condition = ["id", "in", [...new Set(ancestorIds)]]; |
| 1632 |
} else { |
| 1633 |
condition = ["id", "in", imageElementIds]; |
| 1634 |
} |
| 1635 |
comodelDomain = new Domain([...comodelDomain, condition]).toList(); |
| 1636 |
} |
| 1637 |
let comodelRecords = this.mockSearchRead(field.relation, [comodelDomain, fieldNames], { |
| 1638 |
limit, |
| 1639 |
}); |
| 1640 |
|
| 1641 |
if (hierarchize) { |
| 1642 |
const ids = expand ? comodelRecords.map((rec) => rec.id) : imageElementIds; |
| 1643 |
comodelRecords = this.mockSearchPanelSanitizedParentHierarchy( |
| 1644 |
comodelRecords, |
| 1645 |
parentName, |
| 1646 |
ids |
| 1647 |
); |
| 1648 |
} |
| 1649 |
|
| 1650 |
if (limit && comodelRecords.length === limit) { |
| 1651 |
return { error_msg: "Too many items to display." }; |
| 1652 |
} |
| 1653 |
|
| 1654 |
const fieldRange = new Map(); |
| 1655 |
for (const record of comodelRecords) { |
| 1656 |
const values = { |
| 1657 |
id: record.id, |
| 1658 |
display_name: record.display_name, |
| 1659 |
}; |
| 1660 |
if (hierarchize) { |
| 1661 |
values[parentName] = getParentId(record); |
| 1662 |
} |
| 1663 |
if (enableCounters) { |
| 1664 |
values.__count = domainImage.get(record.id) |
| 1665 |
? domainImage.get(record.id).__count |
| 1666 |
: 0; |
| 1667 |
} |
| 1668 |
fieldRange.set(record.id, values); |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (hierarchize && enableCounters) { |
| 1672 |
this.mockSearchPanelGlobalCounters(fieldRange, parentName); |
| 1673 |
} |
| 1674 |
|
| 1675 |
return { |
| 1676 |
parent_field: parentName, |
| 1677 |
values: [...fieldRange.values()], |
| 1678 |
}; |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
mockSearchPanelSelectMultiRange(model, [fieldName], kwargs) { |
| 1704 |
const field = this.models[model].fields[fieldName]; |
| 1705 |
const supportedTypes = ["many2one", "many2many", "selection"]; |
| 1706 |
if (!supportedTypes.includes(field.type)) { |
| 1707 |
throw new Error( |
| 1708 |
`Only types ${supportedTypes} are supported for filter (found type ${field.type})` |
| 1709 |
); |
| 1710 |
} |
| 1711 |
let modelDomain = kwargs.search_domain || []; |
| 1712 |
let extraDomain = new Domain([ |
| 1713 |
...(kwargs.category_domain || []), |
| 1714 |
...(kwargs.filter_domain || []), |
| 1715 |
]).toList(); |
| 1716 |
if (field.type === "selection") { |
| 1717 |
const newKwargs = Object.assign({}, kwargs, { |
| 1718 |
model_domain: modelDomain, |
| 1719 |
extra_domain: extraDomain, |
| 1720 |
}); |
| 1721 |
return { |
| 1722 |
values: this.mockSearchPanelSelectionRange(model, fieldName, newKwargs), |
| 1723 |
}; |
| 1724 |
} |
| 1725 |
const fieldNames = ["display_name"]; |
| 1726 |
const groupBy = kwargs.group_by; |
| 1727 |
let groupIdName; |
| 1728 |
if (groupBy) { |
| 1729 |
const groupByField = this.models[field.relation].fields[groupBy]; |
| 1730 |
fieldNames.push(groupBy); |
| 1731 |
if (groupByField.type === "many2one") { |
| 1732 |
groupIdName = (value) => value || [false, "Not set"]; |
| 1733 |
} else if (groupByField.type === "selection") { |
| 1734 |
const groupBySelection = Object.assign( |
| 1735 |
{}, |
| 1736 |
this.models[field.relation].fields[groupBy].selection |
| 1737 |
); |
| 1738 |
groupBySelection[false] = "Not Set"; |
| 1739 |
groupIdName = (value) => [value, groupBySelection[value]]; |
| 1740 |
} else { |
| 1741 |
groupIdName = (value) => (value ? [value, value] : [false, "Not set"]); |
| 1742 |
} |
| 1743 |
} |
| 1744 |
let comodelDomain = kwargs.comodel_domain || []; |
| 1745 |
const enableCounters = kwargs.enable_counters; |
| 1746 |
const expand = kwargs.expand; |
| 1747 |
const limit = kwargs.limit; |
| 1748 |
if (field.type === "many2many") { |
| 1749 |
const comodelRecords = this.mockSearchRead( |
| 1750 |
field.relation, |
| 1751 |
[comodelDomain, fieldNames], |
| 1752 |
{ |
| 1753 |
limit, |
| 1754 |
} |
| 1755 |
); |
| 1756 |
if (expand && limit && comodelRecords.length === limit) { |
| 1757 |
return { error_msg: "Too many items to display." }; |
| 1758 |
} |
| 1759 |
|
| 1760 |
const groupDomain = kwargs.group_domain; |
| 1761 |
const fieldRange = []; |
| 1762 |
for (const record of comodelRecords) { |
| 1763 |
const values = { |
| 1764 |
id: record.id, |
| 1765 |
display_name: record.display_name, |
| 1766 |
}; |
| 1767 |
let groupId; |
| 1768 |
if (groupBy) { |
| 1769 |
const [gId, gName] = groupIdName(record[groupBy]); |
| 1770 |
values.group_id = groupId = gId; |
| 1771 |
values.group_name = gName; |
| 1772 |
} |
| 1773 |
let count; |
| 1774 |
let inImage; |
| 1775 |
if (enableCounters || !expand) { |
| 1776 |
const searchDomain = new Domain([ |
| 1777 |
...modelDomain, |
| 1778 |
[fieldName, "in", record.id], |
| 1779 |
]).toList(); |
| 1780 |
let localExtraDomain = extraDomain; |
| 1781 |
if (groupBy && groupDomain) { |
| 1782 |
localExtraDomain = new Domain([ |
| 1783 |
...localExtraDomain, |
| 1784 |
...(groupDomain[JSON.stringify(groupId)] || []), |
| 1785 |
]).toList(); |
| 1786 |
} |
| 1787 |
const searchCountDomain = new Domain([ |
| 1788 |
...searchDomain, |
| 1789 |
...localExtraDomain, |
| 1790 |
]).toList(); |
| 1791 |
if (enableCounters) { |
| 1792 |
count = this.mockSearchCount(model, [searchCountDomain]); |
| 1793 |
} |
| 1794 |
if (!expand) { |
| 1795 |
if (enableCounters && JSON.stringify(localExtraDomain) === "[]") { |
| 1796 |
inImage = count; |
| 1797 |
} else { |
| 1798 |
inImage = this.mockSearch(model, [searchDomain], { limit: 1 }).length; |
| 1799 |
} |
| 1800 |
} |
| 1801 |
} |
| 1802 |
if (expand || inImage) { |
| 1803 |
if (enableCounters) { |
| 1804 |
values.__count = count; |
| 1805 |
} |
| 1806 |
fieldRange.push(values); |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
if (!expand && limit && fieldRange.length === limit) { |
| 1811 |
return { error_msg: "Too many items to display." }; |
| 1812 |
} |
| 1813 |
|
| 1814 |
return { values: fieldRange }; |
| 1815 |
} |
| 1816 |
|
| 1817 |
if (field.type === "many2one") { |
| 1818 |
let domainImage; |
| 1819 |
if (enableCounters || !expand) { |
| 1820 |
extraDomain = new Domain([...extraDomain, ...(kwargs.group_domain || [])]).toList(); |
| 1821 |
modelDomain = new Domain([...modelDomain, ...(kwargs.group_domain || [])]).toList(); |
| 1822 |
const newKwargs = Object.assign({}, kwargs, { |
| 1823 |
model_domain: modelDomain, |
| 1824 |
extra_domain: extraDomain, |
| 1825 |
only_counters: expand, |
| 1826 |
set_limit: limit && !(expand || groupBy || comodelDomain), |
| 1827 |
}); |
| 1828 |
domainImage = this.mockSearchPanelFieldImage(model, fieldName, newKwargs); |
| 1829 |
} |
| 1830 |
if (!expand && !groupBy && !comodelDomain.length) { |
| 1831 |
if (limit && domainImage.size === limit) { |
| 1832 |
return { error_msg: "Too many items to display." }; |
| 1833 |
} |
| 1834 |
return { values: [...domainImage.values()] }; |
| 1835 |
} |
| 1836 |
if (!expand) { |
| 1837 |
const imageElementIds = [...domainImage.keys()].map(Number); |
| 1838 |
comodelDomain = new Domain([ |
| 1839 |
...comodelDomain, |
| 1840 |
["id", "in", imageElementIds], |
| 1841 |
]).toList(); |
| 1842 |
} |
| 1843 |
const comodelRecords = this.mockSearchRead( |
| 1844 |
field.relation, |
| 1845 |
[comodelDomain, fieldNames], |
| 1846 |
{ |
| 1847 |
limit, |
| 1848 |
} |
| 1849 |
); |
| 1850 |
if (limit && comodelRecords.length === limit) { |
| 1851 |
return { error_msg: "Too many items to display." }; |
| 1852 |
} |
| 1853 |
|
| 1854 |
const fieldRange = []; |
| 1855 |
for (const record of comodelRecords) { |
| 1856 |
const values = { |
| 1857 |
id: record.id, |
| 1858 |
display_name: record.display_name, |
| 1859 |
}; |
| 1860 |
if (groupBy) { |
| 1861 |
const [groupId, groupName] = groupIdName(record[groupBy]); |
| 1862 |
values.group_id = groupId; |
| 1863 |
values.group_name = groupName; |
| 1864 |
} |
| 1865 |
if (enableCounters) { |
| 1866 |
values.__count = domainImage.get(record.id) |
| 1867 |
? domainImage.get(record.id).__count |
| 1868 |
: 0; |
| 1869 |
} |
| 1870 |
fieldRange.push(values); |
| 1871 |
} |
| 1872 |
return { values: fieldRange }; |
| 1873 |
} |
| 1874 |
} |
| 1875 |
|
| 1876 |
mockSearch(modelName, args, kwargs) { |
| 1877 |
const result = this.mockSearchController({ |
| 1878 |
model: modelName, |
| 1879 |
domain: kwargs.domain || args[0], |
| 1880 |
fields: kwargs.fields || args[1], |
| 1881 |
offset: kwargs.offset || args[2], |
| 1882 |
limit: kwargs.limit || args[3], |
| 1883 |
sort: kwargs.order || args[4], |
| 1884 |
context: kwargs.context, |
| 1885 |
}); |
| 1886 |
return result.records.map((r) => r.id); |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
mockSearchCount(model, args, kwargs = {}) { |
| 1900 |
return this.getRecords(model, args[0], kwargs.context).length; |
| 1901 |
} |
| 1902 |
|
| 1903 |
mockSearchRead(modelName, args, kwargs) { |
| 1904 |
const { fieldNames, records } = this.mockSearchController({ |
| 1905 |
model: modelName, |
| 1906 |
domain: kwargs.domain || args[0], |
| 1907 |
fields: kwargs.fields || args[1], |
| 1908 |
offset: kwargs.offset || args[2], |
| 1909 |
limit: kwargs.limit || args[3], |
| 1910 |
sort: kwargs.order || args[4], |
| 1911 |
context: kwargs.context, |
| 1912 |
}); |
| 1913 |
return this.mockRead(modelName, [records.map((r) => r.id), fieldNames]); |
| 1914 |
} |
| 1915 |
|
| 1916 |
mockWebNameSearch(modelName, args, kwargs) { |
| 1917 |
const idNamePairs = this.mockNameSearch(modelName, args, kwargs); |
| 1918 |
if ( |
| 1919 |
Object.keys(kwargs.specification).length === 1 && |
| 1920 |
"display_name" in kwargs.specification |
| 1921 |
) { |
| 1922 |
return idNamePairs.map(([id, name]) => ({ id, display_name: name, __formatted_display_name: name })); |
| 1923 |
} |
| 1924 |
const ids = idNamePairs.map(([id]) => id); |
| 1925 |
return this.mockWebRead(modelName, [ids], kwargs); |
| 1926 |
} |
| 1927 |
|
| 1928 |
mockWebSave(modelName, args, kwargs) { |
| 1929 |
const ids = args[0]; |
| 1930 |
if (ids.length === 0) { |
| 1931 |
args[0] = this.mockCreate(modelName, args[1], kwargs); |
| 1932 |
} else { |
| 1933 |
this.mockWrite(modelName, args); |
| 1934 |
} |
| 1935 |
if (kwargs.next_id) { |
| 1936 |
args[0] = kwargs.next_id; |
| 1937 |
} |
| 1938 |
return this.mockWebRead(modelName, args, kwargs); |
| 1939 |
} |
| 1940 |
|
| 1941 |
mockWebRead(modelName, args, kwargs) { |
| 1942 |
const ids = args[0]; |
| 1943 |
let fieldNames = Object.keys(kwargs.specification); |
| 1944 |
if (!fieldNames.length) { |
| 1945 |
fieldNames = ["id"]; |
| 1946 |
} |
| 1947 |
const records = this.mockRead(modelName, [ids, fieldNames], { |
| 1948 |
context: kwargs.context, |
| 1949 |
}); |
| 1950 |
this._unityReadRecords(modelName, kwargs.specification, records); |
| 1951 |
return records; |
| 1952 |
} |
| 1953 |
|
| 1954 |
mockWebSearchReadUnity(modelName, args, kwargs) { |
| 1955 |
let _fieldNames = Object.keys(kwargs.specification); |
| 1956 |
if (!_fieldNames.length) { |
| 1957 |
_fieldNames = ["id"]; |
| 1958 |
} |
| 1959 |
const { fieldNames, length, records } = this.mockSearchController({ |
| 1960 |
model: modelName, |
| 1961 |
fields: _fieldNames, |
| 1962 |
domain: kwargs.domain, |
| 1963 |
offset: kwargs.offset, |
| 1964 |
limit: kwargs.limit, |
| 1965 |
sort: kwargs.order, |
| 1966 |
context: kwargs.context, |
| 1967 |
}); |
| 1968 |
const result = { |
| 1969 |
length, |
| 1970 |
records: this.mockRead(modelName, [records.map((r) => r.id), fieldNames]), |
| 1971 |
}; |
| 1972 |
const countLimit = kwargs.count_limit || args[5]; |
| 1973 |
if (countLimit) { |
| 1974 |
result.length = Math.min(result.length, countLimit); |
| 1975 |
} |
| 1976 |
this._unityReadRecords(modelName, kwargs.specification, result.records); |
| 1977 |
return result; |
| 1978 |
} |
| 1979 |
|
| 1980 |
mockSearchController(params) { |
| 1981 |
const model = this.models[params.model]; |
| 1982 |
let fieldNames = params.fields; |
| 1983 |
const offset = params.offset || 0; |
| 1984 |
if (!fieldNames || !fieldNames.length) { |
| 1985 |
fieldNames = Object.keys(model.fields); |
| 1986 |
} |
| 1987 |
fieldNames = [...new Set(fieldNames.concat(["id"]))]; |
| 1988 |
const { context } = params; |
| 1989 |
const active_test = context && "active_test" in context ? context.active_test : true; |
| 1990 |
let records = this.getRecords( |
| 1991 |
params.model, |
| 1992 |
params.domain || [], |
| 1993 |
Object.assign({}, params.context, { active_test }) |
| 1994 |
); |
| 1995 |
this.sortByField(records, params.model, params.sort); |
| 1996 |
const nbRecords = records.length; |
| 1997 |
records = records.slice(offset, params.limit ? offset + params.limit : nbRecords); |
| 1998 |
return { |
| 1999 |
fieldNames, |
| 2000 |
length: nbRecords, |
| 2001 |
records, |
| 2002 |
}; |
| 2003 |
} |
| 2004 |
|
| 2005 |
mockWrite(modelName, args) { |
| 2006 |
args[0].forEach((id) => { |
| 2007 |
const [originalRecord] = this.mockSearchRead(modelName, [[["id", "=", id]]], {}); |
| 2008 |
this.writeRecord(modelName, args[1], id); |
| 2009 |
const updatedRecord = this.models[modelName].records.find((record) => record.id === id); |
| 2010 |
this.updateComodelRelationalFields(modelName, updatedRecord, originalRecord); |
| 2011 |
}); |
| 2012 |
return true; |
| 2013 |
} |
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
updateComodelRelationalFields(modelName, record, originalRecord) { |
| 2028 |
for (const fname in record) { |
| 2029 |
const field = this.models[modelName].fields[fname]; |
| 2030 |
const comodelName = field.relation || record[field.model_name_ref_fname]; |
| 2031 |
const inverseFieldName = |
| 2032 |
field.inverse_fname_by_model_name && field.inverse_fname_by_model_name[comodelName]; |
| 2033 |
if (!inverseFieldName) { |
| 2034 |
|
| 2035 |
continue; |
| 2036 |
} |
| 2037 |
const relatedRecordIds = Array.isArray(record[fname]) ? record[fname] : [record[fname]]; |
| 2038 |
const comodel_inverse_field = this.models[comodelName].fields[inverseFieldName]; |
| 2039 |
|
| 2040 |
if (record[fname]) { |
| 2041 |
for (const relatedRecordId of relatedRecordIds) { |
| 2042 |
let inverseFieldNewValue = record.id; |
| 2043 |
const relatedRecord = this.models[comodelName].records.find( |
| 2044 |
(record) => record.id === relatedRecordId |
| 2045 |
); |
| 2046 |
const relatedFieldValue = relatedRecord && relatedRecord[inverseFieldName]; |
| 2047 |
if ( |
| 2048 |
relatedFieldValue === undefined || |
| 2049 |
relatedFieldValue === record.id || |
| 2050 |
(field.type !== "one2many" && relatedFieldValue.includes(record.id)) |
| 2051 |
) { |
| 2052 |
|
| 2053 |
continue; |
| 2054 |
} |
| 2055 |
if (Array.isArray(relatedFieldValue)) { |
| 2056 |
inverseFieldNewValue = [...relatedFieldValue, record.id]; |
| 2057 |
} |
| 2058 |
const data = { [inverseFieldName]: inverseFieldNewValue }; |
| 2059 |
if (comodel_inverse_field.type === "many2one_reference") { |
| 2060 |
data[comodel_inverse_field.model_name_ref_fname] = modelName; |
| 2061 |
} |
| 2062 |
this.writeRecord(comodelName, data, relatedRecordId); |
| 2063 |
} |
| 2064 |
} else if (field.type === "many2one_reference") { |
| 2065 |
|
| 2066 |
const model_many2one_field = |
| 2067 |
comodel_inverse_field.inverse_fname_by_model_name[modelName]; |
| 2068 |
this.writeRecord(modelName, { [model_many2one_field]: false }, record.id); |
| 2069 |
} |
| 2070 |
|
| 2071 |
|
| 2072 |
if (originalRecord) { |
| 2073 |
const originalRecordIds = Array.isArray(originalRecord[fname]) |
| 2074 |
? originalRecord[fname] |
| 2075 |
: [originalRecord[fname]]; |
| 2076 |
|
| 2077 |
const removedRecordIds = originalRecordIds.filter( |
| 2078 |
(recordId) => Number.isInteger(recordId) && !relatedRecordIds.includes(recordId) |
| 2079 |
); |
| 2080 |
for (const removedRecordId of removedRecordIds) { |
| 2081 |
const removedRecord = this.models[comodelName].records.find( |
| 2082 |
(record) => record.id === removedRecordId |
| 2083 |
); |
| 2084 |
if (!removedRecord) { |
| 2085 |
continue; |
| 2086 |
} |
| 2087 |
let inverseFieldNewValue = false; |
| 2088 |
if (Array.isArray(removedRecord[inverseFieldName])) { |
| 2089 |
inverseFieldNewValue = removedRecord[inverseFieldName].filter( |
| 2090 |
(id) => id !== record.id |
| 2091 |
); |
| 2092 |
} |
| 2093 |
this.writeRecord( |
| 2094 |
comodelName, |
| 2095 |
{ |
| 2096 |
[inverseFieldName]: inverseFieldNewValue.length |
| 2097 |
? inverseFieldNewValue |
| 2098 |
: false, |
| 2099 |
}, |
| 2100 |
removedRecordId |
| 2101 |
); |
| 2102 |
} |
| 2103 |
} |
| 2104 |
} |
| 2105 |
} |
| 2106 |
evaluateDomain(domain, record) { |
| 2107 |
return new Domain(domain).contains(record); |
| 2108 |
} |
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
getOrderByField(modelName, fieldNameSpec) { |
| 2121 |
const { fields } = this.models[modelName]; |
| 2122 |
const fieldName = |
| 2123 |
fieldNameSpec?.split(":")[0] || ("sequence" in fields ? "sequence" : "id"); |
| 2124 |
if (!(fieldName in fields)) { |
| 2125 |
throw new Error( |
| 2126 |
`Mock: cannot sort records of model "${modelName}" by field "${fieldName}": field not found` |
| 2127 |
); |
| 2128 |
} |
| 2129 |
return fields[fieldName]; |
| 2130 |
} |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
getDateSortingValue(record, fieldName) { |
| 2142 |
|
| 2143 |
const values = []; |
| 2144 |
for (const groupedBy of Object.keys(record.__range)) { |
| 2145 |
if (groupedBy.startsWith(fieldName)) { |
| 2146 |
values.push(record.__range[groupedBy].from); |
| 2147 |
} |
| 2148 |
} |
| 2149 |
|
| 2150 |
|
| 2151 |
return !values.length || values.includes(false) |
| 2152 |
? false |
| 2153 |
: values.reduce((max, value) => (value > max ? value : max)); |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
getRecords(modelName, domain, { active_test = true } = {}) { |
| 2162 |
if (!Array.isArray(domain)) { |
| 2163 |
throw new Error("MockServer._getRecords: given domain has to be an array."); |
| 2164 |
} |
| 2165 |
const model = this.models[modelName]; |
| 2166 |
|
| 2167 |
if (active_test && "active" in model.fields) { |
| 2168 |
const activeInDomain = domain.some((subDomain) => subDomain[0] === "active"); |
| 2169 |
if (!activeInDomain) { |
| 2170 |
domain = domain.concat([["active", "=", true]]); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
let records = model.records; |
| 2174 |
if (domain.length) { |
| 2175 |
domain = domain.map((criterion) => { |
| 2176 |
|
| 2177 |
|
| 2178 |
if (criterion[1] === "child_of") { |
| 2179 |
let oldLength = 0; |
| 2180 |
const childIDs = [criterion[2]]; |
| 2181 |
while (childIDs.length > oldLength) { |
| 2182 |
oldLength = childIDs.length; |
| 2183 |
records.forEach((r) => { |
| 2184 |
if (childIDs.indexOf(r.parent_id) >= 0) { |
| 2185 |
childIDs.push(r.id); |
| 2186 |
} |
| 2187 |
}); |
| 2188 |
} |
| 2189 |
criterion = [criterion[0], "in", childIDs]; |
| 2190 |
} else if (criterion[1] === "parent_of") { |
| 2191 |
|
| 2192 |
|
| 2193 |
const childID = criterion[2]; |
| 2194 |
const parentIDs = []; |
| 2195 |
const recordPerID = {}; |
| 2196 |
for (const record of records) { |
| 2197 |
recordPerID[record.id] = record; |
| 2198 |
} |
| 2199 |
let record = recordPerID[childID]; |
| 2200 |
while (record) { |
| 2201 |
parentIDs.push(record.id); |
| 2202 |
record = record.parent_id && recordPerID[record.parent_id]; |
| 2203 |
} |
| 2204 |
criterion = [criterion[0], "in", parentIDs]; |
| 2205 |
} |
| 2206 |
|
| 2207 |
const field = model.fields[criterion[0]] || {}; |
| 2208 |
if (field.type === "many2many" && criterion[1] === "=") { |
| 2209 |
if (criterion[2] === false) { |
| 2210 |
|
| 2211 |
criterion = [criterion[0], "=", []]; |
| 2212 |
} else { |
| 2213 |
criterion = [criterion[0], "in", [criterion[2]]]; |
| 2214 |
} |
| 2215 |
} |
| 2216 |
return criterion; |
| 2217 |
}); |
| 2218 |
records = records.filter((record) => this.evaluateDomain(domain, record)); |
| 2219 |
} |
| 2220 |
return records; |
| 2221 |
} |
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
sortByField(records, modelName, orderBy = "") { |
| 2235 |
const orderBys = orderBy.split(","); |
| 2236 |
const [fieldNameSpec, order] = orderBys.pop().split(" "); |
| 2237 |
const field = this.getOrderByField(modelName, fieldNameSpec); |
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
let valuesMap; |
| 2242 |
switch (field.type) { |
| 2243 |
case "many2many": |
| 2244 |
case "many2one": { |
| 2245 |
const coRecords = this.models[field.relation].records; |
| 2246 |
const coField = this.getOrderByField(field.relation); |
| 2247 |
if (field.type === "many2many") { |
| 2248 |
|
| 2249 |
|
| 2250 |
this.sortByField(coRecords, field.relation); |
| 2251 |
} |
| 2252 |
valuesMap = new Map(coRecords.map((r) => [r.id, r[coField.name]])); |
| 2253 |
break; |
| 2254 |
} |
| 2255 |
case "selection": { |
| 2256 |
|
| 2257 |
valuesMap = new Map(field.selection.map((v, i) => [v[0], i])); |
| 2258 |
break; |
| 2259 |
} |
| 2260 |
} |
| 2261 |
|
| 2262 |
|
| 2263 |
const sortedRecords = records.sort((r1, r2) => { |
| 2264 |
let v1 = r1[fieldNameSpec]; |
| 2265 |
let v2 = r2[fieldNameSpec]; |
| 2266 |
switch (field.type) { |
| 2267 |
case "many2one": { |
| 2268 |
if (v1) { |
| 2269 |
v1 = valuesMap.get(v1[0]); |
| 2270 |
} |
| 2271 |
if (v2) { |
| 2272 |
v2 = valuesMap.get(v2[0]); |
| 2273 |
} |
| 2274 |
break; |
| 2275 |
} |
| 2276 |
case "many2many": { |
| 2277 |
|
| 2278 |
|
| 2279 |
if (v1) { |
| 2280 |
v1 = v1.map((id) => valuesMap.get(id)).join(""); |
| 2281 |
} |
| 2282 |
if (v2) { |
| 2283 |
v2 = v2.map((id) => valuesMap.get(id)).join(""); |
| 2284 |
} |
| 2285 |
break; |
| 2286 |
} |
| 2287 |
case "date": |
| 2288 |
case "datetime": { |
| 2289 |
v1 = Array.isArray(v1) ? new Date(v1[0]).getTime() : v1; |
| 2290 |
v2 = Array.isArray(v2) ? new Date(v2[0]).getTime() : v2; |
| 2291 |
break; |
| 2292 |
} |
| 2293 |
case "selection": { |
| 2294 |
v1 = valuesMap.get(v1); |
| 2295 |
v2 = valuesMap.get(v2); |
| 2296 |
break; |
| 2297 |
} |
| 2298 |
} |
| 2299 |
const result = v1 > v2 ? 1 : v1 < v2 ? -1 : 0; |
| 2300 |
return order === "DESC" ? -result : result; |
| 2301 |
}); |
| 2302 |
|
| 2303 |
|
| 2304 |
if (orderBys.length) { |
| 2305 |
return this.sortByField(sortedRecords, modelName, orderBys.join(",")); |
| 2306 |
} |
| 2307 |
|
| 2308 |
return sortedRecords; |
| 2309 |
} |
| 2310 |
|
| 2311 |
writeRecord(modelName, values, id, { ensureIntegrity = true } = {}) { |
| 2312 |
const model = this.models[modelName]; |
| 2313 |
const record = model.records.find((r) => r.id === id); |
| 2314 |
for (const fieldName in values) { |
| 2315 |
const field = model.fields[fieldName]; |
| 2316 |
let value = values[fieldName]; |
| 2317 |
if (!field) { |
| 2318 |
throw Error( |
| 2319 |
`Mock: Can't write value "${JSON.stringify( |
| 2320 |
value |
| 2321 |
)}" on field "${fieldName}" on record "${model},${id}" (field is undefined)` |
| 2322 |
); |
| 2323 |
} |
| 2324 |
if (["one2many", "many2many"].includes(field.type)) { |
| 2325 |
let ids = record[fieldName] ? record[fieldName].slice() : []; |
| 2326 |
|
| 2327 |
|
| 2328 |
if (Array.isArray(value) && value.length) { |
| 2329 |
if ( |
| 2330 |
value.reduce((hasOnlyInt, val) => hasOnlyInt && Number.isInteger(val), true) |
| 2331 |
) { |
| 2332 |
|
| 2333 |
value = [[6, 0, value]]; |
| 2334 |
} |
| 2335 |
} else if (value === false) { |
| 2336 |
|
| 2337 |
value = [[5]]; |
| 2338 |
} |
| 2339 |
|
| 2340 |
for (const command of value || []) { |
| 2341 |
if (command[0] === 0) { |
| 2342 |
|
| 2343 |
const inverseData = command[2]; |
| 2344 |
const inverseFieldName = |
| 2345 |
field.inverse_fname_by_model_name && |
| 2346 |
field.inverse_fname_by_model_name[field.relation]; |
| 2347 |
if (inverseFieldName) { |
| 2348 |
inverseData[inverseFieldName] = id; |
| 2349 |
} |
| 2350 |
const [newId] = this.mockCreate(field.relation, [inverseData]); |
| 2351 |
ids.push(newId); |
| 2352 |
} else if (command[0] === 1) { |
| 2353 |
|
| 2354 |
this.mockWrite(field.relation, [[command[1]], command[2]]); |
| 2355 |
} else if (command[0] === 2 || command[0] === 3) { |
| 2356 |
|
| 2357 |
ids.splice(ids.indexOf(command[1]), 1); |
| 2358 |
} else if (command[0] === 4) { |
| 2359 |
|
| 2360 |
if (!ids.includes(command[1])) { |
| 2361 |
ids.push(command[1]); |
| 2362 |
} |
| 2363 |
} else if (command[0] === 5) { |
| 2364 |
|
| 2365 |
ids = []; |
| 2366 |
} else if (command[0] === 6) { |
| 2367 |
|
| 2368 |
|
| 2369 |
ids = [...command[2]]; |
| 2370 |
} else { |
| 2371 |
throw Error( |
| 2372 |
`Command "${JSON.stringify( |
| 2373 |
value |
| 2374 |
)}" not supported by the MockServer on field "${fieldName}" on record "${model},${id}"` |
| 2375 |
); |
| 2376 |
} |
| 2377 |
} |
| 2378 |
record[fieldName] = ids; |
| 2379 |
} else if (field.type === "many2one") { |
| 2380 |
if (value) { |
| 2381 |
const relRecord = this.models[field.relation].records.find( |
| 2382 |
(r) => r.id === value |
| 2383 |
); |
| 2384 |
if (!relRecord && ensureIntegrity) { |
| 2385 |
throw Error( |
| 2386 |
`Wrong id "${JSON.stringify( |
| 2387 |
value |
| 2388 |
)}" for a many2one on field "${fieldName}" on record "${model},${id}"` |
| 2389 |
); |
| 2390 |
} |
| 2391 |
record[fieldName] = value; |
| 2392 |
} else { |
| 2393 |
record[fieldName] = false; |
| 2394 |
} |
| 2395 |
} else { |
| 2396 |
record[fieldName] = value; |
| 2397 |
} |
| 2398 |
} |
| 2399 |
} |
| 2400 |
|
| 2401 |
getUnusedID(modelName) { |
| 2402 |
const model = this.models[modelName]; |
| 2403 |
return ( |
| 2404 |
model.records.reduce((max, record) => { |
| 2405 |
if (!Number.isInteger(record.id)) { |
| 2406 |
return max; |
| 2407 |
} |
| 2408 |
return Math.max(record.id, max); |
| 2409 |
}, 0) + 1 |
| 2410 |
); |
| 2411 |
} |
| 2412 |
|
| 2413 |
applyDefaults(model, record, context = {}) { |
| 2414 |
record.display_name = record.display_name || record.name; |
| 2415 |
for (const fieldName in model.fields) { |
| 2416 |
if (fieldName === "id") { |
| 2417 |
continue; |
| 2418 |
} |
| 2419 |
if (!(fieldName in record)) { |
| 2420 |
if (`default_${fieldName}` in context) { |
| 2421 |
record[fieldName] = context[`default_${fieldName}`]; |
| 2422 |
} else if ("default" in model.fields[fieldName]) { |
| 2423 |
const def = model.fields[fieldName].default; |
| 2424 |
record[fieldName] = typeof def === "function" ? def.call(this) : def; |
| 2425 |
} else if (["one2many", "many2many"].includes(model.fields[fieldName].type)) { |
| 2426 |
record[fieldName] = []; |
| 2427 |
} else { |
| 2428 |
record[fieldName] = false; |
| 2429 |
} |
| 2430 |
} |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
_unityReadRecords(modelName, spec, records) { |
| 2435 |
for (const fieldName in spec) { |
| 2436 |
const field = this.models[modelName].fields[fieldName]; |
| 2437 |
const relatedFields = spec[fieldName].fields; |
| 2438 |
switch (field.type) { |
| 2439 |
case "reference": { |
| 2440 |
for (const record of records) { |
| 2441 |
if (!record[fieldName]) { |
| 2442 |
continue; |
| 2443 |
} |
| 2444 |
const [model, i] = record[fieldName].split(","); |
| 2445 |
const id = parseInt(i, 10); |
| 2446 |
record[fieldName] = {}; |
| 2447 |
if (relatedFields && Object.keys(relatedFields).length) { |
| 2448 |
const result = this.mockWebRead(model, [[id]], { |
| 2449 |
specification: relatedFields, |
| 2450 |
context: spec[fieldName].context, |
| 2451 |
}); |
| 2452 |
record[fieldName] = result[0]; |
| 2453 |
} |
| 2454 |
record[fieldName].id = { id, model }; |
| 2455 |
} |
| 2456 |
break; |
| 2457 |
} |
| 2458 |
case "many2one_reference": { |
| 2459 |
for (const record of records) { |
| 2460 |
const id = record[fieldName]; |
| 2461 |
if (!id) { |
| 2462 |
record[fieldName] = 0; |
| 2463 |
continue; |
| 2464 |
} |
| 2465 |
if (!relatedFields) { |
| 2466 |
continue; |
| 2467 |
} |
| 2468 |
|
| 2469 |
|
| 2470 |
const dbRecord = this.models[modelName].records.find( |
| 2471 |
(r) => r.id === record.id |
| 2472 |
); |
| 2473 |
const model = dbRecord[field.model_field]; |
| 2474 |
record[fieldName] = {}; |
| 2475 |
if (relatedFields && Object.keys(relatedFields).length) { |
| 2476 |
const result = this.mockWebRead(model, [[id]], { |
| 2477 |
specification: relatedFields, |
| 2478 |
context: spec[fieldName].context, |
| 2479 |
}); |
| 2480 |
record[fieldName] = result[0]; |
| 2481 |
} |
| 2482 |
} |
| 2483 |
break; |
| 2484 |
} |
| 2485 |
case "one2many": |
| 2486 |
case "many2many": { |
| 2487 |
if (relatedFields && Object.keys(relatedFields).length) { |
| 2488 |
const ids = unique(records.map((r) => r[fieldName]).flat()); |
| 2489 |
const result = this.mockWebRead(field.relation, [ids], { |
| 2490 |
specification: relatedFields, |
| 2491 |
context: spec[fieldName].context, |
| 2492 |
}); |
| 2493 |
const allRelRecords = {}; |
| 2494 |
for (const relRecord of result) { |
| 2495 |
allRelRecords[relRecord.id] = relRecord; |
| 2496 |
} |
| 2497 |
const { limit, order } = spec[fieldName]; |
| 2498 |
for (const record of records) { |
| 2499 |
const relResIds = record[fieldName]; |
| 2500 |
let relRecords = relResIds.map((resId) => allRelRecords[resId]); |
| 2501 |
if (order) { |
| 2502 |
relRecords = this.sortByField(relRecords, field.relation, order); |
| 2503 |
} |
| 2504 |
if (limit) { |
| 2505 |
relRecords = relRecords.map((r, i) => |
| 2506 |
i < limit ? r : { id: r.id } |
| 2507 |
); |
| 2508 |
} |
| 2509 |
record[fieldName] = relRecords; |
| 2510 |
} |
| 2511 |
} |
| 2512 |
break; |
| 2513 |
} |
| 2514 |
case "many2one": { |
| 2515 |
for (const record of records) { |
| 2516 |
if (record[fieldName] !== false) { |
| 2517 |
if (!relatedFields) { |
| 2518 |
record[fieldName] = record[fieldName][0]; |
| 2519 |
} else { |
| 2520 |
record[fieldName] = this.mockWebRead( |
| 2521 |
field.relation, |
| 2522 |
[record[fieldName][0]], |
| 2523 |
{ |
| 2524 |
specification: relatedFields, |
| 2525 |
context: spec[fieldName].context, |
| 2526 |
} |
| 2527 |
)[0]; |
| 2528 |
} |
| 2529 |
} |
| 2530 |
} |
| 2531 |
} |
| 2532 |
} |
| 2533 |
} |
| 2534 |
} |
| 2535 |
} |
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 2542 |
let mockServer; |
| 2543 |
QUnit.testStart(() => (mockServer = undefined)); |
| 2544 |
export async function makeMockServer(serverData, mockRPC) { |
| 2545 |
serverData = serverData || {}; |
| 2546 |
if (!mockServer) { |
| 2547 |
mockServer = new MockServer(serverData, { |
| 2548 |
debug: QUnit.config.debug, |
| 2549 |
}); |
| 2550 |
} else { |
| 2551 |
Object.assign(mockServer.archs, serverData.views); |
| 2552 |
Object.assign(mockServer.actions, serverData.actions); |
| 2553 |
} |
| 2554 |
const _mockRPC = async (route, args = {}) => { |
| 2555 |
let res; |
| 2556 |
if (args.method !== "POST") { |
| 2557 |
|
| 2558 |
args = JSON.parse(JSON.stringify(args)); |
| 2559 |
} |
| 2560 |
if (!mockServer.active) { |
| 2561 |
|
| 2562 |
return new Promise(() => {}); |
| 2563 |
} |
| 2564 |
if (mockRPC) { |
| 2565 |
res = await mockRPC(route, args, mockServer.performRPC.bind(mockServer)); |
| 2566 |
} |
| 2567 |
if (res === undefined) { |
| 2568 |
res = await mockServer.performRPC(route, args); |
| 2569 |
} |
| 2570 |
return res; |
| 2571 |
}; |
| 2572 |
patchWithCleanup(browser, { |
| 2573 |
fetch: makeMockFetch(_mockRPC), |
| 2574 |
}); |
| 2575 |
patchRPCWithCleanup(_mockRPC); |
| 2576 |
if (mockRPC) { |
| 2577 |
const { loadJS, loadCSS } = assets; |
| 2578 |
patchWithCleanup(assets, { |
| 2579 |
async loadJS(resource) { |
| 2580 |
if (resource === "/web/static/lib/stacktracejs/stacktrace.js") { |
| 2581 |
|
| 2582 |
|
| 2583 |
return loadJS(resource); |
| 2584 |
} |
| 2585 |
let res = await mockRPC(resource, {}); |
| 2586 |
if (res === undefined) { |
| 2587 |
res = await loadJS(resource); |
| 2588 |
} else { |
| 2589 |
makeLogger("ASSETS", "fetch (mock) JS resource").request(resource); |
| 2590 |
} |
| 2591 |
return res; |
| 2592 |
}, |
| 2593 |
async loadCSS(resource) { |
| 2594 |
let res = await mockRPC(resource, {}); |
| 2595 |
if (res === undefined) { |
| 2596 |
res = await loadCSS(resource); |
| 2597 |
} else { |
| 2598 |
makeLogger("ASSETS", "fetch (mock) CSS resource").request(resource); |
| 2599 |
} |
| 2600 |
return res; |
| 2601 |
}, |
| 2602 |
}); |
| 2603 |
} |
| 2604 |
|
| 2605 |
registerCleanup(() => (mockServer.active = false)); |
| 2606 |
return mockServer; |
| 2607 |
} |