Browse Source

[ADD] Constraint on date range of childs with respect to date range of parent

pull/1431/head
Nikos Tsirintanis 6 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
18075ff3c1
  1. 25
      date_range/models/date_range.py
  2. 51
      date_range/tests/test_date_range.py

25
date_range/models/date_range.py

@ -55,6 +55,31 @@ class DateRange(models.Model):
_('The Company in the Date Range and in ' _('The Company in the Date Range and in '
'Date Range Type must be the same.')) 'Date Range Type must be the same.'))
@api.constrains('parent_id', 'date_start', 'date_end')
def _validate_child_range(self):
for this in self:
if not this.parent_id:
continue
start = this.parent_id.date_start <= this.date_start
end = this.parent_id.date_end >= this.date_end
child_range = start and end
if not child_range:
if (not start) and end:
text = _("Start dates are not compatible (%s < %s)") % (
this.date_start, this.parent_id.date_start)
elif (not end) and start:
text = _("End dates are not compatible (%s > %s)") % (
this.date_end, this.parent_id.date_end)
else:
text = _("%s range not in %s - %s") % (
this.name,
this.parent_id.date_start,
this.parent_id.date_end,
)
raise ValidationError(
_("%s not a subrange of %s: " + text) % (
this.name, this.parent_id.name))
@api.constrains('type_id', 'date_start', 'date_end', 'company_id') @api.constrains('type_id', 'date_start', 'date_end', 'company_id')
def _validate_range(self): def _validate_range(self):
for this in self: for this in self:

51
date_range/tests/test_date_range.py

@ -138,21 +138,52 @@ class DateRangeTest(TransactionCase):
'date_end': '2018-12-31', 'date_end': '2018-12-31',
'type_id': self.type.id, 'type_id': self.type.id,
}) })
# Check here that a validation error is thrown
# when parent and child have same type_id
with self.assertRaises(ValidationError):
self.date_range.create({
'name': 'FS2018-period1',
'date_start': '2018-01-01',
'date_end': '2018-04-30',
'type_id': self.type.id,
'parent_id': parent.id,
})
type_block = date_range_type.create({ type_block = date_range_type.create({
'name': 'FS2018-type_block', 'name': 'FS2018-type_block',
'company_id': False, 'company_id': False,
'allow_overlap': False, 'allow_overlap': False,
}) })
# Check here all three validation errors thrown
# when child range is not a subrange of parent
with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint():
self.date_range.create({
'name': 'FS2018-period1',
'date_start': '2018-06-06',
'date_end': '2019-01-02',
'type_id': type_block.id,
'parent_id': parent.id,
})
self.assertEqual(
cm.exception.name,
'FS2018-period1 not a subrange of FS2018: '
'End dates are not compatible (2019-01-02 > 2018-12-31)'
)
with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint():
self.date_range.create({
'name': 'FS2018-period1',
'date_start': '2017-06-06',
'date_end': '2018-01-02',
'type_id': type_block.id,
'parent_id': parent.id,
})
self.assertEqual(
cm.exception.name,
'FS2018-period1 not a subrange of FS2018: '
'Start dates are not compatible (2017-06-06 < 2018-01-01)'
)
with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint():
self.date_range.create({
'name': 'FS2018-period1',
'date_start': '2017-06-06',
'date_end': '2019-01-02',
'type_id': type_block.id,
'parent_id': parent.id,
})
self.assertEqual(
cm.exception.name,
'FS2018-period1 not a subrange of FS2018: '
'FS2018-period1 range not in 2018-01-01 - 2018-12-31'
)
period1 = self.date_range.create({ period1 = self.date_range.create({
'name': 'FS2018-period1', 'name': 'FS2018-period1',
'date_start': '2018-01-01', 'date_start': '2018-01-01',

Loading…
Cancel
Save