From fd47e69496b3a49fda176e12d36043b6624660ae Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 18 Sep 2015 10:25:29 +0200 Subject: [PATCH] Prevent to apply a revision if it has prior pending revisions --- .../models/res_partner_revision.py | 23 +++++++++++++++++-- partner_revision/tests/test_revision_flow.py | 14 +++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/partner_revision/models/res_partner_revision.py b/partner_revision/models/res_partner_revision.py index 08fb8e33c..adcd78ad7 100644 --- a/partner_revision/models/res_partner_revision.py +++ b/partner_revision/models/res_partner_revision.py @@ -33,11 +33,13 @@ class ResPartnerRevision(models.Model): partner_id = fields.Many2one(comodel_name='res.partner', string='Partner', + select=True, required=True) change_ids = fields.One2many(comodel_name='res.partner.revision.change', inverse_name='revision_id', string='Changes') - date = fields.Datetime(default=fields.Datetime.now) + date = fields.Datetime(default=fields.Datetime.now, + select=True) state = fields.Selection( compute='_compute_state', selection=[('draft', 'Pending'), @@ -247,7 +249,24 @@ class ResPartnerRevisionChange(models.Model): for change in self: if change.state in ('cancel', 'done'): 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( change.get_new_value() ) diff --git a/partner_revision/tests/test_revision_flow.py b/partner_revision/tests/test_revision_flow.py index c3d6d209c..9bf9964fc 100644 --- a/partner_revision/tests/test_revision_flow.py +++ b/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 .common import RevisionMixin @@ -213,3 +215,15 @@ class TestRevisionFlow(RevisionMixin, common.TransactionCase): self.assertEqual(revision.state, 'draft') revision.apply() 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()