[FIX] attachement_indexation: make pdfminer optional

As pdfminer does not have a Debian package in Ubuntu Bionic, it cannot
be declared as a strong requirement.

With this commit, a warning is logged if the library is not installed.
It does not prevent to index other types of documents.

closes odoo/odoo#44327

Signed-off-by: Christophe Monniez (moc) <moc@odoo.com>
This commit is contained in:
Christophe Monniez
2020-01-31 14:37:56 +00:00
parent 2ac06038dd
commit 6fa4dbf204
4 changed files with 23 additions and 5 deletions
+3 -1
View File
@@ -8,7 +8,9 @@
Attachments list and document indexation Attachments list and document indexation
======================================== ========================================
* Show attachment on the top of the forms * Show attachment on the top of the forms
* Document Indexation: odt * Document Indexation: odt, pdf, xlsx, docx
The `pdfminer.six` Python library has to be installed in order to index PDF files
""", """,
'depends': ['web'], 'depends': ['web'],
'installable': True, 'installable': True,
@@ -4,15 +4,23 @@ import io
import logging import logging
import xml.dom.minidom import xml.dom.minidom
import zipfile import zipfile
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.pdfpage import PDFPage
from odoo import api, models from odoo import api, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try:
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.pdfpage import PDFPage
except ImportError:
PDFResourceManager = PDFPageInterpreter = TextConverter = PDFPage = None
_logger.warning("Attachment indexation of PDF documents is unavailable because the 'pdfminer' Python library cannot be found on the system. "
"You may install it from https://pypi.org/project/pdfminer.six/ (e.g. `pip3 install pdfminer.six`)")
FTYPES = ['docx', 'pptx', 'xlsx', 'opendoc', 'pdf'] FTYPES = ['docx', 'pptx', 'xlsx', 'opendoc', 'pdf']
def textToString(element): def textToString(element):
buff = u"" buff = u""
for node in element.childNodes: for node in element.childNodes:
@@ -93,6 +101,8 @@ class IrAttachment(models.Model):
def _index_pdf(self, bin_data): def _index_pdf(self, bin_data):
'''Index PDF documents''' '''Index PDF documents'''
if PDFResourceManager is None:
return
buf = u"" buf = u""
if bin_data.startswith(b'%PDF-'): if bin_data.startswith(b'%PDF-'):
f = io.BytesIO(bin_data) f = io.BytesIO(bin_data)
@@ -1,14 +1,21 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from odoo.tests.common import TransactionCase, tagged from odoo.tests.common import TransactionCase, tagged
from unittest import skipIf
import os import os
directory = os.path.dirname(__file__) directory = os.path.dirname(__file__)
try:
from pdfminer.pdfinterp import PDFResourceManager
except ImportError:
PDFResourceManager = None
@tagged('post_install', '-at_install') @tagged('post_install', '-at_install')
class TestCaseIndexation(TransactionCase): class TestCaseIndexation(TransactionCase):
@skipIf(PDFResourceManager is None, "pdfminer not installed")
def test_attachment_pdf_indexation(self): def test_attachment_pdf_indexation(self):
with open(os.path.join(directory, 'files', 'test_content.pdf'), 'rb') as file: with open(os.path.join(directory, 'files', 'test_content.pdf'), 'rb') as file:
pdf = file.read() pdf = file.read()
-1
View File
@@ -21,7 +21,6 @@ mock==2.0.0
num2words==0.5.6 num2words==0.5.6
ofxparse==0.19 ofxparse==0.19
passlib==1.7.1 passlib==1.7.1
pdfminer.six==20181108
Pillow==5.4.1 ; python_version < '3.7' or sys_platform != 'win32' Pillow==5.4.1 ; python_version < '3.7' or sys_platform != 'win32'
Pillow==6.1.0 ; sys_platform == 'win32' and python_version >= '3.7' Pillow==6.1.0 ; sys_platform == 'win32' and python_version >= '3.7'
polib==1.1.0 polib==1.1.0