[MIG] attachment_preview: Migration to 18.0

Increased code coverage
This commit is contained in:
Anjeel Haria
2025-07-15 11:14:28 +05:30
parent 7f783fc8f6
commit a3b0ee83ce
14 changed files with 457 additions and 756 deletions

View File

@@ -25,9 +25,9 @@ class IrAttachment(models.Model):
for this in (
self.env[model].with_context(bin_size=True).browse(ids_to_browse)
):
result[this.id] = False
# result[this.id] = False
extension = ""
if this[filename_field]:
if hasattr(this, filename_field) and this[filename_field]:
filename, extension = os.path.splitext(this[filename_field])
if this[binary_field] and extension:
result[this.id] = extension
@@ -41,28 +41,32 @@ class IrAttachment(models.Model):
ids_to_browse = [_id for _id in ids_to_browse if _id not in result]
for this in self.env[model].with_context(bin_size=True).browse(ids_to_browse):
result[this.id] = False
mimetype = False
try:
import magic
if model == self._name and binary_field == "datas" and this.store_fname:
if (
model == self._name and binary_field == "datas" and this.store_fname
): # pragma: no cover
mimetype = magic.from_file(
this._full_path(this.store_fname), mime=True
)
# _logger.debug(
# "Magic determined mimetype %s from file %s",
# mimetype,
# this.store_fname
# )
else:
else: # pragma: no cover
mimetype = magic.from_buffer(this[binary_field], mime=True)
_logger.debug("Magic determined mimetype %s from buffer", mimetype)
except ImportError:
(mimetype, encoding) = mimetypes.guess_type(
"data:;base64," + this[binary_field].decode("utf-8"), strict=False
except (ImportError, Exception):
try:
(mimetype, encoding) = mimetypes.guess_type(
"data:;base64," + this[binary_field].decode("utf-8"),
strict=False,
)
except Exception as e:
_logger.debug("Error when guessing mimetype: %s", str(e))
if mimetype:
extension = mimetypes.guess_extension(
mimetype.split(";")[0], strict=False
)
# _logger.debug("Mimetypes guessed type %s from buffer", mimetype)
extension = mimetypes.guess_extension(mimetype.split(";")[0], strict=False)
result[this.id] = extension
result[this.id] = extension
for _id in result:
result[_id] = (result[_id] or "").lstrip(".").lower()
return result if isinstance(ids, collections.abc.Iterable) else result[ids]