Skip to content

Commit

Permalink
[ADD] attachment_image_resize
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Nov 27, 2024
1 parent 7e6465e commit 832a42d
Show file tree
Hide file tree
Showing 14 changed files with 737 additions and 0 deletions.
81 changes: 81 additions & 0 deletions attachment_image_resize/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
=======================
Attachment Image Resize
=======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:afd5bd822649039a3eba99ddd8344fb683601e768d0478766d341d83c968890c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/15.0/attachment_image_resize
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-15-0/server-tools-15-0-attachment_image_resize
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module can resize attachment images with a customizable resolution for specific models, configurable per company.

Note: If the original image resolution is smaller than the customized resolution, it will not be resized.

**Table of contents**

.. contents::
:local:

Configuration
=============

#. Go to General Settings > Attachment Resize and enter the models you want to resize attachment images for in the 'Attachment Image Resize Models'.
#. In the same section, enter the maximum resolution you want to use for resizing attachment images in the 'Attachment Image Max Resolution'.

Note: If you want to resize the existing old attachment images for your resize models, run the 'Resize Attachment Image' scheduled action.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20attachment_image_resize%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Quartile

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/15.0/attachment_image_resize>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions attachment_image_resize/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions attachment_image_resize/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Attachment Image Resize",
"version": "15.0.1.0.0",
"author": "Quartile, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-tools",
"category": "Tools",
"license": "AGPL-3",
"depends": ["base_setup"],
"data": ["data/scheduler.xml", "views/res_config_settings_views.xml"],
"installable": True,
}
17 changes: 17 additions & 0 deletions attachment_image_resize/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_attachment_image">
<field name="name">Resize Attachment Image</field>
<field name="model_id" ref="model_ir_attachment" />
<field name="state">code</field>
<field name="code">model._cron_resize_attachment_image(1000)</field>
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="active" eval="False" />
</record>
</odoo>
3 changes: 3 additions & 0 deletions attachment_image_resize/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import res_company
from . import res_config_settings
from . import ir_attachment
93 changes: 93 additions & 0 deletions attachment_image_resize/models/ir_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import io
import logging

from PIL import Image

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

_logger = logging.getLogger(__name__)

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


class IrAttachment(models.Model):
_inherit = "ir.attachment"

resize_done = fields.Boolean()

@api.model
def _resize_image(self, datas, is_raw=False):
ICP = self.env["ir.config_parameter"].sudo().get_param
max_resolution = self.env.company.attachment_image_max_resolution or "1920x1920"
max_width, max_height = map(int, max_resolution.split("x"))
quality = int(ICP("base.image_autoresize_quality", 80))
try:
# Use odoo standard resize
if is_raw:
img = ImageProcess(False, verify_resolution=False)
img.image = Image.open(io.BytesIO(datas))
img.original_format = (img.image.format or "").upper()
else:
img = ImageProcess(datas, verify_resolution=False)
width, height = img.image.size
if width > max_width or height > max_height:
img = img.resize(max_width, max_height)
return (
img.image_quality(quality=quality)
if is_raw
else img.image_base64(quality=quality)
)
except Exception as e:
_logger.warning(f"Failed to resize image: {e}")
return datas
return datas

@api.model
def _cron_resize_attachment_image(self, limit):
models = self.env.company.attachment_image_resize_models
model_list = models.split(",") if models else []
if model_list:
attachments = self.sudo().search(
[
("res_model", "in", model_list),
("mimetype", "in", IMAGE_TYPES),
("resize_done", "=", False),
# Added this filter because the default search only
# retrieves records with no res_field.
"|",
("res_field", "=", False),
("res_field", "!=", False),
],
limit=limit,
)
for attachment in attachments:
attachment.datas = self._resize_image(attachment.datas)
attachments.resize_done = True
if len(attachments) == limit:
self.env.ref(
"attachment_image_resize.resize_attachment_image"
)._trigger()

@api.model_create_multi
def create(self, vals_list):
# here we resize the image first to avoid bloating the filestore
for values in vals_list:
res_model = values.get("res_model")
models = self.env.company.attachment_image_resize_models
mimetype = values.get("mimetype") or self._compute_mimetype(values)
if (
res_model
and models
and res_model in models.split(",")
and mimetype in IMAGE_TYPES
):
# Resize raw binary or Base64 data
if "raw" in values and values["raw"]:
values["raw"] = self._resize_image(values["raw"], is_raw=True)
elif "datas" in values and values["datas"]:
values["datas"] = self._resize_image(values["datas"], is_raw=False)
return super(IrAttachment, self).create(vals_list)
17 changes: 17 additions & 0 deletions attachment_image_resize/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

attachment_image_resize_models = fields.Char(
help="When users attach images to these models, the images will be resized "
"based on the maximum resolution specified for attachments."
)
attachment_image_max_resolution = fields.Char(
help="This resolution will be applied to the resizing of images"
" for the specified models."
)
21 changes: 21 additions & 0 deletions attachment_image_resize/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

attachment_image_resize_models = fields.Char(
related="company_id.attachment_image_resize_models",
readonly=False,
help="When users attach images to these models, the images will be resized "
"based on the maximum resolution specified for attachments.",
)
attachment_image_max_resolution = fields.Char(
related="company_id.attachment_image_max_resolution",
readonly=False,
help="This resolution will be applied to the resizing of images"
" for the specified models.",
)
4 changes: 4 additions & 0 deletions attachment_image_resize/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#. Go to General Settings > Attachment Resize and enter the models you want to resize attachment images for in the 'Attachment Image Resize Models'.
#. In the same section, enter the maximum resolution you want to use for resizing attachment images in the 'Attachment Image Max Resolution'.

Note: If you want to resize the existing old attachment images for your resize models, run the 'Resize Attachment Image' scheduled action.
3 changes: 3 additions & 0 deletions attachment_image_resize/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module can resize attachment images with a customizable resolution for specific models, configurable per company.

Note: If the original image resolution is smaller than the customized resolution, it will not be resized.
Loading

0 comments on commit 832a42d

Please sign in to comment.