summaryrefslogtreecommitdiff
path: root/addons/website/controllers/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'addons/website/controllers/main.py')
-rw-r--r--addons/website/controllers/main.py79
1 files changed, 49 insertions, 30 deletions
diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py
index 3852fefe740..a2d1d332f80 100644
--- a/addons/website/controllers/main.py
+++ b/addons/website/controllers/main.py
@@ -4,6 +4,7 @@ import base64
import datetime
import json
import os
+import re
import logging
import requests
import werkzeug.utils
@@ -331,45 +332,27 @@ class Website(Home):
# Themes
# ------------------------------------------------------
- def get_view_ids(self, xml_ids):
- ids = []
+ def _get_customize_views(self, xml_ids):
View = request.env["ir.ui.view"].with_context(active_test=False)
- for xml_id in xml_ids:
- if "." in xml_id:
- # Get website-specific view if possible
- record_id = View.search([
- ("website_id", "=", request.website.id),
- ("key", "=", xml_id),
- ], limit=1).id or request.env.ref(xml_id).id
- else:
- record_id = int(xml_id)
- ids.append(record_id)
- return ids
+ if not xml_ids:
+ return View
+ domain = [("key", "in", xml_ids)] + request.website.website_domain()
+ return View.search(domain).filter_duplicate()
@http.route(['/website/theme_customize_get'], type='json', auth="public", website=True)
def theme_customize_get(self, xml_ids):
- enable = []
- disable = []
- ids = self.get_view_ids(xml_ids)
- for view in request.env['ir.ui.view'].browse(ids):
- if view.active:
- enable.append(view.key)
- else:
- disable.append(view.key)
- return [enable, disable]
+ views = self._get_customize_views(xml_ids)
+ return {
+ 'enabled': views.filtered('active').mapped('key'),
+ 'names': {view.key: view.name for view in views},
+ }
@http.route(['/website/theme_customize'], type='json', auth="public", website=True)
def theme_customize(self, enable=None, disable=None, get_bundle=False):
""" enable or Disable lists of ``xml_id`` of the inherit templates """
- def set_active(xml_ids, active):
- if xml_ids:
- real_ids = self.get_view_ids(xml_ids)
- request.env['ir.ui.view'].browse(real_ids).write({'active': active})
- if disable:
- set_active(disable, False)
- if enable:
- set_active(enable, True)
+ self._get_customize_views(disable).write({'active': False})
+ self._get_customize_views(enable).write({'active': True})
if get_bundle:
context = dict(request.context)
@@ -386,6 +369,42 @@ class Website(Home):
self.theme_customize(enable and enable.split(",") or [], disable and disable.split(",") or [])
return request.redirect(href + ("&theme=true" if "#" in href else "#theme=true") + ("&tab=" + tab))
+ @http.route(['/website/make_scss_custo'], type='json', auth='user', website=True)
+ def make_scss_custo(self, url, values):
+ """
+ Makes a scss customization of the given file. That file must
+ contain a scss map including a line comment containing the word 'hook',
+ to indicate the location where to write the new key,value pairs.
+
+ Params:
+ url (str):
+ the URL of the scss file to customize (supposed to be a variable
+ file which will appear in the assets_common bundle)
+
+ values (dict):
+ key,value mapping to integrate in the file's map (containing the
+ word hook). If a key is already in the file's map, its value is
+ overridden.
+ """
+ AssetsUtils = request.env['web_editor.assets']
+
+ custom_url = AssetsUtils.make_custom_asset_file_url(url, 'web.assets_common')
+ updatedFileContent = AssetsUtils.get_asset_content(custom_url) or AssetsUtils.get_asset_content(url)
+ updatedFileContent = updatedFileContent.decode('utf-8')
+ for name, value in values.items():
+ pattern = "'%s': %%s,\n" % name
+ regex = re.compile(pattern % ".+")
+ replacement = pattern % value
+ if regex.search(updatedFileContent):
+ updatedFileContent = re.sub(regex, replacement, updatedFileContent)
+ else:
+ updatedFileContent = re.sub(r'( *)(.*hook.*)', r'\1%s\1\2' % replacement, updatedFileContent)
+
+ # Bundle is 'assets_common' as this route is only meant to update
+ # variables scss files
+ AssetsUtils.save_asset(url, 'web.assets_common', updatedFileContent, 'scss')
+ return True
+
@http.route(['/website/multi_render'], type='json', auth="public", website=True)
def multi_render(self, ids_or_xml_ids, values=None):
View = request.env['ir.ui.view']