From 774ca18fd70f4d4a3523cf3e97014b33489b7034 Mon Sep 17 00:00:00 2001 From: Nikos Tsirintanis Date: Thu, 4 Oct 2018 15:17:33 +0200 Subject: [PATCH] [IMP] Added hierarchical feature for date_range, via parent_id --- date_range/models/date_range.py | 2 ++ date_range/readme/CONTRIBUTORS.rst | 1 + date_range/tests/test_date_range.py | 48 ++++++++++++++++++++++++++++ date_range/views/date_range_view.xml | 2 ++ 4 files changed, 53 insertions(+) diff --git a/date_range/models/date_range.py b/date_range/models/date_range.py index 29da5b600..a88128013 100644 --- a/date_range/models/date_range.py +++ b/date_range/models/date_range.py @@ -31,6 +31,8 @@ class DateRange(models.Model): active = fields.Boolean( help="The active field allows you to hide the date range without " "removing it.", default=True) + parent_id = fields.Many2one( + comodel_name='date.range', string="Parent", index=1) _sql_constraints = [ ('date_range_uniq', 'unique (name,type_id, company_id)', diff --git a/date_range/readme/CONTRIBUTORS.rst b/date_range/readme/CONTRIBUTORS.rst index 38b10b316..72039de31 100644 --- a/date_range/readme/CONTRIBUTORS.rst +++ b/date_range/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Alexis de Lattre * Miquel Raïch * Andrea Stirpe +* Nikos Tsirintanis diff --git a/date_range/tests/test_date_range.py b/date_range/tests/test_date_range.py index 1b774b6f2..55912fc53 100644 --- a/date_range/tests/test_date_range.py +++ b/date_range/tests/test_date_range.py @@ -129,3 +129,51 @@ class DateRangeTest(TransactionCase): 'type_id': self.typeB.id, 'company_id': self.company_2.id, }) + + def test_parent_id(self): + date_range_type = self.env['date.range.type'] + parent = self.date_range.create({ + 'name': 'FS2018', + 'date_start': '2018-01-01', + 'date_end': '2018-12-31', + '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({ + 'name': 'FS2018-type_block', + 'company_id': False, + 'allow_overlap': False, + }) + period1 = self.date_range.create({ + 'name': 'FS2018-period1', + 'date_start': '2018-01-01', + 'date_end': '2018-04-30', + 'type_id': type_block.id, + 'parent_id': parent.id, + }) + # Check here that a validation error is thrown + # when child periods overlap + with self.assertRaises(ValidationError) as cm, self.env.cr.savepoint(): + self.date_range.create({ + 'name': 'FS2018-period2', + 'date_start': '2018-02-01', + 'date_end': '2018-03-15', + 'type_id': type_block.id, + 'parent_id': parent.id, + }) + self.assertEqual( + cm.exception.name, + 'FS2018-period2 overlaps FS2018-period1' + ) + # Ensure here that parent and children are of different type + self.assertNotEqual(parent.type_id, period1.type_id) + self.assertEqual(parent.type_id, period1.parent_id.type_id) diff --git a/date_range/views/date_range_view.xml b/date_range/views/date_range_view.xml index 97e627e6a..c7506abbb 100644 --- a/date_range/views/date_range_view.xml +++ b/date_range/views/date_range_view.xml @@ -10,6 +10,7 @@ + @@ -25,6 +26,7 @@ +