From c9aa4805b490b022f0ae0b2435b0fb6d6ab9ad3a Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 31 Oct 2018 11:58:26 +0100 Subject: [PATCH] [IMP] date_range. Provide sensible domain for parent_id. --- date_range/models/date_range.py | 33 +++++++++++++++++++--------- date_range/views/date_range_view.xml | 10 +++++++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/date_range/models/date_range.py b/date_range/models/date_range.py index 38986d6dc..55cdedb19 100644 --- a/date_range/models/date_range.py +++ b/date_range/models/date_range.py @@ -142,14 +142,27 @@ class DateRange(models.Model): (field_name, '<=', self.date_end)] @api.multi - @api.onchange('type_id') + @api.onchange('company_id', 'type_id', 'date_start', 'date_end') def onchange_type_id(self): - return { - 'domain': { - 'parent_id': [ - '|', - ('parent_type_id', '=', False), - ('type_id', '=', self.type_id.parent_type_id.id), - ] - } - } + """The type_id and the dates determine the choices for parent.""" + domain = [] + if self.company_id: + domain.append(('company_id', '=', self.company_id.id)) + if self.parent_type_id: + domain.append(('type_id', '=', self.parent_type_id.id)) + if self.date_start: + domain.append('|') + domain.append(('date_start', '<=', self.date_start)) + domain.append(('date_start', '=', False)) + if self.date_end: + domain.append('|') + domain.append(('date_end', '>=', self.date_end)) + domain.append(('date_end', '=', False)) + if domain: + # If user did not select a parent already, autoselect the last + # (ordered by date_start) or only parent that applies. + if self.type_id and self.date_start and not self.parent_id: + possible_parent = self.search( + domain, limit=1, order='date_start desc') + self.parent_id = possible_parent # can be empty! + return {'domain': {'parent_id': domain}} diff --git a/date_range/views/date_range_view.xml b/date_range/views/date_range_view.xml index ceed0e194..a965dc2a8 100644 --- a/date_range/views/date_range_view.xml +++ b/date_range/views/date_range_view.xml @@ -11,7 +11,10 @@ - + @@ -28,7 +31,10 @@ - +