Skip to content

Commit

Permalink
Finish migration to version 10.0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
grindtildeath committed Jan 11, 2018
1 parent 2d5d874 commit 860f381
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 306 deletions.
1 change: 1 addition & 0 deletions pingen/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Contributors
* Yannick Vaucher <yannick.vaucher@camptocamp.com>
* Guewen Baconnier <guewen.baconnier@camptocamp.com>
* Anar Baghirli <a.baghirli@mobilunity.com>
* Akim Juillerat <akim.juillerat@camptocamp.com>

Maintainer
==========
Expand Down
5 changes: 1 addition & 4 deletions pingen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions pingen/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [],
Expand Down
32 changes: 32 additions & 0 deletions pingen/data/pingen_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">


<record forcecreate="True" id="ir_cron_push_pingen" model="ir.cron">
<field name="name">Run Pingen Document Push</field>
<field eval="True" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field name="model">pingen.document</field>
<field name="function">_push_and_send_to_pingen_cron</field>
<field name="args">()</field>
</record>

<record forcecreate="True" id="ir_cron_update_pingen" model="ir.cron">
<field name="name">Run Pingen Document Update</field>
<field eval="True" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field name="model">pingen.document</field>
<field name="function">_update_post_infos_cron</field>
<field name="args">()</field>
</record>


</odoo>
31 changes: 0 additions & 31 deletions pingen/ir_attachment_view.xml

This file was deleted.

8 changes: 8 additions & 0 deletions pingen/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions pingen/models/base_config_settings.py
Original file line number Diff line number Diff line change
@@ -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')
File renamed without changes.
10 changes: 0 additions & 10 deletions pingen/pingen.py → pingen/models/pingen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import urlparse
import json
import pytz
import base64

from datetime import datetime
from requests.packages.urllib3.filepost import encode_multipart_formdata
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -117,11 +112,6 @@ def _send(self, method, endpoint, **kwargs):

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'],
Expand Down
97 changes: 53 additions & 44 deletions pingen/pingen_document.py → pingen/models/pingen_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
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
from requests.exceptions import ConnectionError

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -119,27 +119,37 @@ def push_to_pingen(self):
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):
Expand Down Expand Up @@ -172,7 +182,7 @@ def _push_and_send_to_pingen_cron(self):
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):
Expand Down Expand Up @@ -232,28 +242,27 @@ def ask_pingen_send(self):
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(
'Unexcepted Error when updating the ' +
'status of pingen.document %s: ' %
self.id)
raise UserError(
_('Unexcepted Error when updating the ' +
'status of Document %s') % self.name)
return True

def _update_post_infos(self, pingen):
Expand Down
File renamed without changes.
32 changes: 0 additions & 32 deletions pingen/pingen_data.xml

This file was deleted.

Loading

0 comments on commit 860f381

Please sign in to comment.