Skip to content

Commit e9cfbfa

Browse files
Write better version of the app
1 parent 862dfc3 commit e9cfbfa

File tree

9 files changed

+102
-11
lines changed

9 files changed

+102
-11
lines changed

py3/chapter11/app_better.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
import bank, uuid
3+
from flask import Flask, abort, flash, render_template, redirect, request, session, url_for
4+
5+
app = Flask(__name__)
6+
app.secret_key = 'saiGeij8AiS2ahleahMo5dahveixuV3J'
7+
8+
@app.route('/login', methods=['GET', 'POST'])
9+
def login():
10+
username = request.form.get('username', '')
11+
password = request.form.get('password', '')
12+
if request.method == 'POST':
13+
if (username, password) in [('brandon', 'atigdng'), ('sam', 'xyzzy')]:
14+
session['username'] = username
15+
session['csrf_token'] = uuid.uuid4().hex
16+
return redirect(url_for('index'))
17+
return render_template('login.html', username=username)
18+
19+
@app.route('/logout')
20+
def logout():
21+
session.pop('username', None)
22+
return redirect(url_for('login'))
23+
24+
@app.route('/')
25+
def index():
26+
username = session.get('username')
27+
if not username:
28+
return redirect(url_for('login'))
29+
payments = bank.get_payments_of(bank.open_database(), username)
30+
return render_template('index2.html', payments=payments, username=username,
31+
message=request.args.get('message'))
32+
33+
@app.route('/pay', methods=['GET', 'POST'])
34+
def pay():
35+
username = session.get('username')
36+
if not username:
37+
return redirect(url_for('login'))
38+
account = request.form.get('account', '').strip()
39+
dollars = request.form.get('dollars', '').strip()
40+
message = request.form.get('message', '').strip()
41+
complaint = None
42+
if request.method == 'POST':
43+
if request.form.get('csrf_token') != session['csrf_token']:
44+
abort(403)
45+
if account and dollars and dollars.isdigit() and message:
46+
db = bank.open_database()
47+
bank.add_payment(db, username, account, dollars, message)
48+
db.commit()
49+
flash('Payment successful')
50+
return redirect(url_for('index'))
51+
complaint = ('Dollars must be an integer' if not dollars.isdigit()
52+
else 'Please fill in all three fields')
53+
return render_template('pay2.html', complaint=complaint, account=account,
54+
dollars=dollars, message=message,
55+
csrf_token=session['csrf_token'])
56+
57+
if __name__ == '__main__':
58+
app.debug = True
59+
app.run()

py3/chapter11/app_insecure.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def index():
3030
return redirect(url_for('login'))
3131
payments = bank.get_payments_of(bank.open_database(), username)
3232
return get('index.html').render(payments=payments, username=username,
33-
message=request.args.get('message'))
33+
flash=request.args.get('flash'))
3434

3535
@app.route('/pay', methods=['GET', 'POST'])
3636
def pay():
@@ -46,7 +46,7 @@ def pay():
4646
db = bank.open_database()
4747
bank.add_payment(db, username, account, dollars, message)
4848
db.commit()
49-
return redirect(url_for('index', message='Payment successful'))
49+
return redirect(url_for('index', flash='Payment successful'))
5050
complaint = ('Dollars must be an integer' if not dollars.isdigit()
5151
else 'Please fill in all three fields')
5252
return get('pay.html').render(complaint=complaint, account=account,

py3/chapter11/csrf.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<form method="post" action="http://localhost:5000/pay">
88
<input type="hidden" name="account" value="sam">
99
<input type="hidden" name="dollars" value="220">
10-
<input type="hidden" name="message" value="Dispute charge-back">
10+
<input type="hidden" name="message" value="Someone won big">
1111
<button type="submit">Win Big</button>
1212
</form>
1313
</body>

py3/chapter11/csrf_auto.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<form id="secret_form" method="post" action="http://localhost:5000/pay">
88
<input type="hidden" name="account" value="sam">
99
<input type="hidden" name="dollars" value="330">
10-
<input type="hidden" name="message" value="Dispute charge-back">
10+
<input type="hidden" name="message" value="Someone else won big">
1111
<button type="submit">Win Big</button>
1212
</form>
1313
<script>

py3/chapter11/static/style.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ html {
44
body {
55
display: inline-block;
66
}
7-
.message {
7+
.flash_message {
88
position: relative;
99
padding: 1em;
1010
color: white;
1111
background: green;
1212
}
13-
.message a {
13+
.flash_message a {
1414
position: absolute;
1515
top: 0px;
1616
right: 0px;

py3/chapter11/templates/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{% extends "design.html" %}
22
{% block title %}Welcome, {{ username }}{% endblock %}
33
{% block body %}
4-
{% if message %}
5-
<div class="message">{{ message }}<a href="/">&times;</a></div>
4+
{% if flash %}
5+
<div class="flash_message">{{ flash }}<a href="/">&times;</a></div>
66
{% endif %}
77
<p>Your Payments</p>
88
<ul>

py3/chapter11/templates/index2.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "design.html" %}
2+
{% block title %}Welcome, {{ username }}{% endblock %}
3+
{% block body %}
4+
{% set messages = get_flashed_messages() %}
5+
{% if messages %}
6+
<div class="flash_message">
7+
{% for message in messages %}{{ message }}<br>{% endfor %}
8+
<a href="/">&times;</a></div>
9+
{% endif %}
10+
<p>Your Payments</p>
11+
<ul>
12+
{% for p in payments %}
13+
{% set prep = 'from' if (p.credit == username) else 'to' %}
14+
{% set acct = p.debit if (p.credit == username) else p.credit %}
15+
<li class="{{ prep }}">${{ p.dollars }} {{ prep }} <b>{{ acct }}</b>
16+
for: <i>{{ p.message }}</i></li>
17+
{% endfor %}
18+
</ul>
19+
<a href="/pay">Make payment</a> | <a href="/logout">Log out</a>
20+
{% endblock %}

py3/chapter11/templates/pay.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
{% block body %}
44
<form method="post" action="/pay">
55
{% if complaint %}<span class="complaint">{{ complaint }}</span>{% endif %}
6-
<label>To account: <input name="account" value="{{ account }}""></label>
7-
<label>Dollars: <input name="dollars" value="{{ dollars }}""></label>
8-
<label>Message: <input name="message" value="{{ message }}""></label>
6+
<label>To account: <input name="account" value="{{ account }}"></label>
7+
<label>Dollars: <input name="dollars" value="{{ dollars }}"></label>
8+
<label>Message: <input name="message" value="{{ message }}"></label>
99
<button type="submit">Send money</button> | <a href="/">Cancel</a>
1010
</form>
1111
{% endblock %}

py3/chapter11/templates/pay2.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "design.html" %}
2+
{% block title %}Make a Payment{% endblock %}
3+
{% block body %}
4+
<form method="post" action="/pay">
5+
{% if complaint %}<span class="complaint">{{ complaint }}</span>{% endif %}
6+
<label>To account: <input name="account" value="{{ account }}"></label>
7+
<label>Dollars: <input name="dollars" value="{{ dollars }}"></label>
8+
<label>Message: <input name="message" value="{{ message }}"></label>
9+
<input name="csrf_token" type="hidden" value="{{ csrf_token }}">
10+
<button type="submit">Send money</button> | <a href="/">Cancel</a>
11+
</form>
12+
{% endblock %}

0 commit comments

Comments
 (0)