Skip to content

utils

check_node_not_dict_or_not_last_node(part, is_last, current_level)

Checks if given name is not present in the current level of the structure. Checks if given name is not the last name in the split list of parts. Checks if the given name in current level is not a dictionary.

All those checks verify if there is a need for deeper traversal.

Parameters:

Name Type Description Default
part str
required
is_last bool

flag to check if last element

required
current_level Any

current level of the traversed structure

required

Returns:

Type Description
bool

result of the check

Source code in ormar\queryset\utils.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def check_node_not_dict_or_not_last_node(
    part: str, is_last: bool, current_level: Any
) -> bool:
    """
    Checks if given name is not present in the current level of the structure.
    Checks if given name is not the last name in the split list of parts.
    Checks if the given name in current level is not a dictionary.

    All those checks verify if there is a need for deeper traversal.

    :param part:
    :type part: str
    :param is_last: flag to check if last element
    :type is_last: bool
    :param current_level: current level of the traversed structure
    :type current_level: Any
    :return: result of the check
    :rtype: bool
    """
    return (part not in current_level and not is_last) or (
        part in current_level and not isinstance(current_level[part], dict)
    )

convert_set_to_required_dict(set_to_convert)

Converts set to dictionary of required keys. Required key is Ellipsis.

Parameters:

Name Type Description Default
set_to_convert set

set to convert to dict

required

Returns:

Type Description
Dict

set converted to dict of ellipsis

Source code in ormar\queryset\utils.py
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def convert_set_to_required_dict(set_to_convert: set) -> Dict:
    """
    Converts set to dictionary of required keys.
    Required key is Ellipsis.

    :param set_to_convert: set to convert to dict
    :type set_to_convert: set
    :return: set converted to dict of ellipsis
    :rtype: Dict
    """
    new_dict = dict()
    for key in set_to_convert:
        new_dict[key] = Ellipsis
    return new_dict

extract_models_to_dict_of_lists(model_type, models, select_dict, extracted=None)

Receives a list of models and extracts all of the children and their children into dictionary of lists with children models, flattening the structure to one dict with all children models under their relation keys.

Parameters:

Name Type Description Default
model_type Type[Model]

parent model class

required
models Sequence[Model]

list of models from which related models should be extracted.

required
select_dict Dict

dictionary of related models from select_related

required
extracted Dict

dictionary with already extracted models

None

Returns:

Type Description
Dict

dictionary of lists f related models

Source code in ormar\queryset\utils.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def extract_models_to_dict_of_lists(
    model_type: Type["Model"],
    models: Sequence["Model"],
    select_dict: Dict,
    extracted: Dict = None,
) -> Dict:
    """
    Receives a list of models and extracts all of the children and their children
    into dictionary of lists with children models, flattening the structure to one dict
    with all children models under their relation keys.

    :param model_type: parent model class
    :type model_type: Type[Model]
    :param models: list of models from which related models should be extracted.
    :type models: List[Model]
    :param select_dict: dictionary of related models from select_related
    :type select_dict: Dict
    :param extracted: dictionary with already extracted models
    :type extracted: Dict
    :return: dictionary of lists f related models
    :rtype: Dict
    """
    if not extracted:
        extracted = dict()
    for model in models:
        extract_nested_models(model, model_type, select_dict, extracted)
    return extracted

extract_nested_models(model, model_type, select_dict, extracted)

Iterates over model relations and extracts all nested models from select_dict and puts them in corresponding list under relation name in extracted dict.keys

Basically flattens all relation to dictionary of all related models, that can be used on several models and extract all of their children into dictionary of lists witch children models.

Goes also into nested relations if needed (specified in select_dict).

Parameters:

Name Type Description Default
model Model

parent Model

required
model_type Type[Model]

parent model class

required
select_dict Dict

dictionary of related models from select_related

required
extracted Dict

dictionary with already extracted models

