-
Notifications
You must be signed in to change notification settings - Fork 0
/
PizzaParlour.py
286 lines (253 loc) · 8.47 KB
/
PizzaParlour.py
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
from flask import Flask, jsonify, request
import socket
import random
from collections import OrderedDict
pizza_types = [
{
'method_of_prepration': 'Pepperoni is made from a mixture of ground pork and beef mixed with spices and flavorings. Salt and sodium nitrate are then added as curing agents, which prevent the growth of unwanted microorganisms. Nitrate is also added, which gives pepperoni its color.',
'name': 'Pepperoni',
'price': {'medium': 0, 'large': 0}
},
{
'method_of_prepration': 'Pizza Margherita is a typical Neapolitan pizza, made with San Marzano tomatoes, mozzarella cheese, fresh basil, salt and extra-virgin olive oil.',
'name': 'Margherita',
'price': {'medium': 0, 'large': 0}
},
{
'method_of_prepration': 'Mushrooms, Garlic and Mint',
'name': 'Vegetarian',
'price': {'medium': 0, 'large': 0}
},
{
'method_of_prepration': 'Neapolitan pizza also known as Naples-style pizza, is a style of pizza made with tomatoes and mozzarella cheese.',
'name': 'Neapolitan',
'price': {'medium': 0, 'large': 0}
}
]
drinks = [
{
'name': 'Cock',
'price': 0
},
{
'name': 'Diet Cock',
'price': 0
},
{
'name': 'Cock Zero',
'price': 0
},
{
'name': 'Pepsi',
'price': 0
},
{
'name': 'Diet Pepsi',
'price': 0
},
{
'name': 'Dr. Pepper',
'price': 0
},
{
'name': 'Water',
'price': 0
},
{
'name': 'Juice',
'price': 0
}
]
toppings = [
{
'name' : 'olives',
'price' : 0
},
{
'name' : 'tomatoes',
'price' : 0
},
{
'name' : 'mushrooms',
'price' : 0
},
{
'name' : 'jalapenos',
'price' : 0
},
{
'name' : 'chicken',
'price' : 0
},
{
'name' : 'beef',
'price' : 0
},
{
'name' : 'pepperoni',
'price' : 0
}
]
orders = [{'order_number': 1000, 'Pepperoni': 2, 'Water': 4},
{'order_number': 1001, 'Margherita': 2, 'Juice': 4}
]
deliveries = [{'order_number': 1000, 'delivery': 'Uber', 'items': {'Pepperoni': 2, 'Water': 4}}]
# reads drink prices from 'prices/drink_prices.txt'
def set_drink_prices(path = 'prices/drink_prices.txt'):
try:
file = open(path)
while(True):
line = file.readline()
if not line:
break
line_split = line.split(',')
for i in range(len(drinks)):
if drinks[i]['name'] == line_split[0]:
drinks[i]['price'] = float(line_split[1])
except:
return False
file.close()
return True
# reads pizza prices from 'prices/pizza_prices.txt'
def set_pizza_prices(path='prices/pizza_prices.txt'):
try:
file = open(path)
while(True):
line = file.readline()
if not line:
break
line_split = line.split(',')
for i in range(len(pizza_types)):
if pizza_types[i]['name'] == line_split[0]:
pizza_types[i]['price'] = {'medium': float(line_split[1]), 'large': float(line_split[2])}
except:
return False
file.close()
return True
# reads topping prices from 'prices/topping_prices.txt'
def set_topping_prices(path = 'prices/topping_prices.txt'):
try:
file = open(path)
while(True):
line = file.readline()
if not line:
break
line_split = line.split(',')
for i in range(len(toppings)):
if toppings[i]['name'] == line_split[0]:
toppings[i]['price'] = float(line_split[1])
except:
return False
file.close()
return True
set_pizza_prices()
set_drink_prices()
set_topping_prices()
app = Flask("Assignment 2")
@app.route('/pizza')
def welcome_pizza():
return 'Welcome to Pizza parlour!'
# for getting pizza types. if 'name' argument is sent, return certain pizza type
@app.route('/pizza/pizza_menu')
def get_pizza_types():
if request.args.get('name'):
for item in pizza_types:
if item['name'] == request.args.get('name'):
return jsonify(item)
return jsonify(pizza_types)
# return Pizza type for <name>
@app.route('/pizza/pizza_menu/<name>')
def get_pizza_price(name):
if request.args.get('size'):
for item in pizza_types:
if request.args.get('size') == 'medium' and item['name'] == name:
chosen_price = item['price']
return jsonify(chosen_price['medium'])
elif request.args.get('size') == 'large' and item['name'] == name:
chosen_price = item['price']
return jsonify(chosen_price['large'])
for item in pizza_types:
if item['name'] == name:
chosen_price = item['price']
return jsonify(chosen_price)
# return drinks, if 'name' specified, return certain drink
@app.route('/pizza/drink_menu')
def get_drinks():
if request.args.get('name'):
for item in drinks:
if item['name'] == request.args.get('name'):
return jsonify(item)
return jsonify(drinks)
# return drink type for <name>
@app.route('/pizza/drink_menu/<name>')
def get_drink_price(name):
for item in drinks:
if item['name'] == name:
chosen_price = item['price']
return jsonify(chosen_price)
# return toppings, if 'name' specified, return certain drink
@app.route('/pizza/topping_menu')
def get_toppings():
if request.args.get('name'):
for item in toppings:
if item['name'] == request.args.get('name'):
return jsonify(item)
return jsonify(toppings)
# return topping type for <name>
@app.route('/pizza/topping_menu/<name>')
def get_topping_price(name):
for item in toppings:
if item['name'] == name:
chosen_price = item['price']
return jsonify(chosen_price)
# add order: receives json
@app.route('/pizza/orders', methods=['POST']) #GET requests will be blocked
def add_order():
req_data = request.get_json()
order_number = random.randrange(1000,9999)
req_data['order_number'] = order_number
orders.append(req_data)
return jsonify(req_data)
# update order: receives json
@app.route('/pizza/orders/<orderNumber>', methods=['POST', 'GET'])
def update_order(orderNumber):
order_index = -1
if request.method == 'POST':
for i in range(len(orders)):
if orders[i]['order_number'] == int(orderNumber):
order_index = i
break
update_json = request.get_json()
orders[order_index] = update_json
return jsonify(update_json)
elif request.method == 'GET' and request.args.get('cancel'):
if(request.args.get('cancel') == 'true'):
for i in range(len(orders)):
if orders[i]['order_number'] == int(orderNumber):
del orders[i]
return 'Order Deleted!'
elif request.method == 'GET':
for i in range(len(orders)):
if orders[i]['order_number'] == int(orderNumber):
return jsonify(orders[i])
# get delivery with <orderNumber>
@app.route('/pizza/delivery/<orderNumber>')
def get_delivery(orderNumber):
for i in range(len(deliveries)):
if deliveries[i]['order_number'] == int(orderNumber):
return jsonify(deliveries[i])
# add delivery receives two arguments: 'order_number' and 'service'
@app.route('/pizza/deliveries')
def delivery_add():
if request.args.get('order_number') and request.args.get('service'):
delivery_json = {'order_number': int(request.args.get('order_number')), 'service': request.args.get('service')}
for i in range(len(orders)):
if orders[i]['order_number'] == delivery_json['order_number']:
chosen_order = orders[i]
for item in chosen_order:
if item != 'order_number':
delivery_json[item] = chosen_order[item]
deliveries.append(delivery_json)
return jsonify(delivery_json)
if __name__ == "__main__":
app.run()