Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions addons/test_website_modules/tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ def setUpClass(cls):
})
cls.product_images = cls.env['product.image'].with_context(default_product_tmpl_id=cls.productC.product_tmpl_id.id).create([{
'name': 'Template image',
'image_1920': blue_image,
}, {
'name': 'Variant image',
'image_1920': red_image,
'product_variant_id': cls.productC.id,
}])

for i in range(20):
Expand Down Expand Up @@ -148,7 +144,7 @@ def setUpClass(cls):
images.append({
'name': 'Variant image',
'image_1920': red_image,
'product_variant_id': variant.id,
'product_variant_ids': [Command.link(variant.id)],
})
cls.env['product.image'].create(images)

Expand Down Expand Up @@ -291,11 +287,11 @@ def _allow_to_use_cache(request):

def _get_queries_shop(self):
html = self.url_open('/shop').text
self.assertIn(f'<img src="/web/image/product.product/{self.productC.id}/', html)
self.assertIn(f'<img src="/web/image/product.template/{self.productC.product_tmpl_id.id}/', html)
self.assertIn(f'<img src="/web/image/product.template/{self.productA.product_tmpl_id.id}/', html)
self.assertIn(f'<img src="/web/image/product.image/{self.product_images.ids[1]}/', html)
self.assertIn(f'<img src="/web/image/product.image/{self.product_images.ids[0]}/', html)

