def __index_context(self): switches = map(SwitchForm.from_object, manager.switches) switches = sorted(switches, key=lambda x: x.field('name')) new_switch = SwitchForm() new_switch.conditions = ConditionFormSet() return {"switches": switches, "new_switch": new_switch}
def __index_context(self): switches = map(SwitchForm.from_object, manager.switches) new_switch = SwitchForm() new_switch.conditions = ConditionFormSet() switches = sorted(switches, key=lambda x: x.field('name')) seperator = manager.key_separator for i, switch in enumerate(switches): current = switch.field('name').split(manager.key_separator) if i == 0: depth = 0 else: previous_switch = switches[i - 1] previous = previous_switch.field('name').split(manager.key_separator) depth = len(list(takewhile(lambda x: x[0] == x[1], zip(previous, current)))) switch.depth = depth switch.prefix_depth = depth - 1 switch.path = current switch.path_prefix = seperator.join(current[:depth]) switch.path_leaf = seperator.join(current[depth:]) def nest_switch(d, depth, switch): name = seperator.join(switch.path[:depth]) d = d.setdefault(name, SwitchDict()) if depth < len(switch.path): nest_switch(d, depth + 1, switch) else: d.set_switch(switch) d = SwitchDict() for switch in switches: nest_switch(d, 1, switch) return { "switches": switches, "switchdict": d, "new_switch": new_switch }
def __index_context(self): switches = map(SwitchForm.from_object, manager.switches) new_switch = SwitchForm() new_switch.conditions = ConditionFormSet() return {"switches": switches, "new_switch": new_switch}
def switch_form(self): return SwitchForm(self.post_data)
def test_only_allow_alphanumeric_and_underscore_switch_names(self): self.post_data['name'] = 'ಠ_ಠ' self.assertFalse(SwitchForm(self.post_data).is_valid()) self.post_data['name'] = 'i_am_dissapoint' self.assertTrue(SwitchForm(self.post_data).is_valid())