-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
178 lines (140 loc) · 6.41 KB
/
app.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
import os, uuid
from flask_wtf import FlaskForm
from wtforms.validators import DataRequired, Email, Length, Regexp
from wtforms import StringField, SubmitField, EmailField, FormField, Form
from flask import Flask, g, jsonify, redirect, render_template, request, session, url_for
app = Flask(__name__)
app.secret_key = os.environ.get('FLASK_SECRET_KEY', 'pizzazz_pizza' + str(uuid.uuid4()))
orders_db = {}
pizza_menu = [
{"id": 1, "name": "Margherita", "price": 5.00, "image": "1.webp"},
{"id": 2, "name": "Veggie Delight", "price": 6.00, "image": "2.webp"},
{"id": 3, "name": "Hot Pepperoni", "price": 6.25, "image": "3.webp"},
{"id": 4, "name": "Hawaiian", "price": 6.70, "image": "4.webp"},
{"id": 5, "name": "Meatza", "price": 7.45, "image": "5.webp"},
{"id": 6, "name": "BBQ Chicken", "price": 7.50, "image": "6.webp"}
]
class CreditCardForm(Form):
card_name = StringField('Card Name', validators=[DataRequired(), Length(min=2, max=50)])
card_address = StringField('Address', validators=[DataRequired(), Length(min=10, max=100)])
card_number = StringField('Card Number', validators=[DataRequired(), Regexp(r'^(?:\d{4} ){3}\d{4}$'), Length(min=17, max=19)])
card_expiry = StringField('Exp Date', validators=[DataRequired(), Regexp(r'^(0[1-9]|1[0-2])\/\d{2}$'), Length(min=4, max=5)])
card_cvv = StringField('CVV', validators=[DataRequired(), Length(min=3, max=3)])
class CheckoutForm(FlaskForm):
name = StringField('Name', validators=[DataRequired(), Length(min=2, max=50)])
email = EmailField('Email', validators=[DataRequired(), Email()])
phone = StringField('Phone', validators=[DataRequired(), Length(min=10, max=15)])
credit_card = FormField(CreditCardForm)
submit = SubmitField('Place Order')
def base62_encode(num, alphabet="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"):
if num == 0:
return alphabet[0]
base62 = []
while num:
num, rem = divmod(num, 62)
base62.append(alphabet[rem])
return ''.join(reversed(base62))
def get_next_order_id():
uuid_int = uuid.uuid4().int
short_id = base62_encode(uuid_int)
return short_id[:8]
def generate_order_id(prefix='TASTY_'):
next_order_id = get_next_order_id()
return f"{prefix}{next_order_id}"
def calculate_order_total(order_items):
return round(sum(next((pizza['price'] for pizza in pizza_menu if pizza['id'] == item['id']), 0) * item['quantity'] for item in order_items), 2)
@app.before_request
def cart_quantity():
g.total_quantity = sum(item['quantity'] for item in session.get('cart', []))
@app.route('/')
def home():
return render_template('home.html', total_quantity=g.total_quantity)
@app.route('/menu', methods=['POST', 'GET'])
def menu():
if 'cart' not in session:
session['cart'] = []
cart_ids = [item['id'] for item in session['cart']]
if request.method == 'POST':
form_data = request.form
pizza_id = int(form_data.get('id'))
quantity_str = form_data.get('quantity', '1')
quantity = int(quantity_str) if quantity_str.isdigit() else 1
pizza = next((pizza for pizza in pizza_menu if pizza['id'] == pizza_id), None)
if pizza:
item_in_cart = next((item for item in session['cart'] if item['id'] == pizza_id), None)
if item_in_cart:
item_in_cart['quantity'] += quantity
else:
session['cart'].append({
'id': pizza_id,
'name': pizza['name'],
'price': pizza['price'],
'quantity': quantity
})
session.modified = True
return jsonify({'success': True, 'message': 'Item added to cart'})
else:
return jsonify({'success': False, 'message': 'Item not found'}), 400
return render_template('menu.html', menu=pizza_menu, cart_id=cart_ids, total_quantity=g.total_quantity)
@app.route('/cart')
def cart():
return render_template('cart.html', cart=session.get('cart', []), total_quantity=g.total_quantity)
@app.route('/get_cart_items')
def get_cart_items():
if 'cart' not in session:
return jsonify({'cart': [], 'total_cost': 0})
total_cost = calculate_order_total(session['cart'])
return jsonify({'cart': session['cart'], 'total_cost': total_cost})
@app.route('/clear_cart', methods=['POST'])
def clear_cart():
session['cart'] = []
session.modified = True
return jsonify({'success': True, 'total_cost': 0})
@app.route('/update_item', methods=['POST'])
def update_item():
data = request.get_json()
if 'cart' in session:
for item in data['items']:
item_id = int(item['item_id'])
quantity = int(item['quantity'])
for cart_item in session['cart']:
if cart_item['id'] == item_id:
cart_item['quantity'] = quantity
break
session.modified = True
return jsonify({'success': True})
else:
return jsonify({'success': False, 'message': 'Cart is empty'})
@app.route('/checkout', methods=['GET', 'POST'])
def checkout():
form = CheckoutForm()
if form.validate_on_submit():
customer_info = {
"name": form.name.data,
"email": form.email.data,
"phone": form.phone.data
}
order_details = session.get('cart', [])
total_cost = calculate_order_total(order_details)
order_id = generate_order_id()
orders_db[order_id] = {
"customer_info": customer_info,
"order_details": order_details,
"total_cost": total_cost
}
session['order_id'] = order_id
session.pop('cart', None)
return redirect(url_for('thankyou'))
else:
total_cost = calculate_order_total(session.get('cart', []))
return render_template('checkout.html', form=form, total_cost=total_cost, total_quantity=g.total_quantity)
@app.route('/thankyou')
def thankyou():
order_id = session.get('order_id')
if order_id:
order = orders_db.get(order_id)
return render_template('thankyou.html', order=order, order_id=order_id, total_quantity=g.total_quantity)
else:
return "Order not found", 404
if __name__ == '__main__':
app.run(debug=True)