Update database.py
Browse files- database.py +13 -25
database.py
CHANGED
|
@@ -1,15 +1,21 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from pymongo import MongoClient
|
| 3 |
from datetime import datetime
|
| 4 |
-
import bcrypt
|
| 5 |
|
| 6 |
# ---------------- CONNECTION ----------------
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
if not MONGO_URI:
|
| 10 |
raise Exception("MONGO_URI not found. Please set it in Hugging Face Secrets.")
|
| 11 |
|
| 12 |
-
client
|
|
|
|
|
|
|
| 13 |
db = client["fitplan_ai"]
|
| 14 |
|
| 15 |
# Collections
|
|
@@ -17,32 +23,21 @@ users_col = db["users"]
|
|
| 17 |
weights_col = db["weights"]
|
| 18 |
workouts_col = db["workouts"]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
# ---------------- INIT DB ----------------
|
| 22 |
-
def init_db():
|
| 23 |
-
# MongoDB auto-creates collections
|
| 24 |
-
pass
|
| 25 |
-
import certifi
|
| 26 |
-
|
| 27 |
-
# When defining your client:
|
| 28 |
-
client = pymongo.MongoClient("your_connection_string", tlsCAFile=certifi.where())
|
| 29 |
-
|
| 30 |
-
|
| 31 |
# ---------------- PASSWORD HASH ----------------
|
| 32 |
def hash_password(password):
|
|
|
|
| 33 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 34 |
|
| 35 |
-
|
| 36 |
def check_password(password, hashed):
|
|
|
|
| 37 |
return bcrypt.checkpw(password.encode('utf-8'), hashed)
|
| 38 |
|
| 39 |
-
|
| 40 |
# ---------------- USER FUNCTIONS ----------------
|
| 41 |
def add_user(name, age, gender, height, email, password, goal):
|
| 42 |
# Check if user already exists
|
| 43 |
if users_col.find_one({"email": email}):
|
| 44 |
return False
|
| 45 |
-
|
| 46 |
users_col.insert_one({
|
| 47 |
"name": name,
|
| 48 |
"age": age,
|
|
@@ -55,14 +50,12 @@ def add_user(name, age, gender, height, email, password, goal):
|
|
| 55 |
})
|
| 56 |
return True
|
| 57 |
|
| 58 |
-
|
| 59 |
def verify_user(email, password):
|
| 60 |
user = users_col.find_one({"email": email})
|
| 61 |
if user and check_password(password, user["password"]):
|
| 62 |
return user
|
| 63 |
return None
|
| 64 |
|
| 65 |
-
|
| 66 |
def get_user_profile(email):
|
| 67 |
user = users_col.find_one({"email": email})
|
| 68 |
if user:
|
|
@@ -75,7 +68,6 @@ def get_user_profile(email):
|
|
| 75 |
)
|
| 76 |
return None
|
| 77 |
|
| 78 |
-
|
| 79 |
def update_profile(name, age, gender, height, goal, email):
|
| 80 |
users_col.update_one(
|
| 81 |
{"email": email},
|
|
@@ -88,28 +80,24 @@ def update_profile(name, age, gender, height, goal, email):
|
|
| 88 |
}}
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
# ---------------- WEIGHT TRACKING ----------------
|
| 93 |
def save_weight(email, weight, date):
|
|
|
|
| 94 |
weights_col.insert_one({
|
| 95 |
"email": email,
|
| 96 |
"weight": weight,
|
| 97 |
"date": date
|
| 98 |
})
|
| 99 |
|
| 100 |
-
|
| 101 |
def get_weights(email):
|
| 102 |
data = list(weights_col.find({"email": email}).sort("date", 1))
|
| 103 |
-
|
| 104 |
if not data:
|
| 105 |
return None
|
| 106 |
-
|
| 107 |
return {
|
| 108 |
"date": [d["date"] for d in data],
|
| 109 |
"weight": [d["weight"] for d in data]
|
| 110 |
}
|
| 111 |
|
| 112 |
-
|
| 113 |
# ---------------- WORKOUTS ----------------
|
| 114 |
def save_workout(email, goal, plan):
|
| 115 |
workouts_col.insert_one({
|
|
|
|
| 1 |
import os
|
| 2 |
+
import bcrypt
|
| 3 |
+
import certifi # Added to handle SSL handshake
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
# ---------------- CONNECTION ----------------
|
| 8 |
+
# Use certifi.where() to provide the correct CA bundle for the SSL handshake
|
| 9 |
+
ca = certifi.where()
|
| 10 |
+
|
| 11 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
| 12 |
|
| 13 |
if not MONGO_URI:
|
| 14 |
raise Exception("MONGO_URI not found. Please set it in Hugging Face Secrets.")
|
| 15 |
|
| 16 |
+
# Initialize the client with the TLS certificate fix
|
| 17 |
+
client = MongoClient(MONGO_URI, tlsCAFile=ca)
|
| 18 |
+
|
| 19 |
db = client["fitplan_ai"]
|
| 20 |
|
| 21 |
# Collections
|
|
|
|
| 23 |
weights_col = db["weights"]
|
| 24 |
workouts_col = db["workouts"]
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# ---------------- PASSWORD HASH ----------------
|
| 27 |
def hash_password(password):
|
| 28 |
+
# Ensure password is bytes for bcrypt
|
| 29 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 30 |
|
|
|
|
| 31 |
def check_password(password, hashed):
|
| 32 |
+
# 'hashed' from MongoDB will be in bytes, which bcrypt needs
|
| 33 |
return bcrypt.checkpw(password.encode('utf-8'), hashed)
|
| 34 |
|
|
|
|
| 35 |
# ---------------- USER FUNCTIONS ----------------
|
| 36 |
def add_user(name, age, gender, height, email, password, goal):
|
| 37 |
# Check if user already exists
|
| 38 |
if users_col.find_one({"email": email}):
|
| 39 |
return False
|
| 40 |
+
|
| 41 |
users_col.insert_one({
|
| 42 |
"name": name,
|
| 43 |
"age": age,
|
|
|
|
| 50 |
})
|
| 51 |
return True
|
| 52 |
|
|
|
|
| 53 |
def verify_user(email, password):
|
| 54 |
user = users_col.find_one({"email": email})
|
| 55 |
if user and check_password(password, user["password"]):
|
| 56 |
return user
|
| 57 |
return None
|
| 58 |
|
|
|
|
| 59 |
def get_user_profile(email):
|
| 60 |
user = users_col.find_one({"email": email})
|
| 61 |
if user:
|
|
|
|
| 68 |
)
|
| 69 |
return None
|
| 70 |
|
|
|
|
| 71 |
def update_profile(name, age, gender, height, goal, email):
|
| 72 |
users_col.update_one(
|
| 73 |
{"email": email},
|
|
|
|
| 80 |
}}
|
| 81 |
)
|
| 82 |
|
|
|
|
| 83 |
# ---------------- WEIGHT TRACKING ----------------
|
| 84 |
def save_weight(email, weight, date):
|
| 85 |
+
# Tip: Ensure 'date' is a datetime object or a string ISO format
|
| 86 |
weights_col.insert_one({
|
| 87 |
"email": email,
|
| 88 |
"weight": weight,
|
| 89 |
"date": date
|
| 90 |
})
|
| 91 |
|
|
|
|
| 92 |
def get_weights(email):
|
| 93 |
data = list(weights_col.find({"email": email}).sort("date", 1))
|
|
|
|
| 94 |
if not data:
|
| 95 |
return None
|
|
|
|
| 96 |
return {
|
| 97 |
"date": [d["date"] for d in data],
|
| 98 |
"weight": [d["weight"] for d in data]
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
# ---------------- WORKOUTS ----------------
|
| 102 |
def save_workout(email, goal, plan):
|
| 103 |
workouts_col.insert_one({
|