import pandas as pd from werkzeug.security import check_password_hash, generate_password_hash def collection_to_dataframe(collection, drop_id=True): documents = list(collection.find()) df = pd.DataFrame(documents) if drop_id and '_id' in df.columns: df = df.drop(columns=['_id']) return df def authenticate_user(username: str, password: str, users_collection=None) -> bool: """Authenticate user with cached collection to avoid reconnection. Args: username: Username to authenticate password: Password to check users_collection: Optional cached users collection. If None, will get fresh collection. """ if users_collection is None: from .config import get_collections cols = get_collections() users_collection = cols['users_collection'] user = users_collection.find_one({"username": username}) if user and check_password_hash(user.get('password', ''), password): return True return False def save_best_params(collection, filter_key, best_params): best_params = dict(best_params) best_params["filter_key"] = filter_key best_params["last_updated"] = pd.Timestamp.now().isoformat() collection.replace_one({"filter_key": filter_key}, best_params, upsert=True) def get_best_params(filter_key, collection): return collection.find_one({"filter_key": filter_key})