Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4981][IMP] product_carousel_image_attachment #151

Open
wants to merge 10 commits into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions product_carousel_image_attachment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"category": "Tools",
"license": "AGPL-3",
"depends": ["website_sale"],
"data": ["data/scheduler.xml"],
"installable": True,
}
17 changes: 17 additions & 0 deletions product_carousel_image_attachment/data/scheduler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<!-- This cron is intended for the initial update of existing images, therefore is expected to
be disabled in normal circumstances. -->
<record model="ir.cron" id="resize_product_image">
<field name="name">Resize Extra Product Image</field>
<field name="model_id" ref="model_product_image" />
<field name="state">code</field>
<field name="code">model._cron_resize_product_image(1000)</field>
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="interval_type">days</field>
<field name="interval_type">hours</field>

<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="active" eval="False" />
</record>
</odoo>
1 change: 1 addition & 0 deletions product_carousel_image_attachment/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import ir_attachment
from . import product_image
23 changes: 21 additions & 2 deletions product_carousel_image_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo.tools import ImageProcess

IMAGE_TYPES = ["image/png", "image/jpeg", "image/bmp", "image/gif"]


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))
img = ImageProcess(datas, verify_resolution=False)
w, h = img.image.size
if w > nw or h > nh:
# Use odoo standard resize
img = img.resize(nw, nh)
return img.image_base64(quality=quality)
return datas

@api.model_create_multi
def create(self, vals_list):
attachments = super(IrAttachment, self).create(vals_list)
Expand All @@ -22,20 +40,21 @@ def create(self, vals_list):
]
and not attachment.res_field
):
resized_image = self._resize_image(attachment.datas)
vals = {}
# assignment for pt and p
if attachment.res_model == "product.template":
pt = self.env["product.template"].browse(attachment.res_id)
vals = {
"name": attachment.name,
"image_1920": attachment.datas,
"image_1920": resized_image,
"product_tmpl_id": pt.id,
}
if attachment.res_model == "product.product":
p = self.env["product.product"].browse(attachment.res_id)
vals = {
"name": attachment.name,
"image_1920": attachment.datas,
"image_1920": resized_image,
"product_variant_id": p.id,
}
self.env["product.image"].sudo().create(vals)
Expand Down
20 changes: 20 additions & 0 deletions product_carousel_image_attachment/models/product_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class ProductImage(models.Model):
_inherit = "product.image"

resize_done = fields.Boolean()

@api.model
def _cron_resize_product_image(self, limit):
images = self.sudo().search(
[("can_image_1024_be_zoomed", "=", True), ("resize_done", "=", False)],
limit=limit,
)
for image in images:
image.image_1920 = self.env["ir.attachment"]._resize_image(image.image_1920)
image.resize_done = True
Loading