File size: 3,077 Bytes
9b41ddf dbca2f4 d8f1977 f1a7278 ccd1148 dbca2f4 9b41ddf ccd1148 dbca2f4 d8f1977 ccd1148 9b41ddf d8f1977 a22d8e5 b2d30a1 d8f1977 9b41ddf b2d30a1 d8f1977 9b41ddf b2d30a1 d8f1977 eb95eba 9b41ddf d8f1977 a1202d2 dbca2f4 d8f1977 eb95eba d8f1977 ccd1148 d8f1977 d59aae9 b0dd509 d59aae9 d8f1977 50fff5a d8f1977 bc1739e fcd18cd d8f1977 fcd18cd d8f1977 5b3a14f ccd1148 d8f1977 d59aae9 dbca2f4 d8f1977 b0dd509 41524e5 d8f1977 d59aae9 d8f1977 | 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 42 43 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import os
import bcrypt
import certifi # Added to handle SSL handshake
from pymongo import MongoClient
from datetime import datetime
# ---------------- CONNECTION ----------------
# Use certifi.where() to provide the correct CA bundle for the SSL handshake
ca = certifi.where()
MONGO_URI = os.getenv("MONGO_URI")
if not MONGO_URI:
raise Exception("MONGO_URI not found. Please set it in Hugging Face Secrets.")
# Initialize the client with the TLS certificate fix
client = MongoClient(MONGO_URI, tlsCAFile=ca)
db = client["fitplan_ai"]
# Collections
users_col = db["users"]
weights_col = db["weights"]
workouts_col = db["workouts"]
# ---------------- INIT DB ----------------
def init_db():
pass
# ---------------- PASSWORD HASH ----------------
def hash_password(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
def check_password(password, hashed):
return bcrypt.checkpw(password.encode('utf-8'), hashed)
# ---------------- USER FUNCTIONS ----------------
def add_user(name, age, gender, height, weight, email, password, goal):
# Check if user already exists
if users_col.find_one({"email": email}):
return False
users_col.insert_one({
"name": name,
"age": age,
"gender": gender,
"height": height,
"weight": weight, # Fixed the space and added parameter
"email": email,
"password": hash_password(password),
"goal": goal,
"created_at": datetime.utcnow()
})
return True
def verify_user(email, password):
user = users_col.find_one({"email": email})
if user and check_password(password, user["password"]):
return user
return None
def get_user_profile(email):
user = users_col.find_one({"email": email})
if user:
return (
user.get("name"),
user.get("age"),
user.get("gender"),
user.get("height"),
user.get("weight"),
user.get("goal")
)
return None
def update_profile(name, age, gender, height,weight, goal, email):
users_col.update_one(
{"email": email},
{"$set": {
"name": name,
"age": age,
"gender": gender,
"height": height,
"weight": weight,
"goal": goal
}}
)
# ---------------- WEIGHT TRACKING ----------------
def save_weight(email, weight, date):
# Tip: Ensure 'date' is a datetime object or a string ISO format
weights_col.insert_one({
"email": email,
"weight": weight,
"date": date
})
def get_weights(email):
data = list(weights_col.find({"email": email}).sort("date", 1))
if not data:
return None
return {
"date": [d["date"] for d in data],
"weight": [d["weight"] for d in data]
}
# ---------------- WORKOUTS ----------------
def save_workout(email, goal, plan):
workouts_col.insert_one({
"email": email,
"goal": goal,
"plan": plan,
"created_at": datetime.utcnow()
}) |