[IMP] Use sql constraint to Prevent to add both same journals on a period

This commit is contained in:
Adrien Peiffer
2014-08-22 09:20:30 +02:00
parent 9340f0c86c
commit abbdae757f
2 changed files with 8 additions and 14 deletions

View File

@@ -41,6 +41,11 @@ class AccountJournalPeriod(orm.Model):
store=True, readonly=True)
}
_sql_constraints = [
('journal_period_uniq', 'unique(period_id, journal_id)',
'You can not add both same journal in a period.'),
]
def _check(self, cr, uid, ids, context=None):
return True
@@ -71,19 +76,7 @@ class AccountJournalPeriod(orm.Model):
period = self.pool.get('account.period')\
.browse(cr, uid, values['period_id'], context=context)
values.update({'name': (journal.code or journal.name)+':' +
(period.name or '')}),
if values.get('period_id') and values.get('journal_id'):
journal_period_duplicate_ids = self\
.search(cr, uid, [('period_id', '=',
values.get('period_id')),
('journal_id', '=',
values.get('journal_id'))],
context=context)
if (journal_period_duplicate_ids):
raise orm.except_orm(_('error'),
_('You can not add 2 times'
' the same journal in'
' same period.'))
(period.name or '')})
return super(AccountJournalPeriod, self).create(cr,
uid,
values,

View File

@@ -30,6 +30,7 @@
import openerp.tests.common as common
from openerp.osv import orm
from datetime import datetime
from psycopg2 import IntegrityError
DB = common.DB
ADMIN_USER_ID = common.ADMIN_USER_ID
@@ -202,6 +203,6 @@ class TestAccountConstraintChronology(common.TransactionCase):
create_journal_period(self, period_id, journal_id, context)
# I check if the exception is correctly raised at adding both same
# journal on a period
self.assertRaises(orm.except_orm,
self.assertRaises(IntegrityError,
create_journal_period,
self, period_id, journal_id, context)