Перейти к содержанию

data_grabber

MongoDataGrabber

MongoDataGrabber(
    model_out: type,
    collection: MongoTableEngine,
    settings: dict | None = None,
)
Source code in api/utils/data_grabber.py
114
115
116
117
118
119
120
121
122
def __init__(
    self,
    model_out: type,
    collection: MongoTableEngine,
    settings: dict | None = None,
):
    self.model_out = model_out
    self.collection = collection
    self.settings = settings

settings instance-attribute

settings: dict | None = settings

{ 'where': { 'fields':{ 'secret': 0, 'token': 0, # This will allow search on every field but mentioned ones }, 'operators': { 'in': 1, 'all': 1 # This will allow search using only mentioned operators } }, 'sort': { 'default': [()], 'max_fields': 2, 'fields': { 'name': 1, 'date': 1, # This will allow sort by only mentioned fields } }, 'projection': { 'default': {}, 'policy': 'force' } }

adjust_search_query

adjust_search_query(
    initial_query: str,
    filtered_query_key: str,
    allowed_values: set | None = None,
) -> str

Adjusts the search query by removing the forbidden query keys and then setting it to the required query.

:param initial_query: The initial query in str from which forbidden keys need to be removed. :param filtered_query_key: The forbidden key query need to be removed. :param allowed_values: Optimal structure for filtering by key, jsonable; allowed_values = None allows to drop the key search for user, forcing user to retrieve unfiltered data :return: The adjusted search query in string format.

Source code in api/utils/data_grabber.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
def adjust_search_query(
    initial_query: str,
    filtered_query_key: str,
    allowed_values: set | None = None,
) -> str:
    """
    Adjusts the search query by removing the forbidden query keys and then setting it to the required query.

    :param initial_query: The initial query in str from which forbidden keys need to be removed.
    :param filtered_query_key: The forbidden key query need to be removed.
    :param allowed_values: Optimal structure for filtering by key, jsonable;
      allowed_values = None allows to drop the key search for user,
      forcing user to retrieve unfiltered data
    :return: The adjusted search query in string format.
    """
    # Parse the forbidden query
    search_query = MongoDataGrabber.parse_where(initial_query) or {}
    # If no key, then update query with new key
    if filtered_query_key not in search_query:
        if allowed_values is not None:
            search_query = {
                **search_query,
                **{filtered_query_key: {"$in": list(allowed_values)}},
            }
        return json.dumps(search_query)
    if None is allowed_values:
        del search_query[
            filtered_query_key
        ]  # so None as allowed clears the query
    else:
        if (
            isinstance(search_query[filtered_query_key], dict)
            and "$in" in search_query[filtered_query_key]
        ):
            search_query[filtered_query_key]["$in"] = [
                value
                for value in search_query[filtered_query_key]["$in"]
                if value in allowed_values
            ]
        elif search_query[filtered_query_key] in allowed_values:
            pass
        else:
            raise HTTPException(
                status.HTTP_404_NOT_FOUND,
                "Нет сущностей подходящих по параметрам запроса.",
            )

    return json.dumps(search_query)