query_count = 49 # To increase this number you must ask the permission to al
query_count = 45 # To increase this number you must ask the permission to al
queries = {
'orm_signaling_registry': 1,
'website': 1,
Expand All @@ -316,9 +312,8 @@ def _get_queries_shop(self):
'product_ribbon': 1,
'product_attribute_value': 3,
'product_attribute': 1,
'ir_attachment': 4,
'product_image': 3,
'product_template_attribute_value': 1,
'ir_attachment': 2,
'product_image': 2,
'ir_ui_view': 2,
'website_menu': 1,
'website_page': 1,
Expand All @@ -344,9 +339,6 @@ def _get_queries_shop(self):
queries['ir_attachment'] += 1
queries['product_ribbon'] += 1
queries['res_company'] += 1
else:
query_count += 3
queries['product_template_attribute_value'] += 3

# To increase the query count you must ask the permission to al
return query_count, queries
Expand Down
15 changes: 15 additions & 0 deletions addons/website_sale/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def _post_init_hook(env):
existing_websites = env['website'].search([])
for website in existing_websites:
website._create_checkout_steps()
_create_extra_variant_images(env)

def uninstall_hook(env):
''' Need to reenable the `product` pricelist multi-company rule that were
Expand All @@ -27,3 +28,17 @@ def uninstall_hook(env):
multi_company_rules = pl_rule or env['ir.rule']
multi_company_rules += pl_item_rule or env['ir.rule']
multi_company_rules.write({'active': True})


def _create_extra_variant_images(env):
products = env['product.product'].search([('product_tmpl_id.image_1920', '!=', False)])
image_vals = []
for product in products:
image_vals.append({
'name': product.display_name,
'product_variant_ids': [(4, product.id)],
'attribute_value_ids': [(6, 0, product.product_template_attribute_value_ids.ids)],
'image_1920': product.image_variant_1920 or product.product_tmpl_id.image_1920,
'sequence': 0,
})
env['product.image'].create(image_vals)
1 change: 1 addition & 0 deletions addons/website_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
'website_sale/static/src/js/website_sale_video_field_preview.js',
'website_sale/static/src/scss/website_sale_backend.scss',
'website_sale/static/src/js/tours/website_sale_shop.js',
'website_sale/static/src/js/product_image/*',
'website_sale/static/src/xml/website_sale.xml',
'website_sale/static/src/scss/kanban_record.scss',
'website_sale/static/src/js/dashboard/dashboard.js',
Expand Down
86 changes: 56 additions & 30 deletions addons/website_sale/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,47 @@ def add_product_media(self, media, type, product_product_id, product_template_id
if not request.env.user.has_group('website.group_website_restricted_editor'):
raise NotFound()

product_product = (
request.env['product.product'].browse(int(product_product_id))
if product_product_id else False
)
product_template = (
request.env['product.template'].browse(int(product_template_id))
if product_template_id else False
)

if product_product and not product_template:
product_template = product_product.product_tmpl_id

if not product_product and product_template and product_template.has_dynamic_attributes():
combination = request.env['product.template.attribute.value'].browse(combination_ids)
product_product = product_template._get_variant_for_combination(combination)
if not product_product:
product_product = product_template._create_product_variant(combination)

is_variant_media = (
product_template.has_configurable_attributes
and product_product
and not all(
pa.create_variant == 'no_variant'
for pa in product_template.attribute_line_ids.attribute_id
)
)
if type == 'image': # Image case
image_ids = request.env["ir.attachment"].browse(i['id'] for i in media)
media_create_data = [Command.create({
'name': image.name, # Images uploaded from url do not have any datas. This recovers them manually
'image_1920': image.datas
if image.datas
else request.env['ir.qweb.field.image'].load_remote_url(image.url),
}) for image in image_ids]
media_create_data = []
for image in image_ids:
media_create_values = {
'name': image.name, # Images uploaded from url do not have any datas. This recovers them manually
'image_1920': image.datas
if image.datas
else request.env['ir.qweb.field.image'].load_remote_url(image.url),
}
if is_variant_media:
media_create_values['attribute_value_ids'] = [
Command.set(product_product.product_template_attribute_value_ids.ids)
]
media_create_data.append(Command.create(media_create_values))
elif type == 'video': # Video case
video_data = media[0]
thumbnail = None
Expand All @@ -679,18 +712,7 @@ def add_product_media(self, media, type, product_product_id, product_template_id
'image_1920': thumbnail,
})]

product_product = request.env['product.product'].browse(int(product_product_id)) if product_product_id else False
product_template = request.env['product.template'].browse(int(product_template_id)) if product_template_id else False

if product_product and not product_template:
product_template = product_product.product_tmpl_id

if not product_product and product_template and product_template.has_dynamic_attributes():
combination = request.env['product.template.attribute.value'].browse(combination_ids)
product_product = product_template._get_variant_for_combination(combination)
if not product_product:
product_product = product_template._create_product_variant(combination)
if product_template.has_configurable_attributes and product_product and not all(pa.create_variant == 'no_variant' for pa in product_template.attribute_line_ids.attribute_id):
if is_variant_media:
product_product.write({
'product_variant_image_ids': media_create_data
})
Expand Down Expand Up @@ -719,14 +741,16 @@ def clear_product_images(self, product_product_id, product_template_id):
product_template.product_template_image_ids.unlink()

@route(['/shop/product/resequence-image'], type='jsonrpc', auth='user', website=True)
def resequence_product_image(self, image_res_model, image_res_id, move):
def resequence_product_image(self, image_res_model, image_res_id, move, product_variant_id):
"""
Move the product image in the given direction and update all images' sequence.

:param str image_res_model: The model of the image. It can be 'product.template',
'product.product', or 'product.image'.
or 'product.image'.
:param str image_res_id: The record ID of the image to move.
:param str move: The direction of the move. It can be 'first', 'left', 'right', or 'last'.
:param str product_variant_id: The ID of the product variant in whose context the image
resequencing is performed
:raises NotFound: If the user does not have the required permissions, if the model of the
image is not allowed, or if the move direction is not allowed.
:raise ValidationError: If the product is not found.
Expand All @@ -740,18 +764,20 @@ def resequence_product_image(self, image_res_model, image_res_id, move):
or move not in ['first', 'left', 'right', 'last']
):
raise NotFound()

image_res_id = int(image_res_id)
image_to_resequence = request.env[image_res_model].browse(image_res_id)
if image_res_model == 'product.product':
product = image_to_resequence
product_template = product.product_tmpl_id
elif image_res_model == 'product.template':
if image_res_model == 'product.template':
product_template = image_to_resequence
product = product_template.product_variant_id
else:
product = image_to_resequence.product_variant_id
product = image_to_resequence.product_variant_ids.filtered(
lambda p: p.id == int(product_variant_id)
)
product_template = product.product_tmpl_id or image_to_resequence.product_tmpl_id
if not product:
product = product_template.product_variant_ids.filtered(
lambda p: p.id == int(product_variant_id)
)

if not product and not product_template:
raise ValidationError(_("Product not found"))
Expand All @@ -778,12 +804,12 @@ def resequence_product_image(self, image_res_model, image_res_id, move):

# If the main image has been reordered (i.e. it's no longer in first position), use the
# image that's now in first position as main image instead.
# Additional images are product.image records. The main image is a product.product or
# product.template record.
# Additional images are product.image records. The main image is a product.template record.
main_image_idx = next(
idx for idx, image in enumerate(product_images) if image._name != 'product.image'
(idx for idx, image in enumerate(product_images) if image._name != 'product.image'),
None
)
if main_image_idx != 0:
if main_image_idx is not None and main_image_idx != 0:
main_image = product_images[main_image_idx]
additional_image = product_images[0]
if additional_image.video_url:
Expand Down
Loading