[IMP] rename pingen.task to pingen.document

This commit is contained in:
Guewen Baconnier @ Camptocamp
2012-11-26 07:41:22 +01:00
parent 51feae800d
commit c5054db5b5
6 changed files with 110 additions and 103 deletions

View File

@@ -21,5 +21,5 @@
import ir_attachment import ir_attachment
import pingen import pingen
import pingen_task import pingen_document

View File

@@ -57,7 +57,7 @@ Dependencies
'init_xml': [], 'init_xml': [],
'update_xml': [ 'update_xml': [
'ir_attachment_view.xml', 'ir_attachment_view.xml',
'pingen_task_view.xml', 'pingen_document_view.xml',
], ],
'demo_xml': [], 'demo_xml': [],
'tests': [], 'tests': [],

View File

@@ -32,9 +32,9 @@ class ir_attachment(orm.Model):
_columns = { _columns = {
'send_to_pingen': fields.boolean('Send to Pingen.com'), 'send_to_pingen': fields.boolean('Send to Pingen.com'),
'pingen_task_ids': fields.one2many( 'pingen_document_ids': fields.one2many(
'pingen.task', 'attachment_id', 'pingen.document', 'attachment_id',
string="Pingen Task", readonly=True), string='Pingen Document', readonly=True),
'pingen_send': fields.boolean( 'pingen_send': fields.boolean(
'Send', 'Send',
help="Defines if a document is merely uploaded or also sent"), help="Defines if a document is merely uploaded or also sent"),
@@ -50,58 +50,59 @@ class ir_attachment(orm.Model):
'pingen_speed': '2', 'pingen_speed': '2',
} }
def _prepare_pingen_task_vals(self, cr, uid, attachment, context=None): def _prepare_pingen_document_vals(self, cr, uid, attachment, context=None):
return {'attachment_id': attachment.id, return {'attachment_id': attachment.id,
'config': 'created from attachment'} 'config': 'created from attachment'}
def _handle_pingen_task(self, cr, uid, attachment_id, context=None): def _handle_pingen_document(self, cr, uid, attachment_id, context=None):
""" Reponsible of the related ``pingen.task`` when the ``send_to_pingen`` """ Reponsible of the related ``pingen.document`` when the ``send_to_pingen``
field is modified. field is modified.
Only one pingen task can be created per attachment. Only one pingen document can be created per attachment.
When ``send_to_pingen`` is activated: When ``send_to_pingen`` is activated:
* Create a ``pingen.task`` if it does not already exist * Create a ``pingen.document`` if it does not already exist
* Put the related ``pingen.task`` to ``pending`` if it already exist * Put the related ``pingen.document`` to ``pending`` if it already exist
When it is deactivated: When it is deactivated:
* Do nothing if no related ``pingen.task`` exists * Do nothing if no related ``pingen.document`` exists
* Or cancel it * Or cancel it
* If it has already been pushed to pingen.com, raises * If it has already been pushed to pingen.com, raises
an `osv.except_osv` exception an `osv.except_osv` exception
""" """
pingen_task_obj = self.pool.get('pingen.task') pingen_document_obj = self.pool.get('pingen.document')
attachment = self.browse(cr, uid, attachment_id, context=context) attachment = self.browse(cr, uid, attachment_id, context=context)
task = attachment.pingen_task_ids[0] if attachment.pingen_task_ids else None document = attachment.pingen_document_ids[0] if attachment.pingen_document_ids else None
if attachment.send_to_pingen: if attachment.send_to_pingen:
if task: if document:
task.write({'state': 'pending'}, context=context) document.write({'state': 'pending'}, context=context)
else: else:
pingen_task_obj.create( pingen_document_obj.create(
cr, uid, cr, uid,
self._prepare_pingen_task_vals( self._prepare_pingen_document_vals(
cr, uid, attachment, context=context), cr, uid, attachment, context=context),
context=context) context=context)
else: else:
if task: if document:
if task.state == 'pushed': if document.state == 'pushed':
# TODO: cancel on pingen.com
raise osv.except_osv( raise osv.except_osv(
_('Error'), _('Error'),
_('The attachment %s is already pushed to pingen.com.') % \ _('The attachment %s is already pushed to pingen.com.') % \
attachment.name) attachment.name)
task.write({'state': 'canceled'}, context=context) document.write({'state': 'canceled'}, context=context)
return return
def create(self, cr, uid, vals, context=None): 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: if 'send_to_pingen' in vals:
self._handle_pingen_task(cr, uid, attachment_id, context=context) self._handle_pingen_document(cr, uid, attachment_id, context=context)
return attachment_id return attachment_id
def write(self, cr, uid, ids, vals, context=None): 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: if 'send_to_pingen' in vals:
for attachment_id in ids: for attachment_id in ids:
self._handle_pingen_task(cr, uid, attachment_id, context=context) self._handle_pingen_document(cr, uid, attachment_id, context=context)
return res return res
def _decoded_content(self, cr, uid, attachment, context=None): def _decoded_content(self, cr, uid, attachment, context=None):

