[add] move batch validate: now you can cancel all jobs that are not done yet

This commit is contained in:
Leonardo Pistone
2014-01-14 12:21:47 +01:00
committed by Artem Kostyuk
parent b0fb606c51
commit a1b2ed5e79
3 changed files with 59 additions and 22 deletions

View File

@@ -21,9 +21,11 @@
"""Accounting customisation for delayed posting."""
from openerp.osv import fields, orm
from openerp.tools.translate import _
from openerp.addons.connector.queue.job import job
from openerp.addons.connector.session import ConnectorSession
from openerp.addons.connector.queue.job import OpenERPJobStorage
class account_move(orm.Model):
@@ -43,7 +45,7 @@ class account_move(orm.Model):
),
}
def _delay_post_marked(self, cr, uid, context=None):
def _delay_post_marked(self, cr, uid, eta=None, context=None):
"""Create a job for every move marked for posting.
If some moves already have a job, they are skipped.
@@ -62,11 +64,38 @@ class account_move(orm.Model):
], context=context)
for move_id in move_ids:
job_uuid = validate_one_move.delay(session, self._name, move_id)
job_uuid = validate_one_move.delay(session, self._name, move_id,
eta=eta)
self.write(cr, uid, [move_id], {
'post_job_uuid': job_uuid
})
def _cancel_jobs(self, cr, uid, context=None):
"""Find moves where the mark has been removed and cancel the jobs.
For the moves that are posted already it's too late: we skip them.
"""
if context is None:
context = {}
session = ConnectorSession(cr, uid, context=context)
storage = OpenERPJobStorage(session)
move_ids = self.search(cr, uid, [
('to_post', '=', False),
('post_job_uuid', '!=', False),
('state', '=', 'draft'),
], context=context)
for move in self.browse(cr, uid, move_ids, context=context):
job = storage.load(move.post_job_uuid)
if job.state in (u'pending', u'enqueued'):
job.set_done(result=_(
u'Task set to Done because the user unmarked the move'
))
def mark_for_posting(self, cr, uid, move_ids, context=None):
"""Mark a list of moves for delayed posting, and enqueue the jobs."""
if context is None:
@@ -74,6 +103,13 @@ class account_move(orm.Model):
self.write(cr, uid, move_ids, {'to_post': True}, context=context)
self._delay_post_marked(cr, uid, context=context)
def unmark_for_posting(self, cr, uid, move_ids, context=None):
"""Unmark moves for delayed posting, and cancel the jobs."""
if context is None:
context = {}
self.write(cr, uid, move_ids, {'to_post': False}, context=context)
self._cancel_jobs(cr, uid, context=context)
@job
def validate_one_move(session, model_name, move_id):

View File

@@ -9,6 +9,7 @@
<field name="arch" type="xml">
<field name="to_check" position="after">
<field name="to_post"/>
<field name="post_job_uuid"/>
</field>
</field>
</record>
@@ -20,6 +21,7 @@
<field name="arch" type="xml">
<field name="to_check" position="after">
<field name="to_post"/>
<field name="post_job_uuid"/>
</field>
</field>
</record>

View File

@@ -50,28 +50,27 @@ class AccountMoveMarker(orm.TransientModel):
move_obj = self.pool['account.move']
if wiz.action == 'mark':
domain = []
if wiz.journal_ids:
domain.append((
'journal_id',
'in',
[journal.id for journal in wiz.journal_ids]
))
domain = [('state', '=', 'draft')]
move_ids = move_obj.search(cr, uid, domain, context=context)
move_obj.mark_for_posting(cr, uid, move_ids, context=context)
return {'type': 'ir.actions.act_window_close'}
elif wiz.action == 'unmark':
# TODO
raise NotImplementedError(
'The Unmark action is not implemented yet'
)
if wiz.filter != 'filter_no':
# TODO
raise NotImplementedError(
'Date and period filter are not implemented yet'
'Date and period filters are not implemented yet'
)
if wiz.journal_ids:
domain.append((
'journal_id',
'in',
[journal.id for journal in wiz.journal_ids]
))
move_ids = move_obj.search(cr, uid, domain, context=context)
if wiz.action == 'mark':
move_obj.mark_for_posting(cr, uid, move_ids, context=context)
elif wiz.action == 'unmark':
move_obj.unmark_for_posting(cr, uid, move_ids, context=context)
return {'type': 'ir.actions.act_window_close'}