Browse Source

[FIX] Corrected parent-child constraints and warning messages

pull/1431/head
Nikos Tsirintanis 6 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
6b2d4d8680
  1. 38
      date_range/models/date_range.py
  2. 6
      date_range/models/date_range_type.py
  3. 11
      date_range/tests/test_date_range.py

38
date_range/models/date_range.py

@ -37,8 +37,6 @@ class DateRange(models.Model):
readonly=True)
parent_id = fields.Many2one(
comodel_name='date.range', string="Parent",
domain="['|', ('type_id.parent_type_id', '!=', parent_type_id), "
"('parent_type_id', '=', False)]",
index=1)
_sql_constraints = [
@ -71,21 +69,35 @@ class DateRange(models.Model):
end = this.parent_id.date_end >= this.date_end
child_range = start and end
if not child_range:
text_dict = {
'name': this.name,
'start': this.date_start,
'end': this.date_end,
'parent_name': this.parent_id.name,
'parent_start': this.parent_id.date_start,
'parent_end': this.parent_id.date_end,
}
if (not start) and end:
text = _("Start dates are not compatible (%s < %s)") % (
this.date_start, this.parent_id.date_start)
text = _(
"Start date %(start)s of %(name)s must be greater than"
" or equal to "
"start date %(parent_start)s of %(parent_name)s"
) % text_dict
elif (not end) and start:
text = _("End dates are not compatible (%s > %s)") % (
this.date_end, this.parent_id.date_end)
text = _(
"End date %(end)s of %(name)s must be smaller than"
" or equal to "
"end date %(parent_end)s of %(parent_name)s"
) % text_dict
else:
text = _("%s range not in %s - %s") % (
this.name,
this.parent_id.date_start,
this.parent_id.date_end,
)
text = _(
"%(name)s range not in "
"%(parent_start)s - %(parent_end)s"
) % text_dict
raise ValidationError(
_("%s not a subrange of %s: " + text) % (
this.name, this.parent_id.name))
_("%(name)s not a subrange of"
" %(parent_name)s: " % text_dict) + text
)
@api.constrains('type_id', 'date_start', 'date_end', 'company_id')
def _validate_range(self):

6
date_range/models/date_range_type.py

@ -55,9 +55,7 @@ class DateRangeType(models.Model):
while parent:
if not parent.parent_type_id:
break
if record.parent_type_id == parent:
if parent.parent_type_id == record:
raise ValidationError(
_("A type parent can not have a parent:"
" %s can not have %s as parent") % (
parent.name, record.name))
_("A type can not have itself as parent or child"))
parent = parent.parent_type_id

11
date_range/tests/test_date_range.py

@ -156,7 +156,8 @@ class DateRangeTest(TransactionCase):
self.assertEqual(
cm.exception.name,
'FS2018-period1 not a subrange of FS2018: '
'End dates are not compatible (2019-01-02 > 2018-12-31)'
'End date 2019-01-02 of FS2018-period1 must be '
'smaller than or equal to end date 2018-12-31 of FS2018'
)
with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint():
self.date_range.create({
@ -169,7 +170,8 @@ class DateRangeTest(TransactionCase):
self.assertEqual(
cm.exception.name,
'FS2018-period1 not a subrange of FS2018: '
'Start dates are not compatible (2017-06-06 < 2018-01-01)'
'Start date 2017-06-06 of FS2018-period1 must be '
'greater than or equal to start date 2018-01-01 of FS2018'
)
with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint():
self.date_range.create({
@ -215,15 +217,14 @@ class DateRangeTest(TransactionCase):
'parent_type_id': False,
})
# catch here the validation error when assigning
# a date_range_type parent, to another parent(self included)
# a child as its own parent, or vise-versa (predestination)
with self.assertRaises(ValidationError)as cm, self.env.cr.savepoint():
parent_type.write({
'parent_type_id': parent_type.id,
})
self.assertEqual(
cm.exception.name,
'A type parent can not have a parent:'
' FS2018_parent_type can not have FS2018_parent_type as parent'
'A type can not have itself as parent or child'
)
# Then, add a child type
child_type = date_range_type.create({

Loading…
Cancel
Save