File size: 2,515 Bytes
e2e8616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from src.model.container import Container

INFINITE = 99999

def create_dic_levels(c:Container,dict_of_levels : dict = {}):
    if c.level == 0:
        dict_of_levels[c.level] = [0]
    for child in c.children:
        if child.level not in dict_of_levels:
            dict_of_levels[child.level] = [1 for _ in range(child.level)]
        create_dic_levels(child, dict_of_levels)
    if INFINITE in dict_of_levels.keys():
        dict_of_levels[INFINITE] = [1]
    return dict_of_levels


def create_good_indexes(c:Container, dict_of_levels : dict):
    actual_level = c.level
    c.index = dict_of_levels[actual_level].copy()
    actual_len = len(dict_of_levels[actual_level])
    temp_update = dict_of_levels[actual_level][-1]
    dict_of_levels[actual_level][-1] += 1
    for i in dict_of_levels.values():
        if len(i) > actual_len:
            i[actual_len - 1] = temp_update
    for child in c.children:
        c_lvl = child.level
        for i in dict_of_levels.values():
            if len(i) > c_lvl:
                i[c_lvl:] = [1 for _ in range(len(i[c_lvl:]))]
        create_good_indexes(child, dict_of_levels)  # Apply the function recursively to all children


def create_good_indexes_not_ordered_titles(c:Container, dict_of_levels : dict):
    actual_level = c.level
    c.index = dict_of_levels[actual_level].copy()
    actual_len = len(dict_of_levels[actual_level])
    temp_update = dict_of_levels[actual_level][-1]
    dict_of_levels[actual_level][-1] += 1
    for i in dict_of_levels.values():
        if len(i) > actual_len:
            i[actual_len - 1] = temp_update
    for child in c.children:
        c_lvl = child.level
        for i in dict_of_levels.values():
            if len(i) > c_lvl:
                i[c_lvl:] = [1 for _ in range(len(i[c_lvl:]))]
        create_good_indexes(child, dict_of_levels)  # Apply the function recursively to all children


def set_good_block_indexes(c:Container):
    for i in c.containers:
        for b in i.blocks:
            b.index = i.index


def set_indexes(c:Container):
    dict_levels = create_dic_levels(c)
    myKeys = list(dict_levels.keys())
    myKeys.sort()
    dict_levels = {key: dict_levels[key] for key in myKeys}
    if c.children and c.children[0] and (c.children[0].level > min(list(dict_levels.keys())[1:])):
        c.children[0].level = min(list(dict_levels.keys())[1:])
        create_good_indexes_not_ordered_titles(c, dict_levels)
    else:
        create_good_indexes(c, dict_levels)
    set_good_block_indexes(c)