mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
[IMP] send an already pushed document. handle exceptions differently for the client or internal call
This commit is contained in:
@@ -155,10 +155,15 @@ class Pingen(object):
|
|||||||
:param int/str color: type of print, 0 = B/W, 1 = Color
|
:param int/str color: type of print, 0 = B/W, 1 = Color
|
||||||
:return: id of the post on pingen.com
|
:return: id of the post on pingen.com
|
||||||
"""
|
"""
|
||||||
|
data = {
|
||||||
|
'speed': speed,
|
||||||
|
'color': color,
|
||||||
|
}
|
||||||
response = self._send(
|
response = self._send(
|
||||||
requests.post,
|
requests.post,
|
||||||
'document/send',
|
'document/send',
|
||||||
params={'id': document_id})
|
params={'id': document_id},
|
||||||
|
data={'data': json.dumps(data)})
|
||||||
|
|
||||||
return response.json['id']
|
return response.json['id']
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,9 @@ import datetime
|
|||||||
|
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
from openerp.osv import orm, fields
|
from openerp.osv import osv, orm, fields
|
||||||
from openerp import tools
|
from openerp import tools
|
||||||
|
from openerp.tools.translate import _
|
||||||
from .pingen import Pingen, APIError, ConnectionError
|
from .pingen import Pingen, APIError, ConnectionError
|
||||||
|
|
||||||
# TODO should be configurable
|
# TODO should be configurable
|
||||||
@@ -51,9 +52,10 @@ class pingen_task(orm.Model):
|
|||||||
'state': fields.selection(
|
'state': fields.selection(
|
||||||
[('pending', 'Pending'),
|
[('pending', 'Pending'),
|
||||||
('pushed', 'Pushed'),
|
('pushed', 'Pushed'),
|
||||||
|
('sendcenter', 'In Sendcenter'),
|
||||||
('sent', 'Sent'),
|
('sent', 'Sent'),
|
||||||
('error', 'Error'),
|
('error', 'Connection Error'),
|
||||||
('need_fix', 'Needs a correction'),
|
('pingen_error', 'Pingen Error'),
|
||||||
('canceled', 'Canceled')],
|
('canceled', 'Canceled')],
|
||||||
string='State', readonly=True, required=True),
|
string='State', readonly=True, required=True),
|
||||||
'date': fields.datetime('Creation Date'),
|
'date': fields.datetime('Creation Date'),
|
||||||
@@ -77,21 +79,14 @@ class pingen_task(orm.Model):
|
|||||||
'Only one Pingen task is allowed per attachment.'),
|
'Only one Pingen task is allowed per attachment.'),
|
||||||
]
|
]
|
||||||
|
|
||||||
def _push_to_pingen(self, cr, uid, task_id, context=None):
|
def _push_to_pingen(self, cr, uid, task, context=None):
|
||||||
""" Push a document to pingen.com
|
""" Push a document to pingen.com """
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
attachment_obj = self.pool.get('ir.attachment')
|
attachment_obj = self.pool.get('ir.attachment')
|
||||||
task = self.browse(cr, uid, task_id, context=context)
|
|
||||||
|
|
||||||
decoded_document = attachment_obj._decoded_content(
|
decoded_document = attachment_obj._decoded_content(
|
||||||
cr, uid, task.attachment_id, context=context)
|
cr, uid, task.attachment_id, context=context)
|
||||||
|
|
||||||
success = False
|
pingen = self._pingen(cr, uid, [], context=context)
|
||||||
# parameterize
|
|
||||||
pingen = Pingen(TOKEN, staging=True)
|
|
||||||
doc = (task.datas_fname, )
|
|
||||||
try:
|
try:
|
||||||
doc_id, post_id, __ = pingen.push_document(
|
doc_id, post_id, __ = pingen.push_document(
|
||||||
task.datas_fname,
|
task.datas_fname,
|
||||||
@@ -100,42 +95,119 @@ class pingen_task(orm.Model):
|
|||||||
task.pingen_speed,
|
task.pingen_speed,
|
||||||
task.pingen_color)
|
task.pingen_color)
|
||||||
except ConnectionError as e:
|
except ConnectionError as e:
|
||||||
# we can continue and it will be retried the next time
|
|
||||||
_logger.exception('Connection Error when pushing Pingen Task %s to %s.' %
|
_logger.exception('Connection Error when pushing Pingen Task %s to %s.' %
|
||||||
(task.id, pingen.url))
|
(task.id, pingen.url))
|
||||||
task.write(
|
raise
|
||||||
{'last_error_message': e,
|
|
||||||
'state': 'error'},
|
|
||||||
context=context)
|
|
||||||
|
|
||||||
except APIError as e:
|
except APIError as e:
|
||||||
_logger.warning('API Error when pushing Pingen Task %s to %s.' %
|
_logger.error('API Error when pushing Pingen Task %s to %s.' %
|
||||||
(task.id, pingen.url))
|
(task.id, pingen.url))
|
||||||
task.write(
|
raise
|
||||||
{'last_error_message': e,
|
|
||||||
'state': 'need_fix'},
|
|
||||||
context=context)
|
|
||||||
else:
|
|
||||||
success = True
|
|
||||||
|
|
||||||
now = datetime.datetime.now().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
now = datetime.datetime.now().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
task.write(
|
task.write(
|
||||||
{'last_error_message': False,
|
{'last_error_message': False,
|
||||||
'state': 'pushed',
|
'state': 'sendcenter' if post_id else 'pushed',
|
||||||
'push_date': now,
|
'push_date': now,
|
||||||
'pingen_id': doc_id,
|
'pingen_id': doc_id,
|
||||||
'post_id': post_id},
|
'post_id': post_id},
|
||||||
context=context)
|
context=context)
|
||||||
_logger.info('Pingen Task %s pushed to %s' % (task.id, pingen.url))
|
_logger.info('Pingen Task %s pushed to %s' % (task.id, pingen.url))
|
||||||
|
|
||||||
return success
|
|
||||||
|
|
||||||
def push_to_pingen(self, cr, uid, ids, context=None):
|
def push_to_pingen(self, cr, uid, ids, context=None):
|
||||||
""" Push a document to pingen.com
|
""" Push a document to pingen.com
|
||||||
|
|
||||||
Wrapper method for multiple ids (when triggered from button)
|
Convert errors to osv.except_osv to be handled by the client.
|
||||||
|
|
||||||
|
Wrapper method for multiple ids (when triggered from button for
|
||||||
|
instance) for public interface.
|
||||||
"""
|
"""
|
||||||
for task_id in ids:
|
for task in self.browse(cr, uid, ids, context=context):
|
||||||
self._push_to_pingen(cr, uid, task_id, context=context)
|
try:
|
||||||
|
self._push_to_pingen(cr, uid, task, context=context)
|
||||||
|
except ConnectionError as e:
|
||||||
|
raise osv.except_osv(
|
||||||
|
_('Pingen Connection Error'),
|
||||||
|
_('Connection Error when asking for sending the document %s to Pingen') % task.name)
|
||||||
|
|
||||||
|
except APIError as e:
|
||||||
|
raise osv.except_osv(
|
||||||
|
_('Pingen Error'),
|
||||||
|
_('Error when asking Pingen to send the document %s: '
|
||||||
|
'\n%s') % (task.name, e))
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _push_to_pingen_with_logs(self, cr, uid, ids, context=None):
|
||||||
|
""" Push a document to pingen.com
|
||||||
|
|
||||||
|
Instead of raising, store the error in the pingen.document
|
||||||
|
"""
|
||||||
|
for task in self.browse(cr, uid, ids, context=context):
|
||||||
|
try:
|
||||||
|
self._push_to_pingen(cr, uid, task, context=context)
|
||||||
|
except ConnectionError as e:
|
||||||
|
task.write({'last_error_message': e, 'state': 'error'}, context=context)
|
||||||
|
except APIError as e:
|
||||||
|
task.write({'last_error_message': e, 'state': 'pingen_error'}, context=context)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _pingen(self, cr, uid, ids, context=None):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
assert not ids, "ids is there by convention, should not be used"
|
||||||
|
# TODO parameterize
|
||||||
|
return Pingen(TOKEN, staging=True)
|
||||||
|
|
||||||
|
def _ask_pingen_send(self, cr, uid, task, context=None):
|
||||||
|
""" For a document already pushed to pingen, ask to send it.
|
||||||
|
"""
|
||||||
|
# sending has been explicitely asked so we change the option
|
||||||
|
# for consistency
|
||||||
|
if not task.pingen_send:
|
||||||
|
task.write({'pingen_send': True}, context=context)
|
||||||
|
|
||||||
|
pingen = self._pingen(cr, uid, [], context=context)
|
||||||
|
try:
|
||||||
|
post_id = pingen.send_document(
|
||||||
|
task.pingen_id,
|
||||||
|
task.pingen_speed,
|
||||||
|
task.pingen_color)
|
||||||
|
except ConnectionError as e:
|
||||||
|
_logger.exception('Connection Error when asking for sending Pingen Task %s to %s.' %
|
||||||
|
(task.id, pingen.url))
|
||||||
|
raise
|
||||||
|
except APIError as e:
|
||||||
|
_logger.exception('API Error when asking for sending Pingen Task %s to %s.' %
|
||||||
|
(task.id, pingen.url))
|
||||||
|
raise
|
||||||
|
|
||||||
|
task.write(
|
||||||
|
{'last_error_message': False,
|
||||||
|
'state': 'sendcenter',
|
||||||
|
'post_id': post_id},
|
||||||
|
context=context)
|
||||||
|
_logger.info('Pingen Task %s asked for sending to %s' % (task.id, pingen.url))
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def ask_pingen_send(self, cr, uid, ids, context=None):
|
||||||
|
""" For a document already pushed to pingen, ask to send it.
|
||||||
|
|
||||||
|
Wrapper method for multiple ids (when triggered from button for
|
||||||
|
instance) for public interface.
|
||||||
|
"""
|
||||||
|
for task in self.browse(cr, uid, ids, context=context):
|
||||||
|
try:
|
||||||
|
self._ask_pingen_send(cr, uid, task, context=context)
|
||||||
|
except ConnectionError as e:
|
||||||
|
raise osv.except_osv(
|
||||||
|
_('Pingen Connection Error'),
|
||||||
|
_('Connection Error when asking for sending the document %s to Pingen') % task.name)
|
||||||
|
|
||||||
|
except APIError as e:
|
||||||
|
raise osv.except_osv(
|
||||||
|
_('Pingen Error'),
|
||||||
|
_('Error when asking Pingen to send the document %s: '
|
||||||
|
'\n%s') % (task.name, e))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -34,9 +34,9 @@
|
|||||||
<separator string="Options" colspan="4"/>
|
<separator string="Options" colspan="4"/>
|
||||||
<newline />
|
<newline />
|
||||||
<group col="2" colspan="2">
|
<group col="2" colspan="2">
|
||||||
<field name="pingen_send"/>
|
<field name="pingen_send" attrs="{'readonly': [('state', 'in', ['sendcenter', 'sent'])]}"/>
|
||||||
<field name="pingen_speed" attrs="{'required': [('pingen_send', '=', True)]}"/>
|
<field name="pingen_speed" attrs="{'readonly': [('state', 'in', ['sendcenter', 'sent'])], 'required': [('pingen_send', '=', True)]}"/>
|
||||||
<field name="pingen_color"/>
|
<field name="pingen_color" attrs="{'readonly': [('state', 'in', ['sendcenter', 'sent'])]}"/>
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<separator string="Dates" colspan="4"/>
|
<separator string="Dates" colspan="4"/>
|
||||||
@@ -57,8 +57,11 @@
|
|||||||
|
|
||||||
<group col="2" colspan="2">
|
<group col="2" colspan="2">
|
||||||
<button name="push_to_pingen" type="object"
|
<button name="push_to_pingen" type="object"
|
||||||
states="pending,error,need_fix"
|
states="pending,error,pingen_error"
|
||||||
string="Push to pingen.com" icon="terp-camera_test"/>
|
string="Push to pingen.com" icon="terp-camera_test"/>
|
||||||
|
<button name="ask_pingen_send" type="object"
|
||||||
|
states="pushed"
|
||||||
|
string="Ask pingen.com to send the document" icon="terp-camera_test"/>
|
||||||
</group>
|
</group>
|
||||||
</page>
|
</page>
|
||||||
<page string="Attachment">
|
<page string="Attachment">
|
||||||
@@ -82,7 +85,7 @@
|
|||||||
|
|
||||||
<field name="state" widget="statusbar"
|
<field name="state" widget="statusbar"
|
||||||
statusbar_visible="pending,pushed,sent"
|
statusbar_visible="pending,pushed,sent"
|
||||||
statusbar_colors='{"error":"red","need_fix":"red","canceled":"grey","pushed":"blue","sent":"green"}'/>
|
statusbar_colors='{"error":"red","pingen_error":"red","canceled":"grey","pushed":"blue","sent":"green"}'/>
|
||||||
</form>
|
</form>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|||||||
Reference in New Issue
Block a user