diff --git a/pingen/__openerp__.py b/pingen/__openerp__.py index 4ae5dc8..93b796d 100644 --- a/pingen/__openerp__.py +++ b/pingen/__openerp__.py @@ -30,7 +30,7 @@ 'depends': [], 'external_dependencies': { 'python': ['requests'], - }, + }, 'description': """ Integration with pingen.com =========================== @@ -91,8 +91,8 @@ you will need to manually handle the case, either from the pingen.com backend, or by changing the document on OpenERP and resolving the error on the Pingen Document. -When a connection error occurs, the action will be retried on the next scheduler -run. +When a connection error occurs, the action will be retried on the next +scheduler run. Dependencies ------------ @@ -111,7 +111,7 @@ Dependencies 'pingen_data.xml', 'res_company_view.xml', 'security/ir.model.access.csv', - ], + ], 'tests': [], 'installable': False, 'auto_install': False, diff --git a/pingen/ir_attachment.py b/pingen/ir_attachment.py index ba00c47..6f5b7a0 100644 --- a/pingen/ir_attachment.py +++ b/pingen/ir_attachment.py @@ -41,8 +41,10 @@ class ir_attachment(orm.Model): 'pingen_speed': fields.selection( [('1', 'Priority'), ('2', 'Economy')], 'Speed', - help="Defines the sending speed if the document is automatically sent"), - 'pingen_color': fields.selection([('0', 'B/W'), ('1', 'Color')], 'Type of print'), + help="Defines the sending speed if the document is " + "automatically sent"), + 'pingen_color': fields.selection([('0', 'B/W'), ('1', 'Color')], + 'Type of print'), } _defaults = { @@ -56,14 +58,15 @@ class ir_attachment(orm.Model): 'config': 'created from attachment'} def _handle_pingen_document(self, cr, uid, attachment_id, context=None): - """ Reponsible of the related ``pingen.document`` when the ``send_to_pingen`` - field is modified. + """ Reponsible of the related ``pingen.document`` + when the ``send_to_pingen`` field is modified. Only one pingen document can be created per attachment. When ``send_to_pingen`` is activated: * Create a ``pingen.document`` if it does not already exist - * Put the related ``pingen.document`` to ``pending`` if it already exist + * Put the related ``pingen.document`` to ``pending`` if + it already exist When it is deactivated: * Do nothing if no related ``pingen.document`` exists * Or cancel it @@ -72,7 +75,8 @@ class ir_attachment(orm.Model): """ pingen_document_obj = self.pool.get('pingen.document') attachment = self.browse(cr, uid, attachment_id, context=context) - document = attachment.pingen_document_ids[0] if attachment.pingen_document_ids else None + document = attachment.pingen_document_ids[ + 0] if attachment.pingen_document_ids else None if attachment.send_to_pingen: if document: document.write({'state': 'pending'}, context=context) @@ -87,22 +91,26 @@ class ir_attachment(orm.Model): if document.state == 'pushed': raise osv.except_osv( _('Error'), - _('The attachment %s is already pushed to pingen.com.') % - attachment.name) + _('The attachment %s is already pushed to ' + 'pingen.com.') % attachment.name) document.write({'state': 'canceled'}, context=context) return def create(self, cr, uid, vals, context=None): - attachment_id = super(ir_attachment, self).create(cr, uid, vals, context=context) + attachment_id = super(ir_attachment, self).create( + cr, uid, vals, context=context) if 'send_to_pingen' in vals: - self._handle_pingen_document(cr, uid, attachment_id, context=context) + self._handle_pingen_document( + cr, uid, attachment_id, context=context) return attachment_id def write(self, cr, uid, ids, vals, context=None): - res = super(ir_attachment, self).write(cr, uid, ids, vals, context=context) + res = super(ir_attachment, self).write( + cr, uid, ids, vals, context=context) if 'send_to_pingen' in vals: for attachment_id in ids: - self._handle_pingen_document(cr, uid, attachment_id, context=context) + self._handle_pingen_document( + cr, uid, attachment_id, context=context) return res def _decoded_content(self, cr, uid, attachment, context=None): diff --git a/pingen/pingen.py b/pingen/pingen.py index 475407b..df35a3f 100644 --- a/pingen/pingen.py +++ b/pingen/pingen.py @@ -58,19 +58,23 @@ def pingen_datetime_to_utc(dt): class PingenException(RuntimeError): + """There was an ambiguous exception that occurred while handling your request.""" class ConnectionError(PingenException): + """An Error occured with the pingen API""" class APIError(PingenException): + """An Error occured with the pingen API""" class Pingen(object): + """ Interface to the pingen.com API """ def __init__(self, token, staging=True): @@ -132,11 +136,13 @@ class Pingen(object): if response.json['error']: raise APIError( - "%s: %s" % (response.json['errorcode'], response.json['errormessage'])) + "%s: %s" % (response.json['errorcode'], + response.json['errormessage'])) return response - def push_document(self, filename, filestream, send=None, speed=None, color=None): + def push_document(self, filename, filestream, send=None, speed=None, + color=None): """ Upload a document to pingen.com and eventually ask to send it :param str filename: name of the file to push @@ -154,7 +160,7 @@ class Pingen(object): 'send': send, 'speed': speed, 'color': color, - } + } # we cannot use the `files` param alongside # with the `datas`param when data is a @@ -164,7 +170,7 @@ class Pingen(object): formdata = { 'file': (filename, filestream.read()), 'data': json.dumps(data), - } + } multipart, content_type = encode_multipart_formdata(formdata) @@ -196,7 +202,7 @@ class Pingen(object): data = { 'speed': speed, 'color': color, - } + } response = self._send( self.session.post, 'document/send', diff --git a/pingen/pingen_document.py b/pingen/pingen_document.py index ac59a02..de7e399 100644 --- a/pingen/pingen_document.py +++ b/pingen/pingen_document.py @@ -34,6 +34,7 @@ _logger = logging.getLogger(__name__) class pingen_document(orm.Model): + """ A pingen document is the state of the synchronization of an attachment with pingen.com @@ -75,7 +76,8 @@ class pingen_document(orm.Model): 'post_status': fields.char('Post Status', size=128, readonly=True), 'parsed_address': fields.text('Parsed Address', readonly=True), 'cost': fields.float('Cost', readonly=True), - 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), + 'currency_id': fields.many2one('res.currency', 'Currency', + readonly=True), 'country_id': fields.many2one('res.country', 'Country', readonly=True), 'send_date': fields.datetime('Date of sending', readonly=True), 'pages': fields.integer('Pages', readonly=True), @@ -95,7 +97,8 @@ class pingen_document(orm.Model): """ Returns a pingen session for a user """ company = self.pool.get('res.users').browse( cr, uid, uid, context=context).company_id - return self.pool.get('res.company')._pingen(cr, uid, company, context=context) + return self.pool.get('res.company')._pingen(cr, uid, company, + context=context) def _push_to_pingen(self, cr, uid, document, pingen=None, context=None): """ Push a document to pingen.com @@ -141,11 +144,13 @@ class pingen_document(orm.Model): document.write( {'last_error_message': error, 'state': state, - 'push_date': push_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), + 'push_date': push_date.strftime( + tools.DEFAULT_SERVER_DATETIME_FORMAT), 'pingen_id': doc_id, 'post_id': post_id}, context=context) - _logger.info('Pingen Document %s: pushed to %s' % (document.id, pingen.url)) + _logger.info('Pingen Document %s: pushed to %s' % (document.id, + pingen.url)) def push_to_pingen(self, cr, uid, ids, context=None): """ Push a document to pingen.com @@ -164,7 +169,8 @@ class pingen_document(orm.Model): except ConnectionError as e: raise osv.except_osv( _('Pingen Connection Error'), - _('Connection Error when asking for sending the document %s to Pingen') % document.name) + _('Connection Error when asking for sending the ' + 'document %s to Pingen') % document.name) except APIError as e: raise osv.except_osv( @@ -174,11 +180,13 @@ class pingen_document(orm.Model): except: _logger.exception( - 'Unexcepted Error when updating the status of pingen.document %s: ' % + 'Unexcepted Error when updating the status of ' + 'pingen.document %s: ' % document.id) raise osv.except_osv( _('Error'), - _('Unexcepted Error when updating the status of Document %s') % document.name) + _('Unexcepted Error when updating the status ' + 'of Document %s') % document.name) return True def _push_and_send_to_pingen_cron(self, cr, uid, ids, context=None): @@ -209,11 +217,13 @@ class pingen_document(orm.Model): try: if document.state == 'pending': self._push_to_pingen( - loc_cr, uid, document, pingen=session, context=context) + loc_cr, uid, document, pingen=session, + context=context) elif document.state == 'pushed': self._ask_pingen_send( - loc_cr, uid, document, pingen=session, context=context) + loc_cr, uid, document, pingen=session, + context=context) except ConnectionError as e: document.write({'last_error_message': e, 'state': 'error'}, @@ -264,11 +274,13 @@ class pingen_document(orm.Model): document.pingen_speed, document.pingen_color) except ConnectionError: - _logger.exception('Connection Error when asking for sending Pingen Document %s to %s.' % + _logger.exception('Connection Error when asking for sending ' + 'Pingen Document %s to %s.' % (document.id, pingen.url)) raise except APIError: - _logger.exception('API Error when asking for sending Pingen Document %s to %s.' % + _logger.exception('API Error when asking for sending ' + 'Pingen Document %s to %s.' % (document.id, pingen.url)) raise @@ -277,7 +289,8 @@ class pingen_document(orm.Model): 'state': 'sendcenter', 'post_id': post_id}, context=context) - _logger.info('Pingen Document %s: asked for sending to %s' % (document.id, pingen.url)) + _logger.info('Pingen Document %s: asked for sending to %s' % + (document.id, pingen.url)) return True @@ -291,7 +304,8 @@ class pingen_document(orm.Model): with self._get_pingen_session(cr, uid, context=context) as session: for document in self.browse(cr, uid, ids, context=context): try: - self._ask_pingen_send(cr, uid, document, pingen=session, context=context) + self._ask_pingen_send( + cr, uid, document, pingen=session, context=context) except ConnectionError as e: raise osv.except_osv( _('Pingen Connection Error'), @@ -306,11 +320,13 @@ class pingen_document(orm.Model): except: _logger.exception( - 'Unexcepted Error when updating the status of pingen.document %s: ' % + 'Unexcepted Error when updating the status of ' + 'pingen.document %s: ' % document.id) raise osv.except_osv( _('Error'), - _('Unexcepted Error when updating the status of Document %s') % document.name) + _('Unexcepted Error when updating the status ' + 'of Document %s') % document.name) return True def _update_post_infos(self, cr, uid, document, pingen, context=None): @@ -348,10 +364,11 @@ class pingen_document(orm.Model): 'currency_id': currency_ids[0] if currency_ids else False, 'parsed_address': post_infos['address'], 'country_id': country_ids[0] if country_ids else False, - 'send_date': send_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), + 'send_date': send_date.strftime( + tools.DEFAULT_SERVER_DATETIME_FORMAT), 'pages': post_infos['pages'], 'last_error_message': False, - } + } if pingen.is_posted(post_infos): vals['state'] = 'sent' @@ -381,7 +398,8 @@ class pingen_document(orm.Model): loc_cr, uid, document, pingen=session, context=context) except (ConnectionError, APIError): # will be retried the next time - # In any case, the error has been logged by _update_post_infos + # In any case, the error has been logged by + # _update_post_infos loc_cr.rollback() except: _logger.error('Unexcepted error in pingen cron') @@ -406,20 +424,24 @@ class pingen_document(orm.Model): except ConnectionError as e: raise osv.except_osv( _('Pingen Connection Error'), - _('Connection Error when updating the status of Document %s' + _('Connection Error when updating the status of ' + 'Document %s' ' from Pingen') % document.name) except APIError as e: raise osv.except_osv( _('Pingen Error'), - _('Error when updating the status of Document %s from Pingen: ' + _('Error when updating the status of Document %s ' + 'from Pingen: ' '\n%s') % (document.name, e)) except: _logger.exception( - 'Unexcepted Error when updating the status of pingen.document %s: ' % + 'Unexcepted Error when updating the status of ' + 'pingen.document %s: ' % document.id) raise osv.except_osv( _('Error'), - _('Unexcepted Error when updating the status of Document %s') % document.name) + _('Unexcepted Error when updating the status of ' + 'Document %s') % document.name) return True