(self, loc_types)
return ordered_loc_types
Return loc types in order from parents to children
Return loc types in order from parents to children
Return loc types in order from parents to children
def get_hierarchy(self, loc_types):
        """
        Return loc types in order from parents to children
        """
        assert_no_cycles([
            (lt['pk'], lt['parent_type'])
            if lt['parent_type'] else
            (lt['pk'], ROOT_LOCATION_TYPE)
            for lt in loc_types
        ])

        lt_dict = {lt['pk']: lt for lt in loc_types}
        hierarchy = {}

        def insert_loc_type(loc_type):
            """
            Get parent location's hierarchy, insert loc_type into it,
            and return hierarchy below loc_type
            """
            name = loc_type['name']
            parent = lt_dict.get(loc_type['parent_type'], None)
            if not parent:
                lt_hierarchy = hierarchy
            else:
                lt_hierarchy = insert_loc_type(parent)
            if name not in lt_hierarchy:
                lt_hierarchy[name] = (loc_type, {})
            return lt_hierarchy[name][1]

        for loc_type in loc_types:
            insert_loc_type(loc_type)

        ordered_loc_types = []

        def step_through_graph(hierarchy):
            for name, (loc_type, children) in hierarchy.items():
                ordered_loc_types.append(loc_type)
                step_through_graph(children)

        step_through_graph(hierarchy)
        return ordered_loc_types
def get_hierarchy ( self , loc_types ) : assert_no_cycles ( [ ( lt [ 'pk' ] , lt [ 'parent_type' ] ) if lt [ 'parent_type' ] else ( lt [ 'pk' ] , ROOT_LOCATION_TYPE ) for lt in loc_types ] ) lt_dict = { lt [ 'pk' ] : lt for lt in loc_types } hierarchy = { } def insert_loc_type ( loc_type ) : """
            Get parent location's hierarchy, insert loc_type into it,
            and return hierarchy below loc_type
            """ name = loc_type [ 'name' ] parent = lt_dict . get ( loc_type [ 'parent_type' ] , None ) if not parent : lt_hierarchy = hierarchy else : lt_hierarchy = insert_loc_type ( parent ) if name not in lt_hierarchy : lt_hierarchy [ name ] = ( loc_type , { } ) return lt_hierarchy [ name ] [ 1 ] for loc_type in loc_types : insert_loc_type ( loc_type ) ordered_loc_types = [ ] def step_through_graph ( hierarchy ) : for name , ( loc_type , children ) in hierarchy . items ( ) : ordered_loc_types . append ( loc_type ) step_through_graph ( children ) step_through_graph ( hierarchy ) return ordered_loc_types