forked from david-gary/onlineStoreTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
186 lines (146 loc) · 4.71 KB
/
app.py
File metadata and controls
186 lines (146 loc) · 4.71 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
#!/usr/bin/env python3
from authentication.authTools import login_pipeline, update_passwords, hash_password
from database.db import Database
from flask import Flask, render_template, request
from core.session import Sessions
app = Flask(__name__)
HOST, PORT = 'localhost', 8080
global username, products, db, sessions
username = 'default'
db = Database('database/storeRecords.db')
products = db.get_full_inventory()
sessions = Sessions()
sessions.add_new_session(username, db)
@app.route('/')
def index_page():
"""
Renders the index page when the user is at the `/` endpoint, passing along default flask variables.
args:
- None
returns:
- None
"""
return render_template('index.html', username=username, products=products, sessions=sessions)
from flask import Flask, render_template
app = Flask(__name__)
"""Renders The products"""
products = [
{
"id": 1,
"item_name": "Diamond Ring",
"info": "A beautiful diamond ring, perfect for engagements and weddings.",
"price": 5000.00,
"image_url": "static/images/diamond_ring.jpeg"
},
{
"id": 2,
"item_name": "Diamond Earring",
"info": "Stunning diamond earrings that will catch everyone's attention.",
"price": 8000.00,
"image_url": "static/images/diamond_earring.jpeg"
},
{
"id": 3,
"item_name": "Cuban Link Chain",
"info": "A stylish chain with tightly interlocking links, made of high-quality materials.",
"price": 12000.00,
"image_url": "static/images/cuban_chain.jpeg"
}
]
"""Define your routes"""
@app.route('/')
def index():
return render_template('index.html', products=products)
@app.route('/diamondRing')
def diamondRing():
return render_template('diamondRing.html')
@app.route('/diamondEarring')
def diamondEarring():
return render_template('diamondEarring.html')
@app.route('/cubanChain')
def cubanChain():
return render_template('cubanChain.html')
@app.route('/login')
def login_page():
"""
Renders the login page when the user is at the `/login` endpoint.
args:
- None
returns:
- None
"""
return render_template('login.html')
@app.route('/home', methods=['POST'])
def login():
"""
Renders the home page when the user is at the `/home` endpoint with a POST request.
args:
- None
returns:
- None
modifies:
- sessions: adds a new session to the sessions object
"""
username = request.form['username']
password = request.form['password']
if login_pipeline(username, password):
sessions.add_new_session(username, db)
return render_template('home.html', products=products, sessions=sessions)
else:
print(f"Incorrect username ({username}) or password ({password}).")
return render_template('index.html')
@app.route('/register')
def register_page():
"""
Renders the register page when the user is at the `/register` endpoint.
args:
- None
returns:
- None
"""
return render_template('register.html')
@app.route('/register', methods=['POST'])
def register():
"""
Renders the index page when the user is at the `/register` endpoint with a POST request.
args:
- None
returns:
- None
modifies:
- passwords.txt: adds a new username and password combination to the file
- database/storeRecords.db: adds a new user to the database
"""
username = request.form['username']
password = request.form['password']
email = request.form['email']
first_name = request.form['first_name']
last_name = request.form['last_name']
salt, key = hash_password(password)
update_passwords(username, key, salt)
db.insert_user(username, key, email, first_name, last_name)
return render_template('index.html')
@app.route('/checkout', methods=['POST'])
def checkout():
"""
Renders the checkout page when the user is at the `/checkout` endpoint with a POST request.
args:
- None
returns:
- None
modifies:
- sessions: adds items to the user's cart
"""
order = {}
user_session = sessions.get_session(username)
for item in products:
print(f"item ID: {item['id']}")
if request.form[str(item['id'])] > '0':
count = request.form[str(item['id'])]
order[item['item_name']] = count
user_session.add_new_item(
item['id'], item['item_name'], item['price'], count)
user_session.submit_cart()
return render_template('checkout.html', order=order, sessions=sessions, total_cost=user_session.total_cost)
if __name__ == '__main__':
app.run(debug=True, host=HOST, port=PORT)