View File

@@ -21,10 +21,10 @@
<act_window <act_window
context="{'search_default_attachment_id': [active_id], 'default_attachment_id': active_id}" context="{'search_default_attachment_id': [active_id], 'default_attachment_id': active_id}"
id="act_attachment_to_pingen_task" id="act_attachment_to_pingen_document"
name="Pingen Task" name="Pingen Document"
groups="" groups=""
res_model="pingen.task" res_model="pingen.document"
src_model="ir.attachment"/> src_model="ir.attachment"/>
</data> </data>

View File

@@ -35,20 +35,22 @@ STAGING = True
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
class pingen_task(orm.Model): class pingen_document(orm.Model):
""" A pingen task is the state of the synchronization of """ A pingen document is the state of the synchronization of
an attachment with pingen.com an attachment with pingen.com
It stores the configuration and the current state of the synchronization. It stores the configuration and the current state of the synchronization.
It also serves as a queue of documents to push to pingen.com It also serves as a queue of documents to push to pingen.com
""" """
_name = 'pingen.task' _name = 'pingen.document'
_inherits = {'ir.attachment': 'attachment_id'} _inherits = {'ir.attachment': 'attachment_id'}
_columns = { _columns = {
'attachment_id': fields.many2one( 'attachment_id': fields.many2one(
'ir.attachment', 'Document', required=True, readonly=True, ondelete='cascade'), 'ir.attachment', 'Document',
required=True, readonly=True,
ondelete='cascade'),
'state': fields.selection( 'state': fields.selection(
[('pending', 'Pending'), [('pending', 'Pending'),
('pushed', 'Pushed'), ('pushed', 'Pushed'),
@@ -87,9 +89,9 @@ class pingen_task(orm.Model):
} }
_sql_constraints = [ _sql_constraints = [
('pingen_task_attachment_uniq', ('pingen_document_attachment_uniq',
'unique (attachment_id)', 'unique (attachment_id)',
'Only one Pingen task is allowed per attachment.'), 'Only one Pingen document is allowed per attachment.'),
] ]
def _pingen(self, cr, uid, ids, context=None): def _pingen(self, cr, uid, ids, context=None):
@@ -98,41 +100,41 @@ class pingen_task(orm.Model):
# TODO parameterize # TODO parameterize
return Pingen(TOKEN, staging=STAGING) return Pingen(TOKEN, staging=STAGING)
def _push_to_pingen(self, cr, uid, task, context=None): def _push_to_pingen(self, cr, uid, document, 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')
decoded_document = attachment_obj._decoded_content( decoded_document = attachment_obj._decoded_content(
cr, uid, task.attachment_id, context=context) cr, uid, document.attachment_id, context=context)
pingen = self._pingen(cr, uid, [], context=context) pingen = self._pingen(cr, uid, [], context=context)
try: try:
doc_id, post_id, infos = pingen.push_document( doc_id, post_id, infos = pingen.push_document(
task.datas_fname, document.datas_fname,
StringIO(decoded_document), StringIO(decoded_document),
task.pingen_send, document.pingen_send,
task.pingen_speed, document.pingen_speed,
task.pingen_color) document.pingen_color)
except ConnectionError as e: except ConnectionError as e:
_logger.exception( _logger.exception(
'Connection Error when pushing Pingen Task %s to %s.' % 'Connection Error when pushing Pingen Document %s to %s.' %
(task.id, pingen.url)) (document.id, pingen.url))
raise raise
except APIError as e: except APIError as e:
_logger.error( _logger.error(
'API Error when pushing Pingen Task %s to %s.' % 'API Error when pushing Pingen Document %s to %s.' %
(task.id, pingen.url)) (document.id, pingen.url))
raise raise
task.write( document.write(
{'last_error_message': False, {'last_error_message': False,
'state': 'sendcenter' if post_id else 'pushed', 'state': 'sendcenter' if post_id else 'pushed',
'push_date': infos['date'], 'push_date': infos['date'],
'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 Document %s: pushed to %s' % (document.id, pingen.url))
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
@@ -142,19 +144,19 @@ class pingen_task(orm.Model):
Wrapper method for multiple ids (when triggered from button for Wrapper method for multiple ids (when triggered from button for
instance) for public interface. instance) for public interface.
""" """
for task in self.browse(cr, uid, ids, context=context): for document in self.browse(cr, uid, ids, context=context):
try: try:
self._push_to_pingen(cr, uid, task, context=context) self._push_to_pingen(cr, uid, document, context=context)
except ConnectionError as e: except ConnectionError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Connection Error'), _('Pingen Connection Error'),
_('Connection Error when asking for sending the document %s to Pingen') % task.name) _('Connection Error when asking for sending the document %s to Pingen') % document.name)
except APIError as e: except APIError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Error'), _('Pingen Error'),
_('Error when asking Pingen to send the document %s: ' _('Error when asking Pingen to send the document %s: '
'\n%s') % (task.name, e)) '\n%s') % (document.name, e))
return True return True
def _push_and_send_to_pingen_silent(self, cr, uid, ids, context=None): def _push_and_send_to_pingen_silent(self, cr, uid, ids, context=None):
@@ -168,48 +170,48 @@ class pingen_task(orm.Model):
# do not retry pingen_error, they should be treated manually # do not retry pingen_error, they should be treated manually
[('state', 'in', ['pending', 'pushed', 'error'])], [('state', 'in', ['pending', 'pushed', 'error'])],
context=context) context=context)
for task in self.browse(cr, uid, ids, context=context): for document in self.browse(cr, uid, ids, context=context):
try: try:
if not task.pingen_id: if not document.pingen_id:
self._push_to_pingen(cr, uid, task, context=context) self._push_to_pingen(cr, uid, document, context=context)
if not task.post_id and task.pingen_send: if not document.post_id and document.pingen_send:
self._ask_pingen_send(cr, uid, task, context=context) self._ask_pingen_send(cr, uid, document, context=context)
except ConnectionError as e: except ConnectionError as e:
task.write({'last_error_message': e, 'state': 'error'}, context=context) document.write({'last_error_message': e, 'state': 'error'}, context=context)
except APIError as e: except APIError as e:
task.write({'last_error_message': e, 'state': 'pingen_error'}, context=context) document.write({'last_error_message': e, 'state': 'pingen_error'}, context=context)
return True return True
def _ask_pingen_send(self, cr, uid, task, context=None): def _ask_pingen_send(self, cr, uid, document, context=None):
""" For a document already pushed to pingen, ask to send it. """ For a document already pushed to pingen, ask to send it.
""" """
# sending has been explicitely asked so we change the option # sending has been explicitely asked so we change the option
# for consistency # for consistency
if not task.pingen_send: if not document.pingen_send:
task.write({'pingen_send': True}, context=context) document.write({'pingen_send': True}, context=context)
pingen = self._pingen(cr, uid, [], context=context) pingen = self._pingen(cr, uid, [], context=context)
try: try:
post_id = pingen.send_document( post_id = pingen.send_document(
task.pingen_id, document.pingen_id,
task.pingen_speed, document.pingen_speed,
task.pingen_color) document.pingen_color)
except ConnectionError as e: except ConnectionError as e:
_logger.exception('Connection Error when asking for sending Pingen Task %s to %s.' % _logger.exception('Connection Error when asking for sending Pingen Document %s to %s.' %
(task.id, pingen.url)) (document.id, pingen.url))
raise raise
except APIError as e: except APIError as e:
_logger.exception('API Error when asking for sending Pingen Task %s to %s.' % _logger.exception('API Error when asking for sending Pingen Document %s to %s.' %
(task.id, pingen.url)) (document.id, pingen.url))
raise raise
task.write( document.write(
{'last_error_message': False, {'last_error_message': False,
'state': 'sendcenter', 'state': 'sendcenter',
'post_id': post_id}, 'post_id': post_id},
context=context) context=context)
_logger.info('Pingen Task %s: asked for sending to %s' % (task.id, pingen.url)) _logger.info('Pingen Document %s: asked for sending to %s' % (document.id, pingen.url))
return True return True
@@ -219,37 +221,41 @@ class pingen_task(orm.Model):
Wrapper method for multiple ids (when triggered from button for Wrapper method for multiple ids (when triggered from button for
instance) for public interface. instance) for public interface.
""" """
for task in self.browse(cr, uid, ids, context=context): for document in self.browse(cr, uid, ids, context=context):
try: try:
self._ask_pingen_send(cr, uid, task, context=context) self._ask_pingen_send(cr, uid, document, context=context)
except ConnectionError as e: except ConnectionError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Connection Error'), _('Pingen Connection Error'),
_('Connection Error when asking for sending the document %s to Pingen') % task.name) _('Connection Error when asking for '
'sending the document %s to Pingen') % document.name)
except APIError as e: except APIError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Error'), _('Pingen Error'),
_('Error when asking Pingen to send the document %s: ' _('Error when asking Pingen to send the document %s: '
'\n%s') % (task.name, e)) '\n%s') % (document.name, e))
return True return True
def _update_post_infos(self, cr, uid, task, context=None): def _update_post_infos(self, cr, uid, document, context=None):
""" Update the informations from pingen of a document in the Sendcenter """ Update the informations from pingen of a document in the Sendcenter
""" """
if not task.post_id: if not document.post_id:
return return
pingen = self._pingen(cr, uid, [], context=context) pingen = self._pingen(cr, uid, [], context=context)
try: try:
post_infos = pingen.post_infos(task.post_id) post_infos = pingen.post_infos(document.post_id)
except ConnectionError as e: except ConnectionError as e:
_logger.exception('Connection Error when asking for sending Pingen Task %s to %s.' % _logger.exception(
(task.id, pingen.url)) 'Connection Error when asking for '
'sending Pingen Document %s to %s.' %
(document.id, pingen.url))
raise raise
except APIError as e: except APIError as e:
_logger.exception('API Error when asking for sending Pingen Task %s to %s.' % _logger.exception(
(task.id, pingen.url)) 'API Error when asking for sending Pingen Document %s to %s.' %
(document.id, pingen.url))
raise raise
currency_ids = self.pool.get('res.currency').search( currency_ids = self.pool.get('res.currency').search(
@@ -268,8 +274,8 @@ class pingen_task(orm.Model):
if pingen.is_posted(post_infos): if pingen.is_posted(post_infos):
vals['state'] = 'sent' vals['state'] = 'sent'
task.write(vals, context=context) document.write(vals, context=context)
_logger.info('Pingen Task %s: status updated' % task.id) _logger.info('Pingen Document %s: status updated' % document.id)
def _update_post_infos_silent(self, cr, uid, ids, context=None): def _update_post_infos_silent(self, cr, uid, ids, context=None):
""" Update the informations from pingen of a document in the Sendcenter """ Update the informations from pingen of a document in the Sendcenter
@@ -282,9 +288,9 @@ class pingen_task(orm.Model):
[('state', '=', 'sendcenter')], [('state', '=', 'sendcenter')],
context=context) context=context)
for task in self.browse(cr, uid, ids, context=context): for document in self.browse(cr, uid, ids, context=context):
try: try:
self._update_post_infos(cr, uid, task, context=context) self._update_post_infos(cr, uid, document, context=context)
except (ConnectionError, APIError): except (ConnectionError, APIError):
# Intended silented exception, we can consider that it's not # Intended silented exception, we can consider that it's not
# important if the update not worked, that's # important if the update not worked, that's
@@ -299,19 +305,19 @@ class pingen_task(orm.Model):
Wrapper method for multiple ids (when triggered from button for Wrapper method for multiple ids (when triggered from button for
instance) for public interface. instance) for public interface.
""" """
for task in self.browse(cr, uid, ids, context=context): for document in self.browse(cr, uid, ids, context=context):
try: try:
self._update_post_infos(cr, uid, task, context=context) self._update_post_infos(cr, uid, document, context=context)
except ConnectionError as e: except ConnectionError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Connection Error'), _('Pingen Connection Error'),
_('Connection Error when updating the status of Document %s' _('Connection Error when updating the status of Document %s'
' from Pingen') % task.name) ' from Pingen') % document.name)
except APIError as e: except APIError as e:
raise osv.except_osv( raise osv.except_osv(
_('Pingen Error'), _('Pingen Error'),
_('Error when updating the status of Document %s from Pingen: ' _('Error when updating the status of Document %s from Pingen: '
'\n%s') % (task.name, e)) '\n%s') % (document.name, e))
return True return True

