-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
228 lines (176 loc) · 7.45 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
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
222
223
224
225
226
227
228
from flask import Flask, flash, redirect, render_template, request, session
from flask_mysqldb import MySQL
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology
# Configure application
app = Flask(__name__)
# Add Database
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'cashflow'
mysql = MySQL(app)
# Create a secreat key for WTF
app.config['SECRET_KEY'] = "secretkey"
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def home():
return render_template("home.html")
@app.route("/login", methods=["GET", "POST"])
def login():
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
cursor = mysql.connection.cursor()
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 403)
# Query database for username
username = request.form.get("username")
cursor.execute("SELECT * FROM users WHERE username = %s", (username,))
rows = cursor.fetchone()
# Ensure username exists
if rows == None:
return apology("invalid username", 403)
# Ensure username exists and password is correct
if not check_password_hash(rows[-1], request.form.get("password")):
cursor.close()
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]
# Redirect user to home page
return redirect("/dashbord")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/dashbord", methods=["GET", "POST"])
def dashbord():
if len(session) == 0:
return redirect("/login")
# Get the username
cursor = mysql.connection.cursor()
cursor.execute('SELECT username FROM users WHERE id = %s', (session["user_id"],))
result = cursor.fetchone()
# Remove () , and ' ' from result
if result:
username = result[0]
username = username.strip("'") # Remove single quotes
# Get whole flow table
cursor.execute('SELECT * FROM flow WHERE id = %s ORDER BY date DESC', (session["user_id"],))
results = cursor.fetchall()
# Get the totoal income
cursor.execute("SELECT SUM(amount) AS total_income FROM flow WHERE type = 'income' and id = %s", (session["user_id"],))
income = cursor.fetchone()
total_income = 0
if income != (None,):
total_income = float(income[0])
# Get total expences
cursor.execute("SELECT SUM(amount) AS total_expence FROM flow WHERE type = 'expence' and id = %s", (session["user_id"],))
expence = cursor.fetchone()
total_expence = 0
if expence != (None,):
total_expence = float(expence[0])
# Get Balance
balance = 0
if expence != (None,) or income != (None,):
balance = total_income - total_expence
if request.method == "POST":
selected_type = request.form.get("type")
if selected_type == "all":
cursor.execute('SELECT * FROM flow WHERE id = %s ORDER BY date DESC', (session["user_id"],))
else:
cursor.execute('SELECT * FROM flow WHERE type = %s AND id = %s ORDER BY date DESC', (selected_type, session["user_id"]))
else:
cursor.execute('SELECT * FROM flow WHERE id = %s ORDER BY date DESC', (session["user_id"],))
results = cursor.fetchall()
return render_template("dashbord.html", username=username, results=results, balance=balance)
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html")
if request.method == "POST":
cursor = mysql.connection.cursor()
name = request.form.get("name")
username = request.form.get("username")
password = request.form.get("password")
confirm_password = request.form.get("conform-password")
# Check if username or password is empty
if not username or not password:
return apology("Invalid Username or Password: Blank")
# Check if the passwords match
if password != confirm_password:
return apology("Invalid Password: Passwords do not match")
# Check if username already exists
cursor = mysql.connection.cursor()
cursor.execute('SELECT username FROM users WHERE username = %s', (username,))
existing_user = cursor.fetchone()
if existing_user:
cursor.close()
return apology("Invalid Username: Username already exists")
# Hash the password
hashed_password = generate_password_hash(password)
# Add to the database
cursor.execute('INSERT INTO users (username, name, password_hash) VALUES (%s, %s, %s)', (username, name, hashed_password))
mysql.connection.commit()
cursor.close()
return render_template("login.html")
@app.route("/income", methods=["GET", "POST"])
def income():
if len(session) == 0:
return redirect("/login")
if request.method == "GET":
return render_template("income.html")
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
cursor = mysql.connection.cursor()
method = request.form.get("income-method")
amount = request.form.get("in-amount")
type = request.form.get("income")
user_id = session["user_id"]
# Check if method or amount empty
if not method or not amount:
return apology("Blank Method or Amount")
# Add to the database
cursor.execute('INSERT INTO flow (method, amount, type, id) VALUES (%s, %s, %s, %s)', (method, amount, 'income', user_id))
mysql.connection.commit()
cursor.close()
return redirect("/dashbord")
@app.route("/expence", methods=["GET", "POST"])
def expence():
if len(session) == 0:
return redirect("/login")
if request.method == "GET":
return render_template("expence.html")
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
cursor = mysql.connection.cursor()
method = request.form.get("expence-method")
amount = request.form.get("ex-amount")
user_id = session["user_id"]
# Check if method or amount empty
if not method or not amount:
return apology("Blank Method or Amount")
# Add to the database
cursor.execute('INSERT INTO flow (method, amount, type, id) VALUES (%s, %s, %s, %s)', (method, amount, 'expence', user_id))
mysql.connection.commit()
cursor.close()
return redirect("/dashbord")