Browse Source

Prevent to apply a revision if it has prior pending revisions

pull/208/head
Guewen Baconnier 9 years ago
parent
commit
fd47e69496
  1. 23
      partner_revision/models/res_partner_revision.py
  2. 14
      partner_revision/tests/test_revision_flow.py

23
partner_revision/models/res_partner_revision.py

@ -33,11 +33,13 @@ class ResPartnerRevision(models.Model):
partner_id = fields.Many2one(comodel_name='res.partner', partner_id = fields.Many2one(comodel_name='res.partner',
string='Partner', string='Partner',
select=True,
required=True) required=True)
change_ids = fields.One2many(comodel_name='res.partner.revision.change', change_ids = fields.One2many(comodel_name='res.partner.revision.change',
inverse_name='revision_id', inverse_name='revision_id',
string='Changes') string='Changes')
date = fields.Datetime(default=fields.Datetime.now)
date = fields.Datetime(default=fields.Datetime.now,
select=True)
state = fields.Selection( state = fields.Selection(
compute='_compute_state', compute='_compute_state',
selection=[('draft', 'Pending'), selection=[('draft', 'Pending'),
@ -247,7 +249,24 @@ class ResPartnerRevisionChange(models.Model):
for change in self: for change in self:
if change.state in ('cancel', 'done'): if change.state in ('cancel', 'done'):
continue continue
partner = change.revision_id.partner_id
revision = change.revision_id
previous_revisions = self.env['res.partner.revision'].search(
[('date', '<', revision.date),
('state', '=', 'draft'),
('partner_id', '=', revision.partner_id.id),
],
limit=1,
)
if previous_revisions:
raise exceptions.Warning(
_('This change cannot be applied because a previous '
'revision for the same partner is pending.\n'
'Apply all the anterior revisions before applying '
'this one.')
)
partner = revision.partner_id
value_for_write = change._convert_value_for_write( value_for_write = change._convert_value_for_write(
change.get_new_value() change.get_new_value()
) )

14
partner_revision/tests/test_revision_flow.py

@ -19,7 +19,9 @@
# #
# #
from datetime import datetime, timedelta
from openerp import fields, exceptions
from openerp.tests import common from openerp.tests import common
from .common import RevisionMixin from .common import RevisionMixin
@ -213,3 +215,15 @@ class TestRevisionFlow(RevisionMixin, common.TransactionCase):
self.assertEqual(revision.state, 'draft') self.assertEqual(revision.state, 'draft')
revision.apply() revision.apply()
self.assertEqual(revision.state, 'done') self.assertEqual(revision.state, 'done')
def test_apply_revision_with_other_pending(self):
""" Error when applying when previous pending revisions exist """
changes = [(self.field_name, 'Y', 'draft')]
old_revision = self._create_revision(self.partner, changes)
# if the date is the same, both revision can be applied
to_string = fields.Datetime.to_string
old_revision.date = to_string(datetime.now() - timedelta(days=1))
changes = [(self.field_name, 'Z', 'draft')]
revision = self._create_revision(self.partner, changes)
with self.assertRaises(exceptions.Warning):
revision.change_ids.apply()
Loading…
Cancel
Save