Skip to content

Commit

Permalink
[MIG] storage_thumbnail: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JasminSForgeFlow committed Jan 19, 2024
1 parent 2c5596c commit a19cc59
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion storage_thumbnail/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Storage Thumbnail",
"summary": "Abstract module that add the possibility to have thumbnail",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Storage",
"website": "https://github.com/OCA/storage",
"author": " Akretion, Odoo Community Association (OCA)",
Expand Down
21 changes: 11 additions & 10 deletions storage_thumbnail/models/storage_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests

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

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,9 +56,9 @@ def _resize(self, image, size_x, size_y, fmt):
if image_resize_server and image.backend_id.served_by != "odoo":
values = {"url": image.url, "width": size_x, "height": size_y, "fmt": fmt}
url = image_resize_server.format(**values)
return base64.encodebytes(requests.get(url).content)
image_process = ImageProcess(image.data)
return image_process.resize(max_width=size_x, max_height=size_y).image_base64()
return base64.encodebytes(requests.get(url, timeout=15).content)
image_processed = base64.b64encode(image_process(image.data))
return image_processed

def _get_default_backend_id(self):
"""Choose the correct backend.
Expand All @@ -70,12 +70,13 @@ def _get_default_backend_id(self):
self.env, "storage.thumbnail.backend_id"
)

@api.model
def create(self, vals):
vals["file_type"] = self._default_file_type
if "backend_id" not in vals:
vals["backend_id"] = self._get_default_backend_id()
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
vals["file_type"] = self._default_file_type
if "backend_id" not in vals:
vals["backend_id"] = self._get_default_backend_id()
return super().create(vals_list)

def unlink(self):
files = self.mapped("file_id")
Expand Down
13 changes: 7 additions & 6 deletions storage_thumbnail/models/thumbnail_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_or_create_thumbnail(self, size_x, size_y, url_key=None):
# storage.thumbnail is not defined as a one2many to this mixin.
# As consequence, the ORM is not able to trigger the invalidation
# of thumbnail_ids on our mixin
self.thumbnail_ids.refresh()
self.thumbnail_ids.invalidate_recordset()
return thumbnail

def generate_odoo_thumbnail(self):
Expand All @@ -117,8 +117,9 @@ def generate_odoo_thumbnail(self):
self_sudo._get_medium_thumbnail()
return True

@api.model
def create(self, vals):
record = super().create(vals)
record.generate_odoo_thumbnail()
return record
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for record in records:
record.generate_odoo_thumbnail()
return records
13 changes: 7 additions & 6 deletions storage_thumbnail/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ def _get_backend_id(self):
self.env["ir.config_parameter"].get_param("storage.thumbnail.backend_id")
)

@api.model
def create(self, vals):
vals["file_type"] = "thumbnail"
if "backend_id" not in vals:
vals.update({"backend_id": self._get_backend_id()})
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
vals["file_type"] = "thumbnail"
if "backend_id" not in vals:
vals.update({"backend_id": self._get_backend_id()})
return super().create(vals_list)
2 changes: 1 addition & 1 deletion storage_thumbnail/tests/test_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_urls(self):
self.assertRecordValues(images, expected)
# Unless we enforce it
image1.backend_id.backend_view_use_internal_url = True
images.invalidate_cache()
images.invalidate_recordset()
expected = [
{
"url": f"{cdn}/akretion-logo-{image1.file_id.id}.png",
Expand Down

0 comments on commit a19cc59

Please sign in to comment.