File size: 1,401 Bytes
fa4fc8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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})