View File

@@ -2,12 +2,12 @@
<openerp> <openerp>
<data noupdate="0"> <data noupdate="0">
<record id="view_pingen_task_tree" model="ir.ui.view"> <record id="view_pingen_document_tree" model="ir.ui.view">
<field name="name">pingen.task.tree</field> <field name="name">pingen.document.tree</field>
<field name="model">pingen.task</field> <field name="model">pingen.document</field>
<field name="type">tree</field> <field name="type">tree</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Pingen Task"> <tree string="Pingen Document">
<field name="name"/> <field name="name"/>
<field name="datas_fname"/> <field name="datas_fname"/>
<field name="pingen_send"/> <field name="pingen_send"/>
@@ -18,12 +18,12 @@
</field> </field>
</record> </record>
<record id="view_pingen_task_form" model="ir.ui.view"> <record id="view_pingen_document_form" model="ir.ui.view">
<field name="name">pingen.task.form</field> <field name="name">pingen.document.form</field>
<field name="model">pingen.task</field> <field name="model">pingen.document</field>
<field name="type">form</field> <field name="type">form</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Pingen Task"> <form string="Pingen Document">
<group colspan="4" col="6"> <group colspan="4" col="6">
<field name="name" readonly="True"/> <field name="name" readonly="True"/>
<field name="type" readonly="True"/> <field name="type" readonly="True"/>
@@ -111,12 +111,12 @@
</field> </field>
</record> </record>
<record id="view_pingen_task_search" model="ir.ui.view"> <record id="view_pingen_document_search" model="ir.ui.view">
<field name="name">pingen.task.search</field> <field name="name">pingen.document.search</field>
<field name="model">pingen.task</field> <field name="model">pingen.document</field>
<field name="type">search</field> <field name="type">search</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<search string="Pingen Task"> <search string="Pingen Document">
<filter icon="terp-stage" <filter icon="terp-stage"
string="Pending" string="Pending"
domain="[('state','=','pending')]"/> domain="[('state','=','pending')]"/>
@@ -132,16 +132,16 @@
</field> </field>
</record> </record>
<record id="action_pingen_task" model="ir.actions.act_window"> <record id="action_pingen_document" model="ir.actions.act_window">
<field name="name">Pingen Tasks</field> <field name="name">Pingen Documents</field>
<field name="type">ir.actions.act_window</field> <field name="type">ir.actions.act_window</field>
<field name="res_model">pingen.task</field> <field name="res_model">pingen.document</field>
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
<field name="search_view_id" ref="view_pingen_task_search"/> <field name="search_view_id" ref="view_pingen_document_search"/>
</record> </record>
<menuitem action="action_pingen_task" id="menu_pingen_task" parent="base.next_id_4"/> <menuitem action="action_pingen_document" id="menu_pingen_document" parent="base.next_id_4"/>
</data> </data>
</openerp> </openerp>