required
Source code in ormar\queryset\utils.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def extract_nested_models(  # noqa: CCR001
    model: "Model", model_type: Type["Model"], select_dict: Dict, extracted: Dict
) -> None:
    """
    Iterates over model relations and extracts all nested models from select_dict and
    puts them in corresponding list under relation name in extracted dict.keys

    Basically flattens all relation to dictionary of all related models, that can be
    used on several models and extract all of their children into dictionary of lists
    witch children models.

    Goes also into nested relations if needed (specified in select_dict).

    :param model: parent Model
    :type model: Model
    :param model_type: parent model class
    :type model_type: Type[Model]
    :param select_dict: dictionary of related models from select_related
    :type select_dict: Dict
    :param extracted: dictionary with already extracted models
    :type extracted: Dict
    """
    follow = [rel for rel in model_type.extract_related_names() if rel in select_dict]
    for related in follow:
        child = getattr(model, related)
        if not child:
            continue
        target_model = model_type.Meta.model_fields[related].to
        if isinstance(child, list):
            extracted.setdefault(target_model.get_name(), []).extend(child)
            if select_dict[related] is not Ellipsis:
                for sub_child in child:
                    extract_nested_models(
                        sub_child, target_model, select_dict[related], extracted
                    )
        else:
            extracted.setdefault(target_model.get_name(), []).append(child)
            if select_dict[related] is not Ellipsis:
                extract_nested_models(
                    child, target_model, select_dict[related], extracted
                )

get_relationship_alias_model_and_str(source_model, related_parts)

Walks the relation to retrieve the actual model on which the clause should be constructed, extracts alias based on last relation leading to target model.

Parameters:

Name Type Description Default
related_parts List

list of related names extracted from string

required
source_model Type[Model]

model from which relation starts

required

Returns:

Type Description
Tuple[str, Type["Model"], str]

table prefix, target model and relation string

Source code in ormar\queryset\utils.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def get_relationship_alias_model_and_str(
    source_model: Type["Model"], related_parts: List
) -> Tuple[str, Type["Model"], str, bool]:
    """
    Walks the relation to retrieve the actual model on which the clause should be
    constructed, extracts alias based on last relation leading to target model.
    :param related_parts: list of related names extracted from string
    :type related_parts: Union[List, List[str]]
    :param source_model: model from which relation starts
    :type source_model: Type[Model]
    :return: table prefix, target model and relation string
    :rtype: Tuple[str, Type["Model"], str]
    """
    table_prefix = ""
    is_through = False
    target_model = source_model
    previous_model = target_model
    previous_models = [target_model]
    manager = target_model.Meta.alias_manager
    for relation in related_parts[:]:
        related_field = target_model.Meta.model_fields[relation]

        if related_field.is_through:
            (previous_model, relation, is_through) = _process_through_field(
                related_parts=related_parts,
                relation=relation,
                related_field=related_field,
                previous_model=previous_model,
                previous_models=previous_models,
            )
        if related_field.is_multi:
            previous_model = related_field.through
            relation = related_field.default_target_field_name()  # type: ignore
        table_prefix = manager.resolve_relation_alias(
            from_model=previous_model, relation_name=relation
        )
        target_model = related_field.to
        previous_model = target_model
        if not is_through:
            previous_models.append(previous_model)
    relation_str = "__".join(related_parts)

    return table_prefix, target_model, relation_str, is_through

subtract_dict(current_dict, updating_dict)

Update one dict with another but with regard for nested keys.

That way nested sets are unionised, dicts updated and only other values are overwritten.

Parameters:

Name Type Description Default
current_dict Any

dict to update

required
updating_dict Any

dict with values to update

required

Returns:

Type Description
Dict

combination of both dicts

Source code in ormar\queryset\utils.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def subtract_dict(current_dict: Any, updating_dict: Any) -> Dict:  # noqa: CCR001
    """
    Update one dict with another but with regard for nested keys.

    That way nested sets are unionised, dicts updated and
    only other values are overwritten.

    :param current_dict: dict to update
    :type current_dict: Dict[str, ellipsis]
    :param updating_dict: dict with values to update
    :type updating_dict: Dict
    :return: combination of both dicts
    :rtype: Dict
    """
    for key, value in updating_dict.items():
        old_key = current_dict.get(key, {})
        new_value: Optional[Union[Dict, Set]] = None
        if not old_key:
            continue
        if isinstance(value, set) and isinstance(old_key, set):
            new_value = old_key.difference(value)
        elif isinstance(value, (set, collections.abc.Mapping)) and isinstance(
            old_key, (set, collections.abc.Mapping)
        ):
            value = (
                convert_set_to_required_dict(value)
                if not isinstance(value, collections.abc.Mapping)
                else value
            )
            old_key = (
                convert_set_to_required_dict(old_key)
                if not isinstance(old_key, collections.abc.Mapping)
                else old_key
            )
            new_value = subtract_dict(old_key, value)

        if new_value:
            current_dict[key] = new_value
        else:
            current_dict.pop(key, None)
    return current_dict

