-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_maker.py
More file actions
184 lines (126 loc) · 4.86 KB
/
menu_maker.py
File metadata and controls
184 lines (126 loc) · 4.86 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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 2 14:58:23 2021
@author: Emmet (github.com/emmetdev)
Example dbconfig.ini included in git, needs to be renamed from .example to .ini
A MySQL database needs to be running on a database server such as xampp
An example database import file is included in /setup_resources
"""
import mysql.connector
import configparser
config = configparser.ConfigParser()
config.read('dbconfig.ini')
config = {
'user': config['mysqlDB']['user'],
'password': config['mysqlDB']['pass'],
'host': config['mysqlDB']['host'],
'database': config['mysqlDB']['db'],
'raise_on_warnings': bool(config['mysqlDB']['raise_on_warnings'])
}
cnx = mysql.connector.connect(**config)
print("\nWelcome to recipe generator. The available recipes are: \n")
cursor = cnx.cursor(prepared=True)
query = ("SELECT recipe_id, recipe_name FROM `recipes`")
cursor.execute(query)
i=1
for row in cursor:
print("("+str(i)+") {}".format(row[1]))
i+=1
cursor.close()
# # # # # Get user's recipe choices and sanitise the input # # # # #
while True:
try:
selection_list = input("Please enter which recipe numbers you require as a comma separated list:")
except ValueError:
print("Sorry, that input was not recognised. Please try again.")
# try again... Return to the start of the loop
continue
else:
try:
required_recipes = selection_list.split(",")
#cast each input to type int
for n in range(len(required_recipes)):
required_recipes[n] = int(required_recipes[n].strip())
print(required_recipes)
#input was successfully parsed!
break
except ValueError:
print("Sorry 2, that input was not recognised. Please try again.")
# try again... Return to the start of the loop
continue
# # # # # Retreive all the ingredients & amounts then list them # # # # #
cursor = cnx.cursor(buffered=True)
for recipe_id in required_recipes:
print ("\n**** Ingredients for recipe "+str(recipe_id)+": ****")
# Parameterized query
query = """ SELECT recipe_ingredients.ingredientsID, ingredients.ingredient_name, recipe_ingredients.quantity, recipe_ingredients.quantity_unit, ingredients.is_storecupboard
FROM
recipe_ingredients LEFT JOIN ingredients
ON
recipe_ingredients.ingredientsID = ingredients.ingredient_id
WHERE
recipe_ingredients.recipesID = %s"""
param = (recipe_id, )
cursor.execute(query, param)
i=1
for row in cursor:
print("("+str(i)+") {}".format(row[1]), row[2], row[3])
i+=1
cursor.close()
# # # # # Retreive all the steps needed for each recipe and list them # # # # #
cursor = cnx.cursor(buffered=True)
for recipe_id in required_recipes:
# Parameterized query
query = """SELECT recipe_id, recipe_name, steps FROM `recipes` WHERE recipe_id = %s"""
param = (recipe_id, )
cursor.execute(query, param)
print("\n")
i=1
for row in cursor:
print("**** Steps for recipe "+str(recipe_id)+", "+(row[1])+": ****\n"+row[2])
i+=1
cursor.close()
# # # # # Retreive all the "fresh" ingredients for all meals selected # # # # #
cursor = cnx.cursor(buffered=True)
# Parameterized query
l = required_recipes
if len(l) > 1:
t = tuple(l)
else:
t = '('+str(l[0])+')'
query = """SELECT recipe_ingredients.ingredientsID, ingredients.ingredient_name, SUM(recipe_ingredients.quantity),
recipe_ingredients.quantity_unit, ingredients.is_storecupboard
FROM recipe_ingredients
LEFT JOIN ingredients
ON recipe_ingredients.ingredientsID = ingredients.ingredient_id
WHERE recipe_ingredients.recipesID in {} AND is_storecupboard = 0
GROUP BY recipe_ingredients.ingredientsID""".format(t)
cursor.execute(query)
print("\n**** All fresh ingredients required: ****")
i=1
for row in cursor:
print("("+str(i)+") {}".format(row[1]), row[2], row[3])
i+=1
cursor.close()
# # # # # Retreive all the "store cupboard" ingredients for all meals selected # # # # #
cursor = cnx.cursor(buffered=True)
l = required_recipes
if len(l) > 1:
t = tuple(l)
else:
t = '('+str(l[0])+')'
query = """SELECT recipe_ingredients.ingredientsID, ingredients.ingredient_name, SUM(recipe_ingredients.quantity),
recipe_ingredients.quantity_unit, ingredients.is_storecupboard
FROM recipe_ingredients
LEFT JOIN ingredients
ON recipe_ingredients.ingredientsID = ingredients.ingredient_id
WHERE recipe_ingredients.recipesID in {} AND is_storecupboard = 1
GROUP BY recipe_ingredients.ingredientsID""".format(t)
cursor.execute(query)
print("\n**** All store-cupboard ingredients required: ****")
i=1
for row in cursor:
print("("+str(i)+") {}".format(row[1]), row[2], row[3])
i+=1
cursor.close()
cnx.close()