-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.py
More file actions
247 lines (216 loc) · 8.27 KB
/
Controller.py
File metadata and controls
247 lines (216 loc) · 8.27 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
import sqlite3
import tkinter as tk
from tkinter import Menu
from functools import partial
from tkinter import messagebox
# Global Variables
globalGroceryList = []
Errors = []
def ErrorWindow():
tk.messagebox.showinfo("Error", "This feature is currently in development")
def recipeCapture(self):
newRecipe = [self.recipeField.get()]
listOfList_LazyProgramming = []
if newRecipe[0] == "":
newRecipe.pop(0)
tk.messagebox.showinfo("Error", "The recipe must have a title")
else:
newRecipe.append('NULL')
for i in range(1, 16):
name = ('ingredient' + str(i))
if self.ingredientEntrys[name].get() == '':
continue
else:
newRecipe.append(self.ingredientEntrys[name].get())
listOfList_LazyProgramming.append(newRecipe)
addRecipe(listOfList_LazyProgramming)
close_win(self)
loadRecipes(self)
return
def close_win(self):
print(self.master)
self.destroy()
def read_dbinit_file(master):
recipeList = []
global Errors
try:
master.statusVar.set('Reading startup recipes')
with open('seedRecipes.txt') as file:
# iterate through each line in the file
for line in file:
# separates each line into items using the ',' as a delimiter, strips the trailing space and saves it as
# variable point
recipe = line.strip('\n').split(',')
recipeList.append(recipe)
master.statusVar.set('Starter recipe list created')
return recipeList
except FileNotFoundError:
Errors.append('SeedRecipes.txt file is missing')
except:
Errors.append('Error reading startup recipes')
def create_connection():
try:
dbname = 'Grocery_DB'
conn = sqlite3.connect(dbname)
return conn
except:
Errors.append('Error connecting to the database')
# verifies if table 'TblPoints' exists. If not, it creates it.
def create_project(conn, master):
global Errors
try:
master.statusVar.set('Verifying table initialization')
curs = conn.cursor()
x = 'description TEXT NULL'
for i in range(15):
x += f',ingredient{i + 1} TEXT NULL'
sqlCmd = f'CREATE TABLE IF NOT EXISTS Recipes(title TEXT NOT NULL PRIMARY KEY,{x})'
curs.execute(sqlCmd)
master.statusVar.set('Table verified/created')
except:
Errors.append('Error initializing table')
def lookAtDB():
global Errors
conn = create_connection()
try:
curs = conn.cursor()
curs.execute('SELECT * FROM Recipes ORDER BY description ASC')
recipeRows = curs.fetchall()
return recipeRows
except:
Errors.append('Error looking at the database')
def pullRecTitles():
try:
conn = create_connection()
curs = conn.cursor()
sqlCmd = (f"SELECT title FROM Recipes ORDER BY description DESC")
curs.execute(sqlCmd)
titles = curs.fetchall()
return titles
except:
Errors.append('Error retrieving recipe titles')
def pullRecIngredients(recipeTitle):
try:
conn = create_connection()
curs = conn.cursor()
sqlCmd = f'SELECT * FROM Recipes WHERE title="{recipeTitle}"'
curs.execute(sqlCmd)
recipe = curs.fetchall()
return recipe
except:
Errors.append(f'Error retrieving ingredients for {recipeTitle}')
def addRecipe(listOfRecipes):
global Errors
try:
conn = create_connection()
curs = conn.cursor()
# Using list comprehension, we compile recipe titles from our recipefileseed into one list to prevent
# duplication
rfTitles = [recipe[0] for recipe in listOfRecipes]
# We create a dictionary from our recipes passed to the function (setup this way in case we want to pass files
# with several recipes inside) as a dictionary (hash map) and then use the recipe title as our key. This follows
# our database design built to use recipe title as the primary key. In the future, we can build this around a
# more complex primary key, such as title + author.
recipeDict = {rfTitles[i]: listOfRecipes[i] for i in range(len(listOfRecipes))}
# Pull all recipe titles from our database
dbRecTitles = pullRecTitles()
# Using list comprehension, we compile recipe titles from our database into one list to prevent duplication
dbTitles = [title for title, in dbRecTitles]
for title in rfTitles:
if title in dbTitles:
continue
else:
recipe = recipeDict.get(title)
ingredientColumns = 'ingredient1'
numArgs = '?,?,?'
for j in range(1, len(recipe) - 2):
ingredientColumns += f',ingredient{j + 1}'
numArgs += f',?'
insert = f'INSERT INTO Recipes(title, description, {ingredientColumns})'
sqlCmd = f'{insert} VALUES({numArgs})'
curs.execute(sqlCmd, recipe)
conn.commit()
except UnboundLocalError:
Errors.append(f'recipeListFile failed to load')
except IndexError:
Errors.append(IndexError)
print(IndexError)
except sqlite3.IntegrityError:
Errors.append(f'{title} failed to process. Recipe already exists in DB')
# loads recipes from database into the recipes listbox
def loadRecipes(master):
recipeRows = lookAtDB()
dbRecipeLoad = []
global Errors
try:
master.statusVar.set('Loading recipes from database into GUI')
master.recListbox.delete(0, tk.END)
for i in recipeRows:
master.recListbox.insert(tk.END, i[0])
except:
Errors.append('Error reading recipes from database')
def pullRecord(master, conn, self):
name = [master.recListbox.get(tk.ANCHOR)]
try:
conn.text_factory = str
curs = conn.cursor()
master.statusVar.set(f'Fetching recipe from database for {name}')
sqlCom = '''SELECT * FROM Recipes WHERE title = (?)'''
curs.execute(sqlCom, name)
selectedRecipe = curs.fetchone()
addGroceries(selectedRecipe, master)
except:
Errors.append(f'Failed to fetch {name}')
def addGroceries(selectedRecipe, master):
try:
master.statusVar.set(f'Adding ingredients from {selectedRecipe[0]} recipe to the grocery list')
tempTemplist = []
glbList = []
# used to pull out information from recipe record that aren't ingredients with following if statement
notIngredients = ['NULL', selectedRecipe[0], selectedRecipe[1]]
for i in selectedRecipe:
if i in notIngredients:
continue
else:
if i is not None:
globalGroceryList.append(i)
# tempSet creates a set that removes redundant entries in our globalGroceryList to prepare formatting for our
# grocery listbox display
tempSet = set(globalGroceryList)
for i in tempSet:
tempTemplist.append(f'{i} ({globalGroceryList.count(i)})')
for i in tempTemplist:
glbList.append(i)
master.glbVar.set(glbList)
except:
Errors.append(f'Error adding ingredients from {selectedRecipe[0]} recipe to grocery list')
finally:
Errors.append(f'Ingredients for {selectedRecipe[0]} added successfully')
def reportErrors(master):
if len(Errors) < 1:
master.statusVar.set('Program loaded successfully.')
else:
ErrorStatus = []
i = 0
while i <= len(Errors):
if len(ErrorStatus) < 1:
ErrorStatus.append(Errors[0])
Errors.pop(0)
i += 1
else:
ErrorStatus.append(Errors[0])
Errors.pop(0)
i += 1
master.statusVar.set(', '.join(ErrorStatus))
def removeRecipe(master):
name = [master.recListbox.get(tk.ANCHOR)]
conn = create_connection()
try:
conn.text_factory = str
curs = conn.cursor()
master.statusVar.set(f'Deleting recipe from database for {name[0]}')
curs.execute('DELETE FROM Recipes WHERE title=?', name)
conn.commit()
loadRecipes(master)
except:
Errors.append(f'Failed to delete {name}')