From 46e40459f7336132dc1cd7c2811a936557a8bd38 Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Mon, 25 Nov 2024 08:27:32 +0000 Subject: [PATCH] adj --- .../__manifest__.py | 1 + .../data/scheduler.xml | 17 +++++++++++++++++ .../models/__init__.py | 1 + .../models/ir_attachment.py | 16 +++++++++------- .../models/product_image.py | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 product_carousel_image_attachment/data/scheduler.xml create mode 100644 product_carousel_image_attachment/models/product_image.py diff --git a/product_carousel_image_attachment/__manifest__.py b/product_carousel_image_attachment/__manifest__.py index 1c3b10a7..e7000589 100644 --- a/product_carousel_image_attachment/__manifest__.py +++ b/product_carousel_image_attachment/__manifest__.py @@ -8,5 +8,6 @@ "category": "Tools", "license": "AGPL-3", "depends": ["website_sale"], + "data": ["data/scheduler.xml"], "installable": True, } diff --git a/product_carousel_image_attachment/data/scheduler.xml b/product_carousel_image_attachment/data/scheduler.xml new file mode 100644 index 00000000..1f3698bd --- /dev/null +++ b/product_carousel_image_attachment/data/scheduler.xml @@ -0,0 +1,17 @@ + + + + + Resize Extra Product Image + + code + model._cron_resize_product_image(1000) + + 1 + days + -1 + + + + diff --git a/product_carousel_image_attachment/models/__init__.py b/product_carousel_image_attachment/models/__init__.py index aaf38a16..3305f2c0 100644 --- a/product_carousel_image_attachment/models/__init__.py +++ b/product_carousel_image_attachment/models/__init__.py @@ -1 +1,2 @@ from . import ir_attachment +from . import product_image diff --git a/product_carousel_image_attachment/models/ir_attachment.py b/product_carousel_image_attachment/models/ir_attachment.py index 49313f4b..1116706f 100644 --- a/product_carousel_image_attachment/models/ir_attachment.py +++ b/product_carousel_image_attachment/models/ir_attachment.py @@ -10,19 +10,21 @@ class IrAttachment(models.Model): _inherit = "ir.attachment" + @api.model def _resize_image(self, datas): ICP = self.env["ir.config_parameter"].sudo().get_param + # Use 1025 instead of 1024 to enable the zoom feature. + # Define a static value instead of modifying the system parameter + # 'base.image_autoresize_extensions' to avoid + # affecting other image fields. + nw, nh = (1025, 1025) quality = int(ICP("base.image_autoresize_quality", 80)) - # Use 1025 instead of 1024 to enable the zoom feature - max_resolution = (1025, 1025) img = ImageProcess(datas, verify_resolution=False) w, h = img.image.size - nw, nh = map(int, max_resolution.split("x")) if w > nw or h > nh: - img = img.resize(nw, nh) # Resize the image - return img.image_base64( - quality=quality - ) # Return the resized image as base64 + # Use odoo standard resize + img = img.resize(nw, nh) + return img.image_base64(quality=quality) return datas @api.model_create_multi diff --git a/product_carousel_image_attachment/models/product_image.py b/product_carousel_image_attachment/models/product_image.py new file mode 100644 index 00000000..4f98e67e --- /dev/null +++ b/product_carousel_image_attachment/models/product_image.py @@ -0,0 +1,18 @@ +# Copyright 2024 Quartile +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class ProductImage(models.Model): + _inherit = "product.image" + + @api.model + def _cron_resize_product_image(self, limit): + images = self.sudo().search([], limit=limit) + for image in images: + image.image_1920 = self.env["ir.attachment"]._resize_image(image.image_1920) + if len(images) == limit: + self.env.ref( + "product_carousel_image_attachment.resize_product_image" + )._trigger()