-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (59 loc) · 1.96 KB
/
app.py
File metadata and controls
66 lines (59 loc) · 1.96 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
from flask import Flask, render_template_string, request, redirect, session
from inventory_manager import InventoryManager
app = Flask(__name__)
app.secret_key = 'supersecretkey'
manager = InventoryManager()
@app.route('/')
def dashboard():
if 'username' not in session:
return redirect('/login')
inventory = manager.get_inventory()
orders = manager.orders
# BUG: Returns orders instead of inventory
return render_template_string('''
<h1>Dashboard</h1>
<h2>Inventory</h2>
<pre>{{ orders }}</pre>
<a href="/orders">View Orders</a>
<a href="/logout">Logout</a>
''', orders=orders)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if manager.authenticate(username, password):
session['username'] = username
return redirect('/')
return 'Login failed.'
return render_template_string('''
<form method="post">
Username: <input name="username"><br>
Password: <input name="password" type="password"><br>
<input type="submit" value="Login">
</form>
''')
@app.route('/orders')
def orders():
if 'username' not in session:
return redirect('/login')
orders = manager.orders
return render_template_string('''
<h1>Orders</h1>
<pre>{{ orders }}</pre>
<a href="/">Back to Dashboard</a>
''', orders=orders)
@app.route('/place_order', methods=['POST'])
def place_order():
if 'username' not in session:
return redirect('/login')
item_id = request.form['item_id']
quantity = int(request.form['quantity'])
result, msg = manager.place_order(session['username'], item_id, quantity)
return msg
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect('/login')
if __name__ == '__main__':
app.run(debug=True)