mirror of
https://github.com/OCA/knowledge.git
synced 2025-12-22 13:22:19 -06:00
[MIG] attachment_preview: Migration to 11.0
[FIX] Lint [FIX] lint and flake [ADD] tests [ADD] tests [ADD] tests [ADD] Tests [ADD] Package python-magic [ADD] Tests [FIX] Lint
This commit is contained in:
4
attachment_preview/models/__init__.py
Normal file
4
attachment_preview/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
# Copyright 2014 Therp BV (<http://therp.nl>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from . import ir_attachment
|
||||
75
attachment_preview/models/ir_attachment.py
Normal file
75
attachment_preview/models/ir_attachment.py
Normal file
@@ -0,0 +1,75 @@
|
||||
# Copyright 2014 Therp BV (<http://therp.nl>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
import collections
|
||||
import logging
|
||||
import mimetypes
|
||||
import os.path
|
||||
|
||||
from odoo import models, api
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class IrAttachment(models.Model):
|
||||
_inherit = 'ir.attachment'
|
||||
|
||||
@api.model
|
||||
def get_binary_extension(self, model, ids, binary_field,
|
||||
filename_field=None):
|
||||
result = {}
|
||||
ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids]
|
||||
|
||||
# First pass: load fields in bin_size mode to avoid loading big files
|
||||
# unnecessarily.
|
||||
if filename_field:
|
||||
for this in self.env[model].with_context(
|
||||
bin_size=True).browse(ids_to_browse):
|
||||
if not this.id:
|
||||
result[this.id] = False
|
||||
continue
|
||||
extension = ''
|
||||
if this[filename_field]:
|
||||
filename, extension = os.path.splitext(
|
||||
this[filename_field])
|
||||
if this[binary_field] and extension:
|
||||
result[this.id] = extension
|
||||
_logger.debug('Got extension %s from filename %s',
|
||||
extension, this[filename_field])
|
||||
# Second pass for all attachments which have to be loaded fully
|
||||
# to get the extension from the content
|
||||
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):
|
||||
if this[binary_field] is None:
|
||||
result[this.id] = False
|
||||
continue
|
||||
try:
|
||||
import magic
|
||||
if model == self._name and binary_field == 'datas'\
|
||||
and this.store_fname:
|
||||
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:
|
||||
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], strict=False)
|
||||
_logger.debug('Mimetypes guessed type %s from buffer',
|
||||
mimetype)
|
||||
extension = mimetypes.guess_extension(
|
||||
mimetype.split(';')[0], strict=False)
|
||||
result[this.id] = extension
|
||||
for _id in result:
|
||||
result[_id] = (result[_id] or '').lstrip('.').lower()
|
||||
return result if isinstance(ids, collections.Iterable) else result[ids]
|
||||
|
||||
@api.model
|
||||
def get_attachment_extension(self, ids):
|
||||
return self.get_binary_extension(
|
||||
self._name, ids, 'datas', 'datas_fname')
|
||||
Reference in New Issue
Block a user