From 0a36c8cb3f807c2c72f15709d568209e7eb55800 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 19 Oct 2016 00:46:57 +0200 Subject: [PATCH 1/2] Use try/except when importing external libs Remove self.ensure_one() that has nothing to do in an api.model method --- .../wizard/account_invoice_import.py | 1 - .../wizard/account_invoice_import.py | 13 ++++++++----- account_invoice_zugferd/models/account_invoice.py | 14 +++++++++----- .../models/business_document_import.py | 7 +++++-- base_ubl/models/ubl.py | 7 +++++-- sale_order_import_csv/wizard/sale_order_import.py | 6 ++++-- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/account_invoice_import/wizard/account_invoice_import.py b/account_invoice_import/wizard/account_invoice_import.py index 1c85584456..8fcbe2a358 100644 --- a/account_invoice_import/wizard/account_invoice_import.py +++ b/account_invoice_import/wizard/account_invoice_import.py @@ -441,7 +441,6 @@ def _create_invoice(self, parsed_inv): @api.model def post_process_invoice(self, parsed_inv, invoice): - self.ensure_one() invoice.button_reset_taxes() # Force tax amount if necessary prec = self.env['decimal.precision'].precision_get('Account') diff --git a/account_invoice_import_invoice2data/wizard/account_invoice_import.py b/account_invoice_import_invoice2data/wizard/account_invoice_import.py index 6e23904332..0e7563e003 100644 --- a/account_invoice_import_invoice2data/wizard/account_invoice_import.py +++ b/account_invoice_import_invoice2data/wizard/account_invoice_import.py @@ -6,14 +6,17 @@ from openerp.exceptions import Warning as UserError import os from tempfile import mkstemp -import logging -from invoice2data.main import extract_data -from invoice2data.template import read_templates -from invoice2data.main import logger as loggeri2data import pkg_resources - +import logging logger = logging.getLogger(__name__) +try: + from invoice2data.main import extract_data + from invoice2data.template import read_templates + from invoice2data.main import logger as loggeri2data +except ImportError: + logger.warning('Cannot import invoice2data') + class AccountInvoiceImport(models.TransientModel): _inherit = 'account.invoice.import' diff --git a/account_invoice_zugferd/models/account_invoice.py b/account_invoice_zugferd/models/account_invoice.py index f105762a73..dc7d75333e 100644 --- a/account_invoice_zugferd/models/account_invoice.py +++ b/account_invoice_zugferd/models/account_invoice.py @@ -8,15 +8,19 @@ from openerp.tools import float_compare, float_is_zero, float_round from StringIO import StringIO from lxml import etree -from PyPDF2 import PdfFileWriter, PdfFileReader -from PyPDF2.generic import DictionaryObject, DecodedStreamObject,\ - NameObject, createStringObject, ArrayObject from tempfile import NamedTemporaryFile from datetime import datetime import logging -# from pprint import pprint - logger = logging.getLogger(__name__) + +try: + from PyPDF2 import PdfFileWriter, PdfFileReader + from PyPDF2.generic import DictionaryObject, DecodedStreamObject,\ + NameObject, createStringObject, ArrayObject +except ImportError: + logger.warning('Cannot import PyPDF2') + + ZUGFERD_LEVEL = 'comfort' ZUGFERD_FILENAME = 'ZUGFeRD-invoice.xml' diff --git a/base_business_document_import/models/business_document_import.py b/base_business_document_import/models/business_document_import.py index e69e0e0890..061a8a580f 100644 --- a/base_business_document_import/models/business_document_import.py +++ b/base_business_document_import/models/business_document_import.py @@ -5,15 +5,18 @@ from openerp import models, api, _ from openerp.tools import float_compare from openerp.exceptions import Warning as UserError -import PyPDF2 from lxml import etree from StringIO import StringIO import mimetypes from urlparse import urlparse import logging - logger = logging.getLogger(__name__) +try: + import PyPDF2 +except ImportError: + logger.warning('Cannot import PyPDF2') + class BusinessDocumentImport(models.AbstractModel): _name = 'business.document.import' diff --git a/base_ubl/models/ubl.py b/base_ubl/models/ubl.py index 6163aa4ddb..682340efdc 100644 --- a/base_ubl/models/ubl.py +++ b/base_ubl/models/ubl.py @@ -8,12 +8,15 @@ from lxml import etree from StringIO import StringIO from tempfile import NamedTemporaryFile -import PyPDF2 import mimetypes import logging - logger = logging.getLogger(__name__) +try: + import PyPDF2 +except ImportError: + logger.warning('Cannot import PyPDF2') + class BaseUbl(models.AbstractModel): _name = 'base.ubl' diff --git a/sale_order_import_csv/wizard/sale_order_import.py b/sale_order_import_csv/wizard/sale_order_import.py index df0198a30e..84e7e5657b 100644 --- a/sale_order_import_csv/wizard/sale_order_import.py +++ b/sale_order_import_csv/wizard/sale_order_import.py @@ -6,10 +6,12 @@ from openerp.exceptions import Warning as UserError from openerp.tools import float_compare from tempfile import TemporaryFile -import unicodecsv import logging - logger = logging.getLogger(__name__) +try: + import unicodecsv +except ImportError: + logger.warning('Cannot import unicodecsv') class SaleOrderImport(models.TransientModel): From d0e4c3aac8e14156386aa2cf46c4e17fb4e7efbd Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 19 Oct 2016 08:54:02 +0200 Subject: [PATCH 2/2] logger.warning -> logger.debug --- .../wizard/account_invoice_import.py | 2 +- account_invoice_zugferd/models/account_invoice.py | 2 +- .../models/business_document_import.py | 2 +- .../models/business_document_import.py | 2 +- base_ubl/models/ubl.py | 2 +- sale_order_import_csv/wizard/sale_order_import.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/account_invoice_import_invoice2data/wizard/account_invoice_import.py b/account_invoice_import_invoice2data/wizard/account_invoice_import.py index 0e7563e003..73de4b6220 100644 --- a/account_invoice_import_invoice2data/wizard/account_invoice_import.py +++ b/account_invoice_import_invoice2data/wizard/account_invoice_import.py @@ -15,7 +15,7 @@ from invoice2data.template import read_templates from invoice2data.main import logger as loggeri2data except ImportError: - logger.warning('Cannot import invoice2data') + logger.debug('Cannot import invoice2data') class AccountInvoiceImport(models.TransientModel): diff --git a/account_invoice_zugferd/models/account_invoice.py b/account_invoice_zugferd/models/account_invoice.py index dc7d75333e..62ce7a5973 100644 --- a/account_invoice_zugferd/models/account_invoice.py +++ b/account_invoice_zugferd/models/account_invoice.py @@ -18,7 +18,7 @@ from PyPDF2.generic import DictionaryObject, DecodedStreamObject,\ NameObject, createStringObject, ArrayObject except ImportError: - logger.warning('Cannot import PyPDF2') + logger.debug('Cannot import PyPDF2') ZUGFERD_LEVEL = 'comfort' diff --git a/base_business_document_import/models/business_document_import.py b/base_business_document_import/models/business_document_import.py index 061a8a580f..33ee3cdba4 100644 --- a/base_business_document_import/models/business_document_import.py +++ b/base_business_document_import/models/business_document_import.py @@ -15,7 +15,7 @@ try: import PyPDF2 except ImportError: - logger.warning('Cannot import PyPDF2') + logger.debug('Cannot import PyPDF2') class BusinessDocumentImport(models.AbstractModel): diff --git a/base_business_document_import_phone/models/business_document_import.py b/base_business_document_import_phone/models/business_document_import.py index 8205e5458a..cc3cce593c 100644 --- a/base_business_document_import_phone/models/business_document_import.py +++ b/base_business_document_import_phone/models/business_document_import.py @@ -8,7 +8,7 @@ try: import phonenumbers except ImportError: - logger.warning('Cannot import phonenumbers') + logger.debug('Cannot import phonenumbers') class BusinessDocumentImport(models.AbstractModel): diff --git a/base_ubl/models/ubl.py b/base_ubl/models/ubl.py index 682340efdc..4c9829b0c7 100644 --- a/base_ubl/models/ubl.py +++ b/base_ubl/models/ubl.py @@ -15,7 +15,7 @@ try: import PyPDF2 except ImportError: - logger.warning('Cannot import PyPDF2') + logger.debug('Cannot import PyPDF2') class BaseUbl(models.AbstractModel): diff --git a/sale_order_import_csv/wizard/sale_order_import.py b/sale_order_import_csv/wizard/sale_order_import.py index 84e7e5657b..bfc2c0fe4c 100644 --- a/sale_order_import_csv/wizard/sale_order_import.py +++ b/sale_order_import_csv/wizard/sale_order_import.py @@ -11,7 +11,7 @@ try: import unicodecsv except ImportError: - logger.warning('Cannot import unicodecsv') + logger.debug('Cannot import unicodecsv') class SaleOrderImport(models.TransientModel):