Check if an existing connection could be reused Iterate over all of the connection settings and if an existing connection with the same parameters is suitable, return it :param connection_settings: the settings of the new connection :return: An existing connection or None
(connection_settings)
| 390 | |
| 391 | |
| 392 | def _find_existing_connection(connection_settings): |
| 393 | """ |
| 394 | Check if an existing connection could be reused |
| 395 | |
| 396 | Iterate over all of the connection settings and if an existing connection |
| 397 | with the same parameters is suitable, return it |
| 398 | |
| 399 | :param connection_settings: the settings of the new connection |
| 400 | :return: An existing connection or None |
| 401 | """ |
| 402 | connection_settings_bis = ( |
| 403 | (db_alias, settings.copy()) |
| 404 | for db_alias, settings in _connection_settings.items() |
| 405 | ) |
| 406 | |
| 407 | def _clean_settings(settings_dict): |
| 408 | # Only remove the name but it's important to |
| 409 | # keep the username/password/authentication_source/authentication_mechanism |
| 410 | # to identify if the connection could be shared (cfr https://github.com/MongoEngine/mongoengine/issues/2047) |
| 411 | return {k: v for k, v in settings_dict.items() if k != "name"} |
| 412 | |
| 413 | cleaned_conn_settings = _clean_settings(connection_settings) |
| 414 | for db_alias, connection_settings in connection_settings_bis: |
| 415 | db_conn_settings = _clean_settings(connection_settings) |
| 416 | if cleaned_conn_settings == db_conn_settings and _connections.get(db_alias): |
| 417 | return _connections[db_alias] |
| 418 | |
| 419 | |
| 420 | def get_db(alias=DEFAULT_CONNECTION_NAME, reconnect=False): |
no test coverage detected