[IMP] rename pingen.task to pingen.document

This commit is contained in:
@
2012-11-26 07:41:22 +01:00
committed by Anna Janiszewska
parent cf9ec1ead0
commit 0fdf47e7ae
6 changed files with 110 additions and 103 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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