From 501ec2a5347d73b634a664821e5cbf6dc70f254d Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Mon, 31 Dec 2018 11:11:16 +0000 Subject: [PATCH] Make all options descend from 'all templates' --- app/main/forms.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 18ab60fa7..dd4fe0403 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -747,18 +747,25 @@ class NestedRadioField(RadioFieldWithNoneOption): super().__init__(*args, **kwargs) def children(self): - child_map = {} - child_ids = [ - folder['id'] for folder in self.all_template_folders - if folder['parent_id'] is None] - - child_map[None] = [option for idx, option in enumerate(self) if option.data in child_ids or idx == 0] + # start map with root option as a single child entry + child_map = {None: [option for option in self + if option.data == self.NONE_OPTION_VALUE]} + # add entries for all other children for option in self: - child_ids = [ - folder['id'] for folder in self.all_template_folders - if folder['parent_id'] == option.data] - child_map[option.data] = [option for option in self if option.data in child_ids] + if option.data == self.NONE_OPTION_VALUE: + child_ids = [ + folder['id'] for folder in self.all_template_folders + if folder['parent_id'] is None] + key = self.NONE_OPTION_VALUE + else: + child_ids = [ + folder['id'] for folder in self.all_template_folders + if folder['parent_id'] == option.data] + key = option.data + + child_map[key] = [option for option in self if option.data in child_ids] + return child_map