From e3b2f67deccd0255cd5dac038903476740a04d02 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 11 Jan 2018 14:57:10 +0100 Subject: [PATCH] Finish migration to version 10.0 Do not raise ConnectionError if we get a response Use ConnectionError from requests lib instead of overriding it Fix push_to_pingen (writes on state as for cron), remove unneeded loops --- pingen/README.rst | 2 +- pingen/__init__.py | 5 +- pingen/__manifest__.py | 10 +- pingen/data/pingen_data.xml | 32 +++++ pingen/ir_attachment_view.xml | 31 ----- pingen/models/__init__.py | 8 ++ pingen/models/base_config_settings.py | 11 ++ pingen/{ => models}/ir_attachment.py | 14 +-- pingen/{ => models}/pingen.py | 10 -- pingen/{ => models}/pingen_document.py | 151 ++++++++++++----------- pingen/{ => models}/res_company.py | 0 pingen/pingen_data.xml | 32 ----- pingen/pingen_document_view.xml | 161 ------------------------- pingen/res_company_view.xml | 19 --- pingen/views/base_config_settings.xml | 16 +++ pingen/views/ir_attachment_view.xml | 27 +++++ pingen/views/pingen_document_view.xml | 160 ++++++++++++++++++++++++ 17 files changed, 342 insertions(+), 347 deletions(-) create mode 100644 pingen/data/pingen_data.xml delete mode 100644 pingen/ir_attachment_view.xml create mode 100644 pingen/models/__init__.py create mode 100644 pingen/models/base_config_settings.py rename pingen/{ => models}/ir_attachment.py (89%) rename pingen/{ => models}/pingen.py (95%) rename pingen/{ => models}/pingen_document.py (75%) rename pingen/{ => models}/res_company.py (100%) delete mode 100644 pingen/pingen_data.xml delete mode 100644 pingen/pingen_document_view.xml delete mode 100644 pingen/res_company_view.xml create mode 100644 pingen/views/base_config_settings.xml create mode 100644 pingen/views/ir_attachment_view.xml create mode 100644 pingen/views/pingen_document_view.xml diff --git a/pingen/README.rst b/pingen/README.rst index 14a55b8..55781eb 100644 --- a/pingen/README.rst +++ b/pingen/README.rst @@ -88,9 +88,9 @@ Credits Contributors ============ -* Yannick Vaucher * Guewen Baconnier * Anar Baghirli +* Akim Juillerat Maintainer ========== diff --git a/pingen/__init__.py b/pingen/__init__.py index e26b570..d93cc0f 100644 --- a/pingen/__init__.py +++ b/pingen/__init__.py @@ -3,7 +3,4 @@ # Copyright 2012-2017 Camptocamp SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from . import ir_attachment -from . import pingen -from . import pingen_document -from . import res_company +from . import models \ No newline at end of file diff --git a/pingen/__manifest__.py b/pingen/__manifest__.py index 98523bd..d292266 100644 --- a/pingen/__manifest__.py +++ b/pingen/__manifest__.py @@ -11,16 +11,16 @@ 'license': 'AGPL-3', 'category': 'Reporting', 'complexity': 'easy', - 'depends': [], + 'depends': ['base_setup'], 'external_dependencies': { 'python': ['requests'], }, 'website': 'http://www.camptocamp.com', 'data': [ - 'ir_attachment_view.xml', - 'pingen_document_view.xml', - 'pingen_data.xml', - 'res_company_view.xml', + 'views/ir_attachment_view.xml', + 'views/pingen_document_view.xml', + 'data/pingen_data.xml', + 'views/base_config_settings.xml', 'security/ir.model.access.csv', ], 'tests': [], diff --git a/pingen/data/pingen_data.xml b/pingen/data/pingen_data.xml new file mode 100644 index 0000000..5c0572d --- /dev/null +++ b/pingen/data/pingen_data.xml @@ -0,0 +1,32 @@ + + + + + + Run Pingen Document Push + + + 1 + hours + -1 + + pingen.document + _push_and_send_to_pingen_cron + () + + + + Run Pingen Document Update + + + 1 + days + -1 + + pingen.document + _update_post_infos_cron + () + + + + diff --git a/pingen/ir_attachment_view.xml b/pingen/ir_attachment_view.xml deleted file mode 100644 index 24e2335..0000000 --- a/pingen/ir_attachment_view.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - ir.attachment.pingen.view - ir.attachment - form - - - - - - - - - - - - - - - - - diff --git a/pingen/models/__init__.py b/pingen/models/__init__.py new file mode 100644 index 0000000..5cce2da --- /dev/null +++ b/pingen/models/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import ir_attachment +from . import pingen +from . import pingen_document +from . import res_company +from . import base_config_settings diff --git a/pingen/models/base_config_settings.py b/pingen/models/base_config_settings.py new file mode 100644 index 0000000..c1d43c1 --- /dev/null +++ b/pingen/models/base_config_settings.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class BaseConfigSettings(models.TransientModel): + _inherit = 'base.config.settings' + + pingen_token = fields.Char(related='company_id.pingen_token') + pingen_staging = fields.Boolean(related='company_id.pingen_staging') diff --git a/pingen/ir_attachment.py b/pingen/models/ir_attachment.py similarity index 89% rename from pingen/ir_attachment.py rename to pingen/models/ir_attachment.py index 423230c..bb4e8b8 100644 --- a/pingen/ir_attachment.py +++ b/pingen/models/ir_attachment.py @@ -24,8 +24,7 @@ class IrAttachment(models.Model): pingen_speed = fields.Selection( [('1', 'Priority'), ('2', 'Economy')], 'Speed', default='2', - help='Defines the sending speed if ' + - 'the document is automatically sent') + help='Defines the sending speed if the document is automatically sent') pingen_color = fields.Selection([('0', 'B/W'), ('1', 'Color')], 'Type of print', default='0') @@ -62,17 +61,16 @@ class IrAttachment(models.Model): if document: if document.state == 'pushed': raise UserError( - _('Error. The attachment ' + - '%s is already pushed to pingen.com.') % - self.name) + _('Error. The attachment %s is ' + 'already pushed to pingen.com.') % self.name) document.write({'state': 'canceled'}) return def create(self, vals): - attachment_id = super(IrAttachment, self).create(vals) + attachment = super(IrAttachment, self).create(vals) if 'send_to_pingen' in vals: - attachment_id._handle_pingen_document() - return attachment_id + attachment._handle_pingen_document() + return attachment def write(self, vals): res = super(IrAttachment, self).write(vals) diff --git a/pingen/pingen.py b/pingen/models/pingen.py similarity index 95% rename from pingen/pingen.py rename to pingen/models/pingen.py index 3de6e20..0cb5964 100644 --- a/pingen/pingen.py +++ b/pingen/models/pingen.py @@ -8,7 +8,6 @@ import logging import urlparse import json import pytz -import base64 from datetime import datetime from requests.packages.urllib3.filepost import encode_multipart_formdata @@ -47,10 +46,6 @@ class PingenException(RuntimeError): request.""" -class ConnectionError(PingenException): - """An Error occured with the pingen API""" - - class APIError(PingenException): """An Error occured with the pingen API""" @@ -117,11 +112,6 @@ class Pingen(object): response = method(complete_url, **kwargs) - if not response.ok: - raise ConnectionError( - "%s: %s" % (response.json()['errorcode'], - response.json()['errormessage'])) - if response.json()['error']: raise APIError( "%s: %s" % (response.json()['errorcode'], diff --git a/pingen/pingen_document.py b/pingen/models/pingen_document.py similarity index 75% rename from pingen/pingen_document.py rename to pingen/models/pingen_document.py index eed4a3d..ccdcb09 100644 --- a/pingen/pingen_document.py +++ b/pingen/models/pingen_document.py @@ -4,15 +4,14 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import logging - +from requests.exceptions import ConnectionError from cStringIO import StringIO -# from contextlib import closing +import odoo from odoo import models, fields, _ from odoo.exceptions import UserError -import odoo -from .pingen import APIError, ConnectionError, \ - POST_SENDING_STATUS, pingen_datetime_to_utc +from .pingen import APIError, pingen_datetime_to_utc, POST_SENDING_STATUS + _logger = logging.getLogger(__name__) @@ -119,27 +118,37 @@ class PingenDocument(models.Model): instance) for public interface. """ self.ensure_one() - for document in self: - try: - session = self._get_pingen_session() - document._push_to_pingen(pingen=session) - except ConnectionError as e: - raise UserError( - _('Connection Error when asking for ' + - 'sending the document %s to Pingen') - % document.name) - except APIError as e: - raise UserError( - _('Error when asking Pingen to send the document %s: ' - '\n%s') % (document.name, e)) - except Exception as e: - _logger.exception( - 'Unexcepted Error when updating ' + - 'the status of pingen.document %s: ' % - document.id) - raise UserError( - _('Unexcepted Error when updating the ' + - 'status of Document %s') % document.name) + state = False + error_msg = False + try: + session = self._get_pingen_session() + self._push_to_pingen(pingen=session) + except ConnectionError as e: + state = 'error' + error_msg = _('Connection Error when asking for ' + 'sending the document %s to Pingen') % self.name + except APIError as e: + state = 'pingen_error' + error_msg = _('Error when asking Pingen to send the document %s: ' + '\n%s') % (self.name, e) + except Exception as e: + _logger.exception( + 'Unexpected Error when updating ' + + 'the status of pingen.document %s: ' % + self.id) + error_msg = _('Unexpected Error when updating the ' + 'status of Document %s') % self.name + finally: + if error_msg: + vals = {'last_error_message': error_msg} + if state: + vals.update({'state': state}) + with odoo.registry(self.env.cr.dbname).cursor() as new_cr: + new_env = odoo.api.Environment( + new_cr, self.env.uid, self.env.context) + self.with_env(new_env).write(vals) + + raise UserError(error_msg) return True def _push_and_send_to_pingen_cron(self): @@ -172,7 +181,7 @@ class PingenDocument(models.Model): document.write({'last_error_message': e, 'state': 'pingen_error'}) except BaseException as e: - _logger.error('Unexcepted error in pingen cron') + _logger.error('Unexpected error in pingen cron') return True def _resolve_error(self): @@ -232,28 +241,26 @@ class PingenDocument(models.Model): instance) for public interface. """ self.ensure_one() - for document in self: - try: - session = document._get_pingen_session() - document._ask_pingen_send(pingen=session) - except ConnectionError as e: - raise UserError( - _('Connection Error when asking for ' - 'sending the document %s to Pingen') % document.name) + try: + session = self._get_pingen_session() + self._ask_pingen_send(pingen=session) + except ConnectionError as e: + raise UserError( + _('Connection Error when asking for ' + 'sending the document %s to Pingen') % self.name) - except APIError as e: - raise UserError( - _('Error when asking Pingen to send the document %s: ' - '\n%s') % (document.name, e)) + except APIError as e: + raise UserError( + _('Error when asking Pingen to send the document %s: ' + '\n%s') % (self.name, e)) - except BaseException as e: - _logger.exception( - 'Unexcepted Error when updating the ' + - 'status of pingen.document %s: ' % - document.id) - raise UserError( - _('Unexcepted Error when updating the ' + - 'status of Document %s') % document.name) + except BaseException as e: + _logger.exception( + 'Unexpected Error when updating the status ' + 'of pingen.document %s: ' % self.id) + raise UserError( + _('Unexpected Error when updating the status ' + 'of Document %s') % self.name) return True def _update_post_infos(self, pingen): @@ -277,19 +284,15 @@ class PingenDocument(models.Model): 'Pingen Document %s to %s.' % (self.id, pingen.url)) raise - # currency_ids = self.env['res.currency'].search( - # [('name', '=', post_infos['currency'])]) country = self.env['res.country'].search( [('code', '=', post_infos['country'])]) send_date = pingen_datetime_to_utc(post_infos['date']) vals = { - # 'post_status': POST_SENDING_STATUS[post_infos['status']], - # 'cost': post_infos['cost'], - # 'currency_id': currency_ids[0] if currency_ids else False, + 'post_status': POST_SENDING_STATUS[post_infos['status']], 'parsed_address': post_infos['address'], - 'country_id': country.id if country else False, + 'country_id': country.id, 'send_date': fields.Datetime.to_string(send_date), - # 'pages': post_infos['pages'], + 'pages': post_infos['pages'], 'last_error_message': False, } if pingen.is_posted(post_infos): @@ -330,26 +333,22 @@ class PingenDocument(models.Model): instance) for public interface. """ self.ensure_one() - for document in self: - try: - session = document._get_pingen_session() - document._update_post_infos(pingen=session) - except ConnectionError as e: - raise UserError( - _('Connection Error when updating ' + - 'the status of Document %s' - ' from Pingen') % document.name) - except APIError as e: - raise UserError( - _('Error when updating the status ' + - 'of Document %s from Pingen: ' - '\n%s') % (document.name, e)) - except BaseException as e: - _logger.exception( - 'Unexcepted Error when updating ' + - 'the status of pingen.document %s: ' % - document.id) - raise UserError( - _('Unexcepted Error when updating ' + - 'the status of Document %s') % document.name) + try: + session = self._get_pingen_session() + self._update_post_infos(pingen=session) + except ConnectionError as e: + raise UserError( + _('Connection Error when updating the status ' + 'of Document %s from Pingen') % self.name) + except APIError as e: + raise UserError( + _('Error when updating the status of Document %s from ' + 'Pingen: \n%s') % (self.name, e)) + except BaseException as e: + _logger.exception( + 'Unexpected Error when updating the status ' + 'of pingen.document %s: ' % self.id) + raise UserError( + _('Unexpected Error when updating the status ' + 'of Document %s') % self.name) return True diff --git a/pingen/res_company.py b/pingen/models/res_company.py similarity index 100% rename from pingen/res_company.py rename to pingen/models/res_company.py diff --git a/pingen/pingen_data.xml b/pingen/pingen_data.xml deleted file mode 100644 index 4b9cd38..0000000 --- a/pingen/pingen_data.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - Run Pingen Document Push - - - 1 - hours - -1 - - pingen.document - _push_and_send_to_pingen_cron - () - - - - Run Pingen Document Update - - - 1 - days - -1 - - pingen.document - _update_post_infos_cron - () - - - - diff --git a/pingen/pingen_document_view.xml b/pingen/pingen_document_view.xml deleted file mode 100644 index 253e837..0000000 --- a/pingen/pingen_document_view.xml +++ /dev/null @@ -1,161 +0,0 @@ - - - - - - pingen.document.tree - pingen.document - tree - - - - - - - - - - - - - - pingen.document.form - pingen.document - form - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -