-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.py
99 lines (73 loc) · 3.01 KB
/
bank.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
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
from __future__ import print_function
from flask import Flask, render_template, request, redirect, url_for
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
accounts = {'jdoe': {'name': 'John Doe',
'balance': 100,
'transactions': [
{'type': 'from', 'who': "Big Mama Doe", 'amount': 100}
]
},
'mmustermann': {'name': 'Max Mustermann',
'balance': 100000000,
'transactions': [
{'type': 'from', 'who': 'Employer',
'amount': 3000},
{'type': 'to', 'who': 'Landlord',
'amount': 1200},
{'type': 'to', 'who': 'Money Launderer',
'amount': 5000},
]
}
}
def perform_transaction(sender, receiver, amount):
accounts[sender]['balance'] -= amount
accounts[receiver]['balance'] += amount
accounts[sender]['transactions'].append({'type': 'to',
'who': receiver,
'amount': amount})
accounts[receiver]['transactions'].append({'type': 'from',
'who': sender,
'amount': amount})
@app.route('/logout')
def logout():
return redirect(url_for('index'))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/account', methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
if request.form['user'] and request.form['user'] in accounts:
user = request.form['user']
return redirect(url_for("account_status", username=user))
else:
error = "Invalid Username or Password"
return render_template('login.html', error=error)
@app.route('/account/<username>')
def account_status(username):
return render_template('account.html',
user=accounts[username],
username=username)
@app.route('/account/<username>/transfer', methods=['GET', 'POST'])
def transfer_moneyz(username):
if request.method == 'GET':
return render_template('transfer.html', username=username)
else:
toUser = request.form['to']
amount = int(request.form['amount'])
if toUser not in accounts:
# TODO: error
error = "no such user '{}'".format(toUser)
return render_template('transfer.html', error=error)
perform_transaction(username, toUser, amount)
return redirect(url_for("account_status", username=username))
@app.route('/help')
def help():
return render_template('help.html')
if __name__ == '__main__':
app.run(debug=True)