From 832a42d051f289ab7e26963422ec905709428b82 Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Tue, 26 Nov 2024 08:07:32 +0000 Subject: [PATCH] [ADD] attachment_image_resize --- attachment_image_resize/README.rst | 81 ++++ attachment_image_resize/__init__.py | 1 + attachment_image_resize/__manifest__.py | 13 + attachment_image_resize/data/scheduler.xml | 17 + attachment_image_resize/models/__init__.py | 3 + .../models/ir_attachment.py | 93 ++++ attachment_image_resize/models/res_company.py | 17 + .../models/res_config_settings.py | 21 + attachment_image_resize/readme/CONFIGURE.rst | 4 + .../readme/DESCRIPTION.rst | 3 + .../static/description/index.html | 423 ++++++++++++++++++ .../views/res_config_settings_views.xml | 54 +++ .../odoo/addons/attachment_image_resize | 1 + setup/attachment_image_resize/setup.py | 6 + 14 files changed, 737 insertions(+) create mode 100644 attachment_image_resize/README.rst create mode 100644 attachment_image_resize/__init__.py create mode 100644 attachment_image_resize/__manifest__.py create mode 100644 attachment_image_resize/data/scheduler.xml create mode 100644 attachment_image_resize/models/__init__.py create mode 100644 attachment_image_resize/models/ir_attachment.py create mode 100644 attachment_image_resize/models/res_company.py create mode 100644 attachment_image_resize/models/res_config_settings.py create mode 100644 attachment_image_resize/readme/CONFIGURE.rst create mode 100644 attachment_image_resize/readme/DESCRIPTION.rst create mode 100644 attachment_image_resize/static/description/index.html create mode 100644 attachment_image_resize/views/res_config_settings_views.xml create mode 120000 setup/attachment_image_resize/odoo/addons/attachment_image_resize create mode 100644 setup/attachment_image_resize/setup.py diff --git a/attachment_image_resize/README.rst b/attachment_image_resize/README.rst new file mode 100644 index 0000000..8a3a2d6 --- /dev/null +++ b/attachment_image_resize/README.rst @@ -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 `_. +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 `_. + +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 `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/attachment_image_resize/__init__.py b/attachment_image_resize/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/attachment_image_resize/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/attachment_image_resize/__manifest__.py b/attachment_image_resize/__manifest__.py new file mode 100644 index 0000000..c278126 --- /dev/null +++ b/attachment_image_resize/__manifest__.py @@ -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, +} diff --git a/attachment_image_resize/data/scheduler.xml b/attachment_image_resize/data/scheduler.xml new file mode 100644 index 0000000..4ac7ebc --- /dev/null +++ b/attachment_image_resize/data/scheduler.xml @@ -0,0 +1,17 @@ + + + + + Resize Attachment Image + + code + model._cron_resize_attachment_image(1000) + + 1 + hours + -1 + + + + diff --git a/attachment_image_resize/models/__init__.py b/attachment_image_resize/models/__init__.py new file mode 100644 index 0000000..731c1f7 --- /dev/null +++ b/attachment_image_resize/models/__init__.py @@ -0,0 +1,3 @@ +from . import res_company +from . import res_config_settings +from . import ir_attachment diff --git a/attachment_image_resize/models/ir_attachment.py b/attachment_image_resize/models/ir_attachment.py new file mode 100644 index 0000000..4a0711b --- /dev/null +++ b/attachment_image_resize/models/ir_attachment.py @@ -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) diff --git a/attachment_image_resize/models/res_company.py b/attachment_image_resize/models/res_company.py new file mode 100644 index 0000000..003f25c --- /dev/null +++ b/attachment_image_resize/models/res_company.py @@ -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." + ) diff --git a/attachment_image_resize/models/res_config_settings.py b/attachment_image_resize/models/res_config_settings.py new file mode 100644 index 0000000..3a2753b --- /dev/null +++ b/attachment_image_resize/models/res_config_settings.py @@ -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.", + ) diff --git a/attachment_image_resize/readme/CONFIGURE.rst b/attachment_image_resize/readme/CONFIGURE.rst new file mode 100644 index 0000000..6594466 --- /dev/null +++ b/attachment_image_resize/readme/CONFIGURE.rst @@ -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. diff --git a/attachment_image_resize/readme/DESCRIPTION.rst b/attachment_image_resize/readme/DESCRIPTION.rst new file mode 100644 index 0000000..4879d3f --- /dev/null +++ b/attachment_image_resize/readme/DESCRIPTION.rst @@ -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. diff --git a/attachment_image_resize/static/description/index.html b/attachment_image_resize/static/description/index.html new file mode 100644 index 0000000..200d6d5 --- /dev/null +++ b/attachment_image_resize/static/description/index.html @@ -0,0 +1,423 @@ + + + + + +Attachment Image Resize + + + +
+

Attachment Image Resize

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

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

+ +
+

Configuration

+
    +
  1. Go to General Settings > Attachment Resize and enter the models you want to resize attachment images for in the ‘Attachment Image Resize Models’.
  2. +
  3. In the same section, enter the maximum resolution you want to use for resizing attachment images in the ‘Attachment Image Max Resolution’.
  4. +
+

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. +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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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 project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/attachment_image_resize/views/res_config_settings_views.xml b/attachment_image_resize/views/res_config_settings_views.xml new file mode 100644 index 0000000..22e0ffe --- /dev/null +++ b/attachment_image_resize/views/res_config_settings_views.xml @@ -0,0 +1,54 @@ + + + + res.config.settings + res.config.settings + + + +
+

Attachment Resize

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/setup/attachment_image_resize/odoo/addons/attachment_image_resize b/setup/attachment_image_resize/odoo/addons/attachment_image_resize new file mode 120000 index 0000000..d74b1c7 --- /dev/null +++ b/setup/attachment_image_resize/odoo/addons/attachment_image_resize @@ -0,0 +1 @@ +../../../../attachment_image_resize \ No newline at end of file diff --git a/setup/attachment_image_resize/setup.py b/setup/attachment_image_resize/setup.py new file mode 100644 index 0000000..28c57bb --- /dev/null +++ b/setup/attachment_image_resize/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)