8000 [15.0][ADD] attachment_image_resize by AungKoKoLin1997 · Pull Request #3327 · OCA/server-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[15.0][ADD] attachment_image_resize #3327

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

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
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
8000
109 changes: 109 additions & 0 deletions attachment_image_resize/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
=======================
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 resizes attachment images with customizable resolutions,
configurable per model.

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

**Table of contents**

.. contents::
:local:

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

If you want to apply the same resolution to multiple models:

- Go to Settings > Technical > Models.
- Select a model
- Set the Attachment Image Max Resolution field.

Note: If you want to resize the existing attachment images for your
resize models, run the 'Resize Attachment Image' scheduled action. Each
run will resize only 1,000 records to prevent prolonged processing
times. You will need to run the action multiple times until all your
records are resized.

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

Contributors
------------

- ``Quartile <https://www.quartile.co>``\ \_\_:

- Aung Ko Ko Lin
- Yoshi Tashiro

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.

.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px
:target: https://github.com/yostashiro
:alt: yostashiro
.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px
:target: https://github.com/aungkokolin1997
:alt: aungkokolin1997

Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-yostashiro| |maintainer-aungkokolin1997|

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
17 changes: 17 additions & 0 deletions attachment_image_resize/__manifest__.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).
{
"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/ir_model_views.xml",
],
"maintainers": ["yostashiro", "aungkokolin1997"],
"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 5D32 number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import ir_attachment
from . import ir_config_parameter
from . import ir_model
55 changes: 55 additions & 0 deletions attachment_image_resize/models/ir_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Quartile
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models

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


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

resize_done = fields.Boolean(
help="Indicates whether the resizing process has been run on this record. "
"Once selected, the record will be excluded from "
"the _cron_resize_attachment_image() target."
)

def _postprocess_contents(self, values):
self = self.with_context(
resize_target_model=values.get("res_model") or self.res_model
)
self.resize_done = True
return super()._postprocess_contents(values)

@api.model
def _cron_resize_attachment_image(self, limit):
model_list = (

Check warning on line 27 in attachment_image_resize/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_attachment.py#L27

Added line #L27 was not covered by tests
self.env["ir.model"]
.search([("attachment_image_max_resolution", "!=", False)])
.mapped("model")
)
if model_list:
attachments = self.sudo().search(

Check warning on line 33 in attachment_image_resize/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_attachment.py#L33

Added line #L33 was not covered by tests
[
("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:
values = {

Check warning on line 47 in attachment_image_resize/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_attachment.py#L47

Added line #L47 was not covered by tests
"datas": attachment.datas,
"mimetype": attachment.mimetype,
"res_model": attachment.res_model,
}
processed = attachment._postprocess_contents(values)

Check warning on line 52 in attachment_image_resize/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_attachment.py#L52

Added line #L52 was not covered by tests
if processed.get("datas") and processed["datas"] != attachment.datas:
attachment.write({"datas": processed["datas"]})
attachment.resize_done = True

Check warning on line 55 in attachment_image_resize/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_attachment.py#L54-L55

Added lines #L54 - L55 were not covered by tests
18 changes: 18 additions & 0 deletions attachment_image_resize/models/ir_config_parameter.py
Original file line number Diff line number Diff line change
@@ -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 IrConfigParameter(models.Model):
_inherit = "ir.config_parameter"

@api.model
def get_param(self, key, default=False):
res_model = self.env.context.get("resize_target_model")
if not res_model or key != "base.image_autoresize_max_px":
return super().get_param(key, default=default)
model_rec = self.env["ir.model"].search([("model", "=", res_model)], limit=1)
if model_rec.attachment_image_max_resolution:
return model_rec.attachment_image_max_resolution

Check warning on line 17 in attachment_image_resize/models/ir_config_parameter.py

View check run for this annotation

Codecov / codecov/patch

attachment_image_resize/models/ir_config_parameter.py#L17

Added line #L17 was not covered by tests
return super().get_param(key, default=default)
13 changes: 13 additions & 0 deletions attachment_image_resize/models/ir_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2025 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class IrModel(models.Model):
_inherit = "ir.model"

attachment_image_max_resolution = fields.Char(
help="This resolution will be applied to the resizing of images"
" for this model."
)
8 changes: 8 additions & 0 deletions attachment_image_resize/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
If you want to apply the same resolution to multiple models:
- Go to Settings > Technical > Models.
- Select a model
- Set the Attachment Image Max Resolution field.

Note: If you want to resize the existing attachment images for your resize models, run the 'Resize Attachment Image'
scheduled action. Each run will resize only 1,000 records to prevent prolonged processing times.
You will need to run the action multiple times until all your records are resized.
4 changes: 4 additions & 0 deletions attachment_image_resize/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin
* Yoshi Tashiro
3 changes: 3 additions & 0 deletions attachment_image_resize/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module resizes attachment images with customizable resolutions, configurable per model.

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