[FIX] website: use the domain on the form fields

For each form action, existing fields can be defined. These existing
fields are references to objects in the ORM. It is possible to put a
domain on the field in order to filter the elements you want. For
example, the "Create an opportunity" action has a "Salesperson" field
that refers to users. This field filters the external users of the
company in order to have only the internal users. Unfortunately,
although this domain is present, it was not taken into account when
displaying the values for the field. This commit fixes this problem and
takes into account the defined domain.

Steps to reproduce the bug fixed by this commit:
- Have the website_crm module installed
- Put a form on a page of the website
- Set the form action to "Create an Opportunity"
- Add a new field
- Set the field type to "Salesperson"

=> The list of users displayed contains external users of the company.

task-3095745

closes odoo/odoo#121903

Signed-off-by: Benoit Socias (bso) <bso@odoo.com>
This commit is contained in:
Guillaume (gdi)
2023-06-27 16:26:43 +02:00
parent 5c37a45bec
commit 4c0840cac3
2 changed files with 35 additions and 1 deletions
@@ -3,6 +3,7 @@
import Wysiwyg from "web_editor.wysiwyg";
import snippetsEditor from "website.snippet.editor";
import socialMediaOptions from "@website/snippets/s_social_media/options";
import formOptions from "@website/snippets/s_website_form/options";
/**
* Show/hide the dropdowns associated to the given toggles and allows to wait
@@ -113,6 +114,8 @@ const WebsiteWysiwyg = Wysiwyg.extend({
// from the accessed website.
socialMediaOptions.clearDbSocialValuesCache();
formOptions.clearAllFormsInfo();
this._restoreMegaMenus();
this._super.apply(this, arguments);
},
@@ -14,6 +14,31 @@ const qweb = core.qweb;
const _t = core._t;
let currentActionName;
const allFormsInfo = new Map();
const clearAllFormsInfo = () => {
allFormsInfo.clear();
};
/**
* Returns the domain of a field.
*
* @private
* @param {HTMLElement} formEl
* @param {String} name
* @param {String} type
* @param {String} relation
* @returns {Object|false}
*/
function _getDomain(formEl, name, type, relation) {
// We need this because the field domain is in formInfo in the
// WebsiteFormEditor but we need it in the WebsiteFieldEditor.
if (!allFormsInfo.get(formEl) || !name || !type || !relation) {
return false;
}
const field = allFormsInfo.get(formEl).fields
.find(el => el.name === name && el.type === type && el.relation === relation);
return field && field.domain;
}
const FormEditor = options.Class.extend({
//----------------------------------------------------------------------
// Private
@@ -607,6 +632,7 @@ options.registry.WebsiteFormEditor = FormEditor.extend({
if (!formInfo || !formInfo.fields) {
return;
}
allFormsInfo.set(this.$target[0], formInfo);
const proms = formInfo.fields.map(field => this._fetchFieldRecords(field));
return Promise.all(proms).then(() => {
formInfo.fields.forEach(field => {
@@ -849,7 +875,8 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({
this.fields = {};
for (const [fieldName, field] of Object.entries(fields)) {
field.name = fieldName;
field.domain = field.domain || [];
const fieldDomain = _getDomain(this.formEl, field.name, field.type, field.relation);
field.domain = fieldDomain || field.domain || [];
this.fields[fieldName] = field;
}
// Create the buttons for the type we-select
@@ -1590,3 +1617,7 @@ options.registry.DeviceVisibility.include({
&& !this.$target.hasClass('s_website_form_field_hidden');
},
});
export default {
clearAllFormsInfo,
};