-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
534 lines (443 loc) · 18.7 KB
/
database.py
File metadata and controls
534 lines (443 loc) · 18.7 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import sqlite3
import json
import datetime
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
import uuid
@dataclass
class Recipe:
id: str
title: str
servings: int
ingredients: List[Dict[str, float]] # [{"name": str, "grams": float}]
steps: List[str]
nutrition_per_recipe: Dict[str, float]
nutrition_per_serving: Dict[str, float]
created_at: str
rating: Optional[int] = None
is_favorite: bool = False
tags: List[str] = None
dietary_restriction: Optional[str] = None
cuisine_type: Optional[str] = None
meal_type: Optional[str] = None
cooking_time: Optional[str] = None
difficulty_level: Optional[str] = None
class RecipeDatabase:
def __init__(self, db_path: str = "recipes.db"):
self.db_path = db_path
self.init_database()
def init_database(self):
"""Initialize the database with required tables."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create recipes table
cursor.execute('''
CREATE TABLE IF NOT EXISTS recipes (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
servings INTEGER NOT NULL,
ingredients TEXT NOT NULL, -- JSON string
steps TEXT NOT NULL, -- JSON string
nutrition_per_recipe TEXT NOT NULL, -- JSON string
nutrition_per_serving TEXT NOT NULL, -- JSON string
created_at TEXT NOT NULL,
rating INTEGER,
is_favorite BOOLEAN DEFAULT FALSE,
tags TEXT, -- JSON string
dietary_restriction TEXT,
cuisine_type TEXT,
meal_type TEXT,
cooking_time TEXT,
difficulty_level TEXT
)
''')
# Create recipe_history table for tracking generation attempts
cursor.execute('''
CREATE TABLE IF NOT EXISTS recipe_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
input_ingredients TEXT NOT NULL,
recipe_id TEXT,
success BOOLEAN NOT NULL,
error_message TEXT,
created_at TEXT NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES recipes (id)
)
''')
# Create user_preferences table
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_preferences (
id INTEGER PRIMARY KEY AUTOINCREMENT,
preference_key TEXT UNIQUE NOT NULL,
preference_value TEXT NOT NULL,
updated_at TEXT NOT NULL
)
''')
# Upgrade existing database to add new columns if they don't exist
self._upgrade_database(cursor)
conn.commit()
conn.close()
def _upgrade_database(self, cursor):
"""Add new columns to existing database if they don't exist."""
try:
cursor.execute('ALTER TABLE recipes ADD COLUMN dietary_restriction TEXT')
except sqlite3.OperationalError:
pass # Column already exists
try:
cursor.execute('ALTER TABLE recipes ADD COLUMN cuisine_type TEXT')
except sqlite3.OperationalError:
pass # Column already exists
try:
cursor.execute('ALTER TABLE recipes ADD COLUMN meal_type TEXT')
except sqlite3.OperationalError:
pass # Column already exists
try:
cursor.execute('ALTER TABLE recipes ADD COLUMN cooking_time TEXT')
except sqlite3.OperationalError:
pass # Column already exists
try:
cursor.execute('ALTER TABLE recipes ADD COLUMN difficulty_level TEXT')
except sqlite3.OperationalError:
pass # Column already exists
def save_recipe(self, title: str, servings: int, ingredients: List[Dict],
steps: List[str], nutrition_per_recipe: Dict[str, float],
nutrition_per_serving: Dict[str, float], tags: List[str] = None,
dietary_restriction: str = None, cuisine_type: str = None,
meal_type: str = None, cooking_time: str = None, difficulty_level: str = None) -> str:
"""Save a recipe to the database and return the recipe ID."""
recipe_id = str(uuid.uuid4())
created_at = datetime.datetime.now().isoformat()
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO recipes (
id, title, servings, ingredients, steps,
nutrition_per_recipe, nutrition_per_serving,
created_at, tags, dietary_restriction, cuisine_type,
meal_type, cooking_time, difficulty_level
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
recipe_id,
title,
servings,
json.dumps(ingredients),
json.dumps(steps),
json.dumps(nutrition_per_recipe),
json.dumps(nutrition_per_serving),
created_at,
json.dumps(tags or []),
dietary_restriction,
cuisine_type,
meal_type,
cooking_time,
difficulty_level
))
conn.commit()
conn.close()
return recipe_id
def get_recipe(self, recipe_id: str) -> Optional[Recipe]:
"""Retrieve a recipe by ID."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('SELECT * FROM recipes WHERE id = ?', (recipe_id,))
row = cursor.fetchone()
conn.close()
if not row:
return None
return Recipe(
id=row[0],
title=row[1],
servings=row[2],
ingredients=json.loads(row[3]),
steps=json.loads(row[4]),
nutrition_per_recipe=json.loads(row[5]),
nutrition_per_serving=json.loads(row[6]),
created_at=row[7],
rating=row[8],
is_favorite=bool(row[9]),
tags=json.loads(row[10]) if row[10] else [],
dietary_restriction=row[11] if len(row) > 11 else None,
cuisine_type=row[12] if len(row) > 12 else None,
meal_type=row[13] if len(row) > 13 else None,
cooking_time=row[14] if len(row) > 14 else None,
difficulty_level=row[15] if len(row) > 15 else None
)
def get_all_recipes(self, limit: int = 50, offset: int = 0) -> List[Recipe]:
"""Get all recipes with pagination."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM recipes
ORDER BY created_at DESC
LIMIT ? OFFSET ?
''', (limit, offset))
rows = cursor.fetchall()
conn.close()
recipes = []
for row in rows:
recipes.append(Recipe(
id=row[0],
title=row[1],
servings=row[2],
ingredients=json.loads(row[3]),
steps=json.loads(row[4]),
nutrition_per_recipe=json.loads(row[5]),
nutrition_per_serving=json.loads(row[6]),
created_at=row[7],
rating=row[8],
is_favorite=bool(row[9]),
tags=json.loads(row[10]) if row[10] else [],
dietary_restriction=row[11] if len(row) > 11 else None,
cuisine_type=row[12] if len(row) > 12 else None,
meal_type=row[13] if len(row) > 13 else None,
cooking_time=row[14] if len(row) > 14 else None,
difficulty_level=row[15] if len(row) > 15 else None
))
return recipes
def search_recipes(self, query: str, limit: int = 20) -> List[Recipe]:
"""Search recipes by title or ingredients."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM recipes
WHERE title LIKE ? OR ingredients LIKE ?
ORDER BY created_at DESC
LIMIT ?
''', (f'%{query}%', f'%{query}%', limit))
rows = cursor.fetchall()
conn.close()
recipes = []
for row in rows:
recipes.append(Recipe(
id=row[0],
title=row[1],
servings=row[2],
ingredients=json.loads(row[3]),
steps=json.loads(row[4]),
nutrition_per_recipe=json.loads(row[5]),
nutrition_per_serving=json.loads(row[6]),
created_at=row[7],
rating=row[8],
is_favorite=bool(row[9]),
tags=json.loads(row[10]) if row[10] else [],
dietary_restriction=row[11] if len(row) > 11 else None,
cuisine_type=row[12] if len(row) > 12 else None,
meal_type=row[13] if len(row) > 13 else None,
cooking_time=row[14] if len(row) > 14 else None,
difficulty_level=row[15] if len(row) > 15 else None
))
return recipes
def update_recipe_rating(self, recipe_id: str, rating: int) -> bool:
"""Update the rating for a recipe."""
if rating < 1 or rating > 5:
return False
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
UPDATE recipes SET rating = ? WHERE id = ?
''', (rating, recipe_id))
success = cursor.rowcount > 0
conn.commit()
conn.close()
return success
def toggle_favorite(self, recipe_id: str) -> bool:
"""Toggle the favorite status of a recipe."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Get current favorite status
cursor.execute('SELECT is_favorite FROM recipes WHERE id = ?', (recipe_id,))
row = cursor.fetchone()
if not row:
conn.close()
return False
new_status = not bool(row[0])
cursor.execute('''
UPDATE recipes SET is_favorite = ? WHERE id = ?
''', (new_status, recipe_id))
conn.commit()
conn.close()
return True
def get_favorites(self) -> List[Recipe]:
"""Get all favorite recipes."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM recipes
WHERE is_favorite = TRUE
ORDER BY created_at DESC
''')
rows = cursor.fetchall()
conn.close()
recipes = []
for row in rows:
recipes.append(Recipe(
id=row[0],
title=row[1],
servings=row[2],
ingredients=json.loads(row[3]),
steps=json.loads(row[4]),
nutrition_per_recipe=json.loads(row[5]),
nutrition_per_serving=json.loads(row[6]),
created_at=row[7],
rating=row[8],
is_favorite=bool(row[9]),
tags=json.loads(row[10]) if row[10] else [],
dietary_restriction=row[11] if len(row) > 11 else None,
cuisine_type=row[12] if len(row) > 12 else None,
meal_type=row[13] if len(row) > 13 else None,
cooking_time=row[14] if len(row) > 14 else None,
difficulty_level=row[15] if len(row) > 15 else None
))
return recipes
def log_recipe_generation(self, input_ingredients: str, recipe_id: str = None,
success: bool = True, error_message: str = None):
"""Log a recipe generation attempt."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO recipe_history (
input_ingredients, recipe_id, success, error_message, created_at
) VALUES (?, ?, ?, ?, ?)
''', (
input_ingredients,
recipe_id,
success,
error_message,
datetime.datetime.now().isoformat()
))
conn.commit()
conn.close()
def get_recipe_stats(self) -> Dict[str, any]:
"""Get database statistics."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Total recipes
cursor.execute('SELECT COUNT(*) FROM recipes')
total_recipes = cursor.fetchone()[0]
# Favorite recipes
cursor.execute('SELECT COUNT(*) FROM recipes WHERE is_favorite = TRUE')
favorite_recipes = cursor.fetchone()[0]
# Average rating
cursor.execute('SELECT AVG(rating) FROM recipes WHERE rating IS NOT NULL')
avg_rating = cursor.fetchone()[0] or 0
# Total generation attempts
cursor.execute('SELECT COUNT(*) FROM recipe_history')
total_attempts = cursor.fetchone()[0]
# Success rate
cursor.execute('SELECT COUNT(*) FROM recipe_history WHERE success = TRUE')
successful_attempts = cursor.fetchone()[0]
success_rate = (successful_attempts / total_attempts * 100) if total_attempts > 0 else 0
conn.close()
return {
'total_recipes': total_recipes,
'favorite_recipes': favorite_recipes,
'average_rating': round(avg_rating, 2),
'total_attempts': total_attempts,
'success_rate': round(success_rate, 2)
}
def delete_recipe(self, recipe_id: str) -> bool:
"""Delete a recipe from the database."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('DELETE FROM recipes WHERE id = ?', (recipe_id,))
success = cursor.rowcount > 0
conn.commit()
conn.close()
return success
def clear_all_recipes(self) -> bool:
"""Delete all recipes and history from the database."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
try:
# Delete all recipes
cursor.execute('DELETE FROM recipes')
recipes_deleted = cursor.rowcount
# Delete all recipe history
cursor.execute('DELETE FROM recipe_history')
history_deleted = cursor.rowcount
# Reset user preferences
cursor.execute('DELETE FROM user_preferences')
conn.commit()
conn.close()
return True
except Exception as e:
conn.rollback()
conn.close()
return False
def filter_recipes(self, dietary_restriction: str = None, cuisine_type: str = None,
meal_type: str = None, cooking_time: str = None,
difficulty_level: str = None, limit: int = 50) -> List[Recipe]:
"""Filter recipes by various criteria."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Build the WHERE clause dynamically
conditions = []
params = []
if dietary_restriction:
conditions.append('dietary_restriction = ?')
params.append(dietary_restriction)
if cuisine_type:
conditions.append('cuisine_type = ?')
params.append(cuisine_type)
if meal_type:
conditions.append('meal_type = ?')
params.append(meal_type)
if cooking_time:
conditions.append('cooking_time = ?')
params.append(cooking_time)
if difficulty_level:
conditions.append('difficulty_level = ?')
params.append(difficulty_level)
where_clause = ' AND '.join(conditions) if conditions else '1=1'
query = f'''
SELECT * FROM recipes
WHERE {where_clause}
ORDER BY created_at DESC
LIMIT ?
'''
params.append(limit)
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
recipes = []
for row in rows:
recipes.append(Recipe(
id=row[0],
title=row[1],
servings=row[2],
ingredients=json.loads(row[3]),
steps=json.loads(row[4]),
nutrition_per_recipe=json.loads(row[5]),
nutrition_per_serving=json.loads(row[6]),
created_at=row[7],
rating=row[8],
is_favorite=bool(row[9]),
tags=json.loads(row[10]) if row[10] else [],
dietary_restriction=row[11] if len(row) > 11 else None,
cuisine_type=row[12] if len(row) > 12 else None,
meal_type=row[13] if len(row) > 13 else None,
cooking_time=row[14] if len(row) > 14 else None,
difficulty_level=row[15] if len(row) > 15 else None
))
return recipes
def get_available_categories(self) -> Dict[str, List[str]]:
"""Get all available categories for filtering."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
categories = {}
# Get dietary restrictions
cursor.execute('SELECT DISTINCT dietary_restriction FROM recipes WHERE dietary_restriction IS NOT NULL')
categories['dietary_restrictions'] = [row[0] for row in cursor.fetchall()]
# Get cuisine types
cursor.execute('SELECT DISTINCT cuisine_type FROM recipes WHERE cuisine_type IS NOT NULL')
categories['cuisine_types'] = [row[0] for row in cursor.fetchall()]
# Get meal types
cursor.execute('SELECT DISTINCT meal_type FROM recipes WHERE meal_type IS NOT NULL')
categories['meal_types'] = [row[0] for row in cursor.fetchall()]
# Get cooking times
cursor.execute('SELECT DISTINCT cooking_time FROM recipes WHERE cooking_time IS NOT NULL')
categories['cooking_times'] = [row[0] for row in cursor.fetchall()]
# Get difficulty levels
cursor.execute('SELECT DISTINCT difficulty_level FROM recipes WHERE difficulty_level IS NOT NULL')
categories['difficulty_levels'] = [row[0] for row in cursor.fetchall()]
conn.close()
return categories