forked from david-gary/onlineStoreTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
221 lines (176 loc) · 6.45 KB
/
app.py
File metadata and controls
221 lines (176 loc) · 6.45 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
#!/usr/bin/env python3
from authentication.auth_tools 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/store_records.db')
products = db.get_full_inventory()
sessions = Sessions()
sessions.add_new_session(username, db)
wallets = db.get_all_wallets()
@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)
@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
"""
global username
username = request.form['username']
password = request.form['password']
if login_pipeline(username, password):
sessions.add_new_session(username, db)
db.create_wallet(username, 0)
return render_template('home.html', products=products, sessions=sessions, username=username)
else:
print(f"Incorrect username ({username}) or password ({password}).")
username = 'default'
return render_template('index.html', products=products, sessions=sessions, username=username)
@app.route('/home', methods=['GET'])
def home():
return render_template("home.html",products=products, sessions=sessions, username=username)
@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('/admin')
def admin_page():
"""
Renders the admin page when the user is at the `/admin` endpoint.
args:
- None
returns:
- None
"""
auth_level = sessions.get_session(username).auth_level
auth_level = 1 # Not good security
if auth_level == 0: #The user does not have admin privelages
return render_template('admin.html', message = "Access Denied. Not authorized to access this page.", username=username, products=[], sessions=sessions, auth_level=auth_level)
elif auth_level == 1:
return render_template("admin.html", message = "Product Management", username=username, products=products, sessions=sessions, auth_level=auth_level)
@app.route('/admin',methods=['POST'])
def admin():
"""
Sets the price of the products after price selection
"""
global products
for product in products:
price_string = request.form[str(product['id'])]
try:
new_price = float(price_string)
db.set_item_price(product['id'], new_price)
except ValueError: #No new value was passed
pass
products = db.get_full_inventory()
return render_template('admin.html', message='Your changes have been saved.')
@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/store_records.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)
if db.get_email_by_username(username) == None: # A user does not already exist with that username
db.insert_user(username, key, email, first_name, last_name)
else:
print(f"A user already exists with the username {username}")
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)
if db.get_wallet_amount_username(username)[0]['amount'] > user_session.total_cost:
user_session.submit_cart()
db.increment_wallet_by_username(username, -1 * user_session.total_cost)
return render_template('checkout.html', order=order, sessions=sessions, total_cost=user_session.total_cost)
else:
return render_template('checkout.html', order=order, sessions=sessions, total_cost=" - Purchase failed due to not enough funds.")
@app.route("/wallet", methods=['GET'])
def wallet():
"""
Renders the wallet page when the user is at the `/wallet` endpoint with a GET request.
args:
- None
returns:
- None
modifies:
- None
"""
# Add wallet logic here
wallet_amount_json = db.get_wallet_amount_username(username)
if len(wallet_amount_json) > 0:
wallet_amount = wallet_amount_json[0]['amount']
else:
wallet_amount=0
# Render wallet.html
return render_template('wallet.html',balance=wallet_amount, username=username)
@app.route("/wallet", methods=['POST'])
def wallet_increment():
db.increment_wallet_by_username(username, 10)
wallet_amount_json = db.get_wallet_amount_username(username)
wallet_amount = wallet_amount_json[0]['amount']
return render_template('wallet.html',balance=wallet_amount, username=username)
if __name__ == '__main__':
app.run(debug=True, host=HOST, port=PORT)