diff --git a/addons/website/models/mixins.py b/addons/website/models/mixins.py index bbc1d60bfb1..039196562c2 100644 --- a/addons/website/models/mixins.py +++ b/addons/website/models/mixins.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -import json import logging - from odoo import api, fields, models, _ from odoo.http import request from odoo.osv import expression from odoo.exceptions import AccessError +from odoo.tools.json import scriptsafe as json_safe logger = logging.getLogger(__name__) @@ -102,7 +101,7 @@ class WebsiteCoverPropertiesMixin(models.AbstractModel): _name = 'website.cover_properties.mixin' _description = 'Cover Properties Website Mixin' - cover_properties = fields.Text('Cover Properties', default=lambda s: json.dumps(s._default_cover_properties())) + cover_properties = fields.Text('Cover Properties', default=lambda s: json_safe.dumps(s._default_cover_properties())) def _default_cover_properties(self): return { @@ -112,6 +111,21 @@ class WebsiteCoverPropertiesMixin(models.AbstractModel): "resize_class": "o_half_screen_height", } + def _get_background(self, height=None, width=None): + self.ensure_one() + properties = json_safe.loads(self.cover_properties) + img = properties.get('background-image', "none") + + if img.startswith('url(/web/image/'): + suffix = "" + if height is not None: + suffix += "&height=%s" % height + if width is not None: + suffix += "&width=%s" % width + if suffix: + suffix = '?' not in img and "?%s" % suffix or suffix + img = img[:-1] + suffix + ')' + return img class WebsiteMultiMixin(models.AbstractModel): diff --git a/addons/website/models/website_snippet_filter.py b/addons/website/models/website_snippet_filter.py index 336ec100314..5896eabb001 100644 --- a/addons/website/models/website_snippet_filter.py +++ b/addons/website/models/website_snippet_filter.py @@ -65,6 +65,9 @@ class WebsiteSnippetFilter(models.Model): if self.website_id and self.env['website'].get_current_website() != self.website_id: return '' + if self.model_name.replace('.', '_') not in template_key: + return '' + records = self._prepare_values(limit, search_domain) is_sample = with_sample and not records if is_sample: @@ -173,10 +176,11 @@ class WebsiteSnippetFilter(models.Model): sample = [] model = self.env[self.model_name] sample_data = self._get_hardcoded_sample(model) - for index in range(0, length): - single_sample_data = sample_data[index % len(sample_data)].copy() - self._fill_sample(single_sample_data, index) - sample.append(model.new(single_sample_data)) + if sample_data: + for index in range(0, length): + single_sample_data = sample_data[index % len(sample_data)].copy() + self._fill_sample(single_sample_data, index) + sample.append(model.new(single_sample_data)) return sample def _fill_sample(self, sample, index): diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/000.xml b/addons/website/static/src/snippets/s_dynamic_snippet/000.xml index 105078e087f..9d73d41d8e0 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/000.xml +++ b/addons/website/static/src/snippets/s_dynamic_snippet/000.xml @@ -2,7 +2,7 @@ - + diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/options.js b/addons/website/static/src/snippets/s_dynamic_snippet/options.js index 3b9e762b25f..4777d27729a 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/options.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet/options.js @@ -42,12 +42,19 @@ const dynamicSnippetOptions = options.Class.extend({ this.$target.get(0).dataset.numberOfRecords = filter.limit; this._filterUpdated(filter); } + if (params.attributeName === 'templateKey' && previewMode === false) { + this._templateUpdated(widgetValue, params.activeValue); + } }, //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- + _getTemplateClass: function (templateKey) { + return templateKey.replace(/.*\.dynamic_filter_template_/, "s_"); + }, + /** * * @override @@ -148,6 +155,7 @@ const dynamicSnippetOptions = options.Class.extend({ */ _renderDynamicFilterTemplatesSelector: async function (uiFragment) { const dynamicFilterTemplates = await this._fetchDynamicFilterTemplates(); + this.dynamicFilterTemplates = {}; for (let index in dynamicFilterTemplates) { this.dynamicFilterTemplates[dynamicFilterTemplates[index].key] = dynamicFilterTemplates[index]; } @@ -155,7 +163,10 @@ const dynamicSnippetOptions = options.Class.extend({ const selectedTemplateId = this.$target.get(0).dataset['templateKey']; if (!this.dynamicFilterTemplates[selectedTemplateId]) { this.$target.get(0).dataset['templateKey'] = dynamicFilterTemplates[0].key; - this._refreshPublicWidgets(); + setTimeout(() => { + this._templateUpdated(dynamicFilterTemplates[0].key, selectedTemplateId); + this._refreshPublicWidgets(); + }); } } else { this._refreshPublicWidgets(); @@ -190,6 +201,18 @@ const dynamicSnippetOptions = options.Class.extend({ this._rerenderXML(); } }, + /** + * Take the new template selection into account + * @param newTemplate + * @param oldTemplate + * @private + */ + _templateUpdated: function (newTemplate, oldTemplate) { + if (oldTemplate) { + this.$target.removeClass(this._getTemplateClass(oldTemplate)); + } + this.$target.addClass(this._getTemplateClass(newTemplate)); + }, /** * Sets the option value. * @param optionName diff --git a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml index 1efb8f22e80..0a69cbadb1b 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml +++ b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml @@ -4,7 +4,7 @@