translate_list_to_dict(list_to_trans, is_order=False)

Splits the list of strings by '__' and converts them to dictionary with nested models grouped by parent model. That way each model appears only once in the whole dictionary and children are grouped under parent name.

Default required key ise Ellipsis like in pydantic.

Parameters:

Name Type Description Default
list_to_trans Union[List, Set]

input list

required
is_order bool

flag if change affects order_by clauses are they require special default value with sort order.

False

Returns:

Type Description
Dict

converted to dictionary input list

Source code in ormar\queryset\utils.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def translate_list_to_dict(  # noqa: CCR001
    list_to_trans: Union[List, Set], is_order: bool = False
) -> Dict:
    """
    Splits the list of strings by '__' and converts them to dictionary with nested
    models grouped by parent model. That way each model appears only once in the whole
    dictionary and children are grouped under parent name.

    Default required key ise Ellipsis like in pydantic.

    :param list_to_trans: input list
    :type list_to_trans: Union[List, Set]
    :param is_order: flag if change affects order_by clauses are they require special
    default value with sort order.
    :type is_order: bool
    :return: converted to dictionary input list
    :rtype: Dict
    """
    new_dict: Dict = dict()
    for path in list_to_trans:
        current_level = new_dict
        parts = path.split("__")
        def_val: Any = ...
        if is_order:
            if parts[0][0] == "-":
                def_val = "desc"
                parts[0] = parts[0][1:]
            else:
                def_val = "asc"

        for ind, part in enumerate(parts):
            is_last = ind == len(parts) - 1
            if check_node_not_dict_or_not_last_node(
                part=part, is_last=is_last, current_level=current_level
            ):
                current_level[part] = dict()
            elif part not in current_level:
                current_level[part] = def_val
            current_level = current_level[part]
    return new_dict

update(current_dict, updating_dict)

Update one dict with another but with regard for nested keys.

That way nested sets are unionised, dicts updated and only other values are overwritten.

Parameters:

Name Type Description Default
current_dict Any

dict to update

required
updating_dict Any

dict with values to update

required

Returns:

Type Description
Dict

combination of both dicts

Source code in ormar\queryset\utils.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def update(current_dict: Any, updating_dict: Any) -> Dict:  # noqa: CCR001
    """
    Update one dict with another but with regard for nested keys.

    That way nested sets are unionised, dicts updated and
    only other values are overwritten.

    :param current_dict: dict to update
    :type current_dict: Dict[str, ellipsis]
    :param updating_dict: dict with values to update
    :type updating_dict: Dict
    :return: combination of both dicts
    :rtype: Dict
    """
    if current_dict is Ellipsis:
        current_dict = dict()
    for key, value in updating_dict.items():
        if isinstance(value, collections.abc.Mapping):
            old_key = current_dict.get(key, {})
            if isinstance(old_key, set):
                old_key = convert_set_to_required_dict(old_key)
            current_dict[key] = update(old_key, value)
        elif isinstance(value, set) and isinstance(current_dict.get(key), set):
            current_dict[key] = current_dict.get(key).union(value)
        else:
            current_dict[key] = value
    return current_dict

update_dict_from_list(curr_dict, list_to_update)

Converts the list into dictionary and later performs special update, where nested keys that are sets or dicts are combined and not overwritten.

Parameters:

Name Type Description Default
curr_dict Dict

dict to update

required
list_to_update Union[List, Set]

list with values to update the dict

required

Returns:

Type Description
Dict

updated dict

Source code in ormar\queryset\utils.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def update_dict_from_list(curr_dict: Dict, list_to_update: Union[List, Set]) -> Dict:
    """
    Converts the list into dictionary and later performs special update, where
    nested keys that are sets or dicts are combined and not overwritten.

    :param curr_dict: dict to update
    :type curr_dict: Dict
    :param list_to_update: list with values to update the dict
    :type list_to_update: List[str]
    :return: updated dict
    :rtype: Dict
    """
    updated_dict = copy.copy(curr_dict)
    dict_to_update = translate_list_to_dict(list_to_update)
    update(updated_dict, dict_to_update)
    return updated_dict