diff options
| author | Samuel Degueldre <sad@odoo.com> | 2020-07-31 06:47:34 +0000 |
|---|---|---|
| committer | qsm-odoo <qsm@odoo.com> | 2020-08-07 15:34:45 +0000 |
| commit | b84e0af742c51b88b4c108ebec2d0c7fff4b7483 (patch) | |
| tree | 5f26347b2ffc0dd30ddbe6193c21218a9e922c91 /addons/web_editor/controllers | |
| parent | c3dacfea05c531683bfcf94243597ce2d82aeab8 (diff) | |
[IMP] web_editor, website: add background shapes snippet option
This commit adds a new snippet option allowing the user to choose one of
a series of SVG shapes as their background, or to be overlaid over the
background image of a snippet and to customize the colors of those
shapes.
Part of https://github.com/odoo/odoo/pull/53017
task-2210790
closes odoo/odoo#53017
Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
Diffstat (limited to 'addons/web_editor/controllers')
| -rw-r--r-- | addons/web_editor/controllers/main.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index 3c2f3a31c89..2b2a5a7d650 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -7,10 +7,12 @@ import time import werkzeug.wrappers from PIL import Image, ImageFont, ImageDraw from lxml import etree +from base64 import b64decode from odoo.http import request from odoo import http, tools, _ from odoo.exceptions import UserError +from odoo.modules.module import get_resource_path logger = logging.getLogger(__name__) @@ -492,3 +494,45 @@ class Web_Editor(http.Controller): return attachment.image_src attachment.generate_access_token() return '%s?access_token=%s' % (attachment.image_src, attachment.access_token) + + @http.route(['/web_editor/shape/<module>/<path:filename>'], type='http', auth="public", website=True) + def background_shape(self, module, filename, **kwargs): + """ + Returns a color-customized background-shape. + """ + shape_path = get_resource_path(module, 'static', 'shapes', filename) + if shape_path: + svg = open(shape_path, 'r').read() + else: + # Fallback to attachments for future migrations + attachment = request.env['ir.attachment'].search([('url', '=like', request.httprequest.path)], limit=1) + if not attachment: + raise werkzeug.exceptions.NotFound() + svg = b64decode(attachment.datas).decode('utf-8') + + user_colors = [] + for key, color in kwargs.items(): + match = re.match('^c([12345])$', key) + if match: + user_colors.append([tools.html_escape(color), match.group(1)]) + + default_palette = { + '1': '#3AADAA', + '2': '#7C6576', + '3': '#F6F6F6', + '4': '#FFFFFF', + '5': '#383E45', + } + color_mapping = {default_palette[palette_number]: color for color, palette_number in user_colors} + # create a case-insensitive regex to match all the colors to replace, eg: '(?i)(#3AADAA)|(#7C6576)' + regex = '(?i)%s' % '|'.join('(%s)' % color for color in color_mapping.keys()) + + def subber(match): + key = match.group().upper() + return color_mapping[key] if key in color_mapping else key + svg = re.sub(regex, subber, svg) + + return request.make_response(svg, [ + ('Content-type', 'image/svg+xml'), + ('Cache-control', 'max-age=%s' % http.STATIC_CACHE_LONG), + ]) |
