-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
347 lines (297 loc) · 12.8 KB
/
database.py
File metadata and controls
347 lines (297 loc) · 12.8 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import mysql.connector
from mysql.connector import Error
import streamlit as st
import hashlib
import os
from dotenv import load_dotenv
from datetime import datetime, date
import json
# Load environment variables
load_dotenv()
class DatabaseManager:
def __init__(self):
self.host = os.getenv('DB_HOST', 'localhost')
self.port = os.getenv('DB_PORT', '3306')
self.database = os.getenv('DB_NAME', 'calories_tracker')
self.user = os.getenv('DB_USER', 'placeholder_username')
self.password = os.getenv('DB_PASSWORD', 'placeholder_password')
def get_connection(self):
"""Create and return a database connection"""
try:
connection = mysql.connector.connect(
host=self.host,
port=self.port,
database=self.database,
user=self.user,
password=self.password
)
return connection
except Error as e:
st.error(f"Database connection error: {e}")
return None
def init_database(self):
"""Initialize the database with required tables"""
try:
connection = self.get_connection()
if connection is None:
return False
cursor = connection.cursor()
# Create users table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
gemini_api_key TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
daily_calorie_goal INT DEFAULT 2000,
daily_protein_goal INT DEFAULT 150,
daily_carb_goal INT DEFAULT 250,
daily_fat_goal INT DEFAULT 65
)
""")
# Create meals table
cursor.execute("""
CREATE TABLE IF NOT EXISTS meals (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
meal_date DATE NOT NULL,
meal_time TIME NOT NULL,
meal_type ENUM('breakfast', 'lunch', 'dinner', 'snack') NOT NULL,
image_name VARCHAR(255),
total_calories DECIMAL(10,2) DEFAULT 0,
total_protein DECIMAL(10,2) DEFAULT 0,
total_carbs DECIMAL(10,2) DEFAULT 0,
total_fat DECIMAL(10,2) DEFAULT 0,
total_sugar DECIMAL(10,2) DEFAULT 0,
total_fiber DECIMAL(10,2) DEFAULT 0,
ai_analysis TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)
""")
# Create meal_items table
cursor.execute("""
CREATE TABLE IF NOT EXISTS meal_items (
id INT AUTO_INCREMENT PRIMARY KEY,
meal_id INT NOT NULL,
item_name VARCHAR(255) NOT NULL,
calories DECIMAL(10,2) DEFAULT 0,
protein DECIMAL(10,2) DEFAULT 0,
carbs DECIMAL(10,2) DEFAULT 0,
fat DECIMAL(10,2) DEFAULT 0,
sugar DECIMAL(10,2) DEFAULT 0,
fiber DECIMAL(10,2) DEFAULT 0,
FOREIGN KEY (meal_id) REFERENCES meals(id) ON DELETE CASCADE
)
""")
connection.commit()
cursor.close()
connection.close()
return True
except Error as e:
st.error(f"Database initialization error: {e}")
return False
def hash_password(self, password):
"""Hash a password for storing"""
return hashlib.sha256(password.encode()).hexdigest()
def create_user(self, username, email, password, gemini_api_key):
"""Create a new user"""
try:
connection = self.get_connection()
if connection is None:
return False, "Database connection failed"
cursor = connection.cursor()
password_hash = self.hash_password(password)
cursor.execute("""
INSERT INTO users (username, email, password_hash, gemini_api_key)
VALUES (%s, %s, %s, %s)
""", (username, email, password_hash, gemini_api_key))
connection.commit()
cursor.close()
connection.close()
return True, "User created successfully"
except Error as e:
if "Duplicate entry" in str(e):
return False, "Username or email already exists"
return False, f"Database error: {e}"
def authenticate_user(self, username, password):
"""Authenticate a user and return user data"""
try:
connection = self.get_connection()
if connection is None:
return False, None, "Database connection failed"
cursor = connection.cursor()
password_hash = self.hash_password(password)
cursor.execute("""
SELECT id, username, email, gemini_api_key, daily_calorie_goal,
daily_protein_goal, daily_carb_goal, daily_fat_goal
FROM users
WHERE username = %s AND password_hash = %s
""", (username, password_hash))
user = cursor.fetchone()
cursor.close()
connection.close()
if user:
user_data = {
'id': user[0],
'username': user[1],
'email': user[2],
'gemini_api_key': user[3],
'daily_calorie_goal': user[4],
'daily_protein_goal': user[5],
'daily_carb_goal': user[6],
'daily_fat_goal': user[7]
}
return True, user_data, "Login successful"
else:
return False, None, "Invalid username or password"
except Error as e:
return False, None, f"Database error: {e}"
def save_meal_analysis(self, user_id, meal_type, ai_analysis, nutrition_data, image_name=None):
"""Save meal analysis to database"""
try:
connection = self.get_connection()
if connection is None:
return False
cursor = connection.cursor()
# Insert meal record
cursor.execute("""
INSERT INTO meals (user_id, meal_date, meal_time, meal_type, image_name,
total_calories, total_protein, total_carbs, total_fat,
total_sugar, total_fiber, ai_analysis)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", (
user_id,
date.today(),
datetime.now().time(),
meal_type,
image_name,
nutrition_data.get('total_calories', 0),
nutrition_data.get('total_protein', 0),
nutrition_data.get('total_carbs', 0),
nutrition_data.get('total_fat', 0),
nutrition_data.get('total_sugar', 0),
nutrition_data.get('total_fiber', 0),
ai_analysis
))
meal_id = cursor.lastrowid
# Insert individual meal items if provided
if 'items' in nutrition_data:
for item in nutrition_data['items']:
cursor.execute("""
INSERT INTO meal_items (meal_id, item_name, calories, protein,
carbs, fat, sugar, fiber)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (
meal_id,
item['name'],
item.get('calories', 0),
item.get('protein', 0),
item.get('carbs', 0),
item.get('fat', 0),
item.get('sugar', 0),
item.get('fiber', 0)
))
connection.commit()
cursor.close()
connection.close()
return True
except Error as e:
st.error(f"Error saving meal: {e}")
return False
def get_daily_nutrition(self, user_id, target_date=None):
"""Get daily nutrition summary for a user"""
if target_date is None:
target_date = date.today()
try:
connection = self.get_connection()
if connection is None:
return None
cursor = connection.cursor()
cursor.execute("""
SELECT
COALESCE(SUM(total_calories), 0) as total_calories,
COALESCE(SUM(total_protein), 0) as total_protein,
COALESCE(SUM(total_carbs), 0) as total_carbs,
COALESCE(SUM(total_fat), 0) as total_fat,
COALESCE(SUM(total_sugar), 0) as total_sugar,
COALESCE(SUM(total_fiber), 0) as total_fiber
FROM meals
WHERE user_id = %s AND meal_date = %s
""", (user_id, target_date))
result = cursor.fetchone()
cursor.close()
connection.close()
if result:
return {
'calories': float(result[0]),
'protein': float(result[1]),
'carbs': float(result[2]),
'fat': float(result[3]),
'sugar': float(result[4]),
'fiber': float(result[5])
}
return None
except Error as e:
st.error(f"Error getting daily nutrition: {e}")
return None
def get_meals_by_date(self, user_id, target_date=None):
"""Get all meals for a specific date"""
if target_date is None:
target_date = date.today()
try:
connection = self.get_connection()
if connection is None:
return []
cursor = connection.cursor()
cursor.execute("""
SELECT id, meal_type, meal_time, total_calories, total_protein,
total_carbs, total_fat, ai_analysis, image_name
FROM meals
WHERE user_id = %s AND meal_date = %s
ORDER BY meal_time
""", (user_id, target_date))
meals = cursor.fetchall()
cursor.close()
connection.close()
meal_list = []
for meal in meals:
meal_list.append({
'id': meal[0],
'type': meal[1],
'time': meal[2],
'calories': float(meal[3]),
'protein': float(meal[4]),
'carbs': float(meal[5]),
'fat': float(meal[6]),
'analysis': meal[7],
'image_name': meal[8]
})
return meal_list
except Error as e:
st.error(f"Error getting meals: {e}")
return []
def update_user_goals(self, user_id, calorie_goal, protein_goal, carb_goal, fat_goal):
"""Update user's daily nutrition goals"""
try:
connection = self.get_connection()
if connection is None:
return False
cursor = connection.cursor()
cursor.execute("""
UPDATE users
SET daily_calorie_goal = %s, daily_protein_goal = %s,
daily_carb_goal = %s, daily_fat_goal = %s
WHERE id = %s
""", (calorie_goal, protein_goal, carb_goal, fat_goal, user_id))
connection.commit()
cursor.close()
connection.close()
return True
except Error as e:
st.error(f"Error updating goals: {e}")
return False
# Global database manager instance
db_manager = DatabaseManager()