-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathapp.py
148 lines (114 loc) · 3.89 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
#!/usr/bin/env python3
from authentication.auth_tools import login_pipeline, update_passwords, hash_password
from config.config_loader import get_config, load_by_key
from database.db import Database
from database.reset_database import reset_database
from flask import Flask, render_template, request
from core.session import Sessions
app = Flask(__name__)
global username, products, db, sessions
config = get_config()
HOST = load_by_key(config, 'host')
PORT = load_by_key(config, 'port')
username = load_by_key(config, 'username')
if load_by_key(config, 'reset_database'):
reset_database(load_by_key(config, 'database_path'), load_by_key(
config, 'schema_path'), load_by_key(config, 'starting_data_path'))
db = Database('database/store_records.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)
@ 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/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)
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)