summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDramixDw <rve@odoo.com>2020-05-18 16:10:05 +0000
committerJeremy Kersten <jke@odoo.com>2020-05-29 13:59:15 +0000
commit714f0aa07f25e8a88a778e8b21c79b48e9801231 (patch)
tree5bf93bb6fe6810abef9e33495bdd2026e34f7f55
parente16a325329be1d49d856ee0255224534baa0e353 (diff)
[IMP] website: reintroduce a way to post from js
It was deleted in 11.0 as it was not used anymore. Now, it is reintroduce as it is a nice utility function for some external app or fixes. Here, it is use so that /website/add and /website/add/<path> work as a controller POST with a CSRF token. task-2241766
-rw-r--r--addons/website/controllers/main.py2
-rw-r--r--addons/website/static/src/js/menu/new_content.js5
-rw-r--r--addons/website/static/src/js/utils.js33
3 files changed, 37 insertions, 3 deletions
diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py
index e6efda5b16a..e87f87d0f69 100644
--- a/addons/website/controllers/main.py
+++ b/addons/website/controllers/main.py
@@ -311,7 +311,7 @@ class Website(Home):
}
return request.render("website.list_website_pages", values)
- @http.route(['/website/add/', '/website/add/<path:path>'], type='http', auth="user", website=True)
+ @http.route(['/website/add/', '/website/add/<path:path>'], type='http', auth="user", website=True, methods=['POST'])
def pagenew(self, path="", noredirect=False, add_menu=False, template=False, **kwargs):
# for supported mimetype, get correct default template
_, ext = os.path.splitext(path)
diff --git a/addons/website/static/src/js/menu/new_content.js b/addons/website/static/src/js/menu/new_content.js
index efb1b428134..1d6fdfce77e 100644
--- a/addons/website/static/src/js/menu/new_content.js
+++ b/addons/website/static/src/js/menu/new_content.js
@@ -94,8 +94,9 @@ var NewContentMenu = websiteNavbarData.WebsiteNavbarActionWidget.extend({
return;
}
var url = '/website/add/' + encodeURIComponent(val);
- if ($dialog.find('input[type="checkbox"]').is(':checked')) url +='?add_menu=1';
- document.location = url;
+ const res = wUtils.sendRequest(url, {
+ add_menu: $dialog.find('input[type="checkbox"]').is(':checked') || '',
+ });
return new Promise(function () {});
});
},
diff --git a/addons/website/static/src/js/utils.js b/addons/website/static/src/js/utils.js
index 7947eb6f93c..2544f4347fc 100644
--- a/addons/website/static/src/js/utils.js
+++ b/addons/website/static/src/js/utils.js
@@ -227,11 +227,44 @@ function websiteDomain(self) {
return ['|', ['website_id', '=', false], ['website_id', '=', websiteID]];
}
+function sendRequest(route, params) {
+ function _addInput(form, name, value) {
+ let param = document.createElement('input');
+ param.setAttribute('type', 'hidden');
+ param.setAttribute('name', name);
+ param.setAttribute('value', value);
+ form.appendChild(param);
+ }
+
+ let form = document.createElement('form');
+ form.setAttribute('action', route);
+ form.setAttribute('method', params.method || 'POST');
+
+ if (core.csrf_token) {
+ _addInput(form, 'csrf_token', core.csrf_token);
+ }
+
+ for (const key in params) {
+ const value = params[key];
+ if (Array.isArray(value) && value.length) {
+ for (const val of value) {
+ _addInput(form, key, val);
+ }
+ } else {
+ _addInput(form, key, value);
+ }
+ }
+
+ document.body.appendChild(form);
+ form.submit();
+}
+
return {
loadAnchors: loadAnchors,
autocompleteWithPages: autocompleteWithPages,
onceAllImagesLoaded: onceAllImagesLoaded,
prompt: prompt,
+ sendRequest: sendRequest,
websiteDomain: websiteDomain,
};
});