|
1
|
|
|
from flask import request, redirect, url_for, send_from_directory, flash, send_file |
|
2
|
|
|
from flask_login import login_required, current_user |
|
3
|
|
|
from flask import Blueprint, render_template |
|
4
|
|
|
from .models import Schedule, Balance, Total, Running, User, Settings, Transactions, Email, Hold |
|
5
|
|
|
from app import db |
|
6
|
|
|
from datetime import datetime |
|
7
|
|
|
import os |
|
8
|
|
|
from sqlalchemy import desc, extract, asc |
|
9
|
|
|
from werkzeug.security import generate_password_hash |
|
10
|
|
|
from .cashflow import calc_schedule, calc_transactions, plot_cash |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
main = Blueprint('main', __name__) |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
@main.route('/', methods=('GET', 'POST')) |
|
17
|
|
|
@login_required |
|
18
|
|
|
def index(): |
|
19
|
|
|
# get today's date |
|
20
|
|
|
todaydate = datetime.today().strftime('%A, %B %d, %Y') |
|
21
|
|
|
|
|
22
|
|
|
# query the latest balance information |
|
23
|
|
|
balance = Balance.query.order_by(desc(Balance.date), desc(Balance.id)).first() |
|
24
|
|
|
|
|
25
|
|
|
try: |
|
26
|
|
|
if balance.amount: |
|
27
|
|
|
db.session.query(Balance).delete() |
|
28
|
|
|
balance = Balance(amount=balance.amount, date=datetime.today()) |
|
29
|
|
|
db.session.add(balance) |
|
30
|
|
|
db.session.commit() |
|
31
|
|
|
except: |
|
32
|
|
|
balance = Balance(amount='0', |
|
33
|
|
|
date=datetime.today()) |
|
34
|
|
|
db.session.add(balance) |
|
35
|
|
|
db.session.commit() |
|
36
|
|
|
|
|
37
|
|
|
# empty the tables to create fresh data from the schedule |
|
38
|
|
|
db.session.query(Total).delete() |
|
39
|
|
|
db.session.query(Running).delete() |
|
40
|
|
|
db.session.query(Transactions).delete() |
|
41
|
|
|
db.session.commit() |
|
42
|
|
|
|
|
43
|
|
|
# retrieve the number of years for the cash flow plot |
|
44
|
|
|
yearamount = request.form.get('yearamount') |
|
45
|
|
|
|
|
46
|
|
|
# calculate total events for the year amount |
|
47
|
|
|
calc_schedule(yearamount) |
|
48
|
|
|
|
|
49
|
|
|
# calculate sum of running transactions |
|
50
|
|
|
calc_transactions(balance) |
|
51
|
|
|
|
|
52
|
|
|
# plot cash flow results |
|
53
|
|
|
minbalance, graphJSON = plot_cash() |
|
54
|
|
|
|
|
55
|
|
|
return render_template('index.html', title='Index', todaydate=todaydate, balance=balance.amount, |
|
56
|
|
|
minbalance=minbalance, graphJSON=graphJSON) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
@main.route('/profile') |
|
60
|
|
|
@login_required |
|
61
|
|
|
def profile(): |
|
62
|
|
|
|
|
63
|
|
|
return render_template('profile.html') |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@main.route('/schedule') |
|
67
|
|
|
@login_required |
|
68
|
|
|
def schedule(): |
|
69
|
|
|
schedule = Schedule.query.order_by(asc(extract('day', Schedule.startdate))) |
|
70
|
|
|
|
|
71
|
|
|
return render_template('schedule_table.html', title='Schedule Table', schedule=schedule) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
@main.route('/holds') |
|
75
|
|
|
@login_required |
|
76
|
|
|
def holds(): |
|
77
|
|
|
hold = Hold.query |
|
78
|
|
|
|
|
79
|
|
|
return render_template('holds_table.html', title='Holds Table', hold=hold) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
@main.route('/create', methods=('GET', 'POST')) |
|
83
|
|
|
@login_required |
|
84
|
|
|
def create(): |
|
85
|
|
|
# create a new schedule item |
|
86
|
|
|
format = '%Y-%m-%d' |
|
87
|
|
|
if request.method == 'POST': |
|
88
|
|
|
name = request.form['name'] |
|
89
|
|
|
amount = request.form['amount'] |
|
90
|
|
|
frequency = request.form['frequency'] |
|
91
|
|
|
startdate = request.form['startdate'] |
|
92
|
|
|
type = request.form['type'] |
|
93
|
|
|
schedule = Schedule(name=name, |
|
94
|
|
|
type=type, |
|
95
|
|
|
amount=amount, |
|
96
|
|
|
frequency=frequency, |
|
97
|
|
|
startdate=datetime.strptime(startdate, format).date()) |
|
98
|
|
|
existing = Schedule.query.filter_by(name=name).first() |
|
99
|
|
|
if existing: |
|
100
|
|
|
flash("Schedule already exists") |
|
101
|
|
|
return redirect(url_for('main.schedule')) |
|
102
|
|
|
db.session.add(schedule) |
|
103
|
|
|
db.session.commit() |
|
104
|
|
|
flash("Added Successfully") |
|
105
|
|
|
|
|
106
|
|
|
return redirect(url_for('main.schedule')) |
|
107
|
|
|
|
|
108
|
|
|
return redirect(url_for('main.schedule')) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
@main.route('/update', methods=['GET', 'POST']) |
|
112
|
|
|
@login_required |
|
113
|
|
|
def update(): |
|
114
|
|
|
# update an existing schedule item |
|
115
|
|
|
format = '%Y-%m-%d' |
|
116
|
|
|
|
|
117
|
|
|
if request.method == 'POST': |
|
118
|
|
|
my_data = Schedule.query.get(request.form.get('id')) |
|
119
|
|
|
my_data.name = request.form['name'] |
|
120
|
|
|
my_data.amount = request.form['amount'] |
|
121
|
|
|
my_data.type = request.form['type'] |
|
122
|
|
|
my_data.frequency = request.form['frequency'] |
|
123
|
|
|
my_data.startdate = request.form['startdate'] |
|
124
|
|
|
startdate = request.form['startdate'] |
|
125
|
|
|
my_data.startdate = datetime.strptime(startdate, format).date() |
|
126
|
|
|
db.session.commit() |
|
127
|
|
|
flash("Updated Successfully") |
|
128
|
|
|
|
|
129
|
|
|
return redirect(url_for('main.schedule')) |
|
130
|
|
|
|
|
131
|
|
|
return redirect(url_for('main.schedule')) |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
@main.route('/addhold/<id>') |
|
135
|
|
|
@login_required |
|
136
|
|
|
def addhold(id): |
|
137
|
|
|
# add a hold item from the schedule |
|
138
|
|
|
schedule = Schedule.query.filter_by(id=id).first() |
|
139
|
|
|
hold = Hold(name=schedule.name, type=schedule.type, amount=schedule.amount) |
|
140
|
|
|
db.session.add(hold) |
|
141
|
|
|
db.session.commit() |
|
142
|
|
|
|
|
143
|
|
|
return redirect(url_for('main.schedule')) |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
@main.route('/deletehold/<id>') |
|
147
|
|
|
@login_required |
|
148
|
|
|
def holds_delete(id): |
|
149
|
|
|
# delete a hold item |
|
150
|
|
|
hold = Hold.query.filter_by(id=id).first() |
|
151
|
|
|
|
|
152
|
|
|
if hold: |
|
153
|
|
|
db.session.delete(hold) |
|
154
|
|
|
db.session.commit() |
|
155
|
|
|
flash("Deleted Successfully") |
|
156
|
|
|
|
|
157
|
|
|
return redirect(url_for('main.holds')) |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
@main.route('/clearholds') |
|
161
|
|
|
@login_required |
|
162
|
|
|
def clear_holds(): |
|
163
|
|
|
# clear holds |
|
164
|
|
|
|
|
165
|
|
|
db.session.query(Hold).delete() |
|
166
|
|
|
db.session.commit() |
|
167
|
|
|
|
|
168
|
|
|
return redirect(url_for('main.index')) |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
@main.route('/delete/<id>') |
|
172
|
|
|
@login_required |
|
173
|
|
|
def schedule_delete(id): |
|
174
|
|
|
# delete a schedule item |
|
175
|
|
|
schedule = Schedule.query.filter_by(id=id).first() |
|
176
|
|
|
|
|
177
|
|
|
if schedule: |
|
178
|
|
|
db.session.delete(schedule) |
|
179
|
|
|
db.session.commit() |
|
180
|
|
|
flash("Deleted Successfully") |
|
181
|
|
|
|
|
182
|
|
|
return redirect(url_for('main.schedule')) |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
@main.route('/favicon') |
|
186
|
|
|
def favicon(): |
|
187
|
|
|
return send_from_directory(os.path.join(main.root_path, 'static'), |
|
188
|
|
|
'favicon.ico', mimetype='image/vnd.microsoft.icon') |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
@main.route('/appleicon') |
|
192
|
|
|
def appleicon(): |
|
193
|
|
|
return send_from_directory(os.path.join(main.root_path, 'static'), |
|
194
|
|
|
'apple-touch-icon.png', mimetype='image/png') |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
@main.route('/balance', methods=('GET', 'POST')) |
|
198
|
|
|
@login_required |
|
199
|
|
|
def balance(): |
|
200
|
|
|
# manually update the balance from the balance button |
|
201
|
|
|
format = '%Y-%m-%d' |
|
202
|
|
|
if request.method == 'POST': |
|
203
|
|
|
amount = request.form['amount'] |
|
204
|
|
|
dateentry = request.form['date'] |
|
205
|
|
|
balance = Balance(amount=amount, |
|
206
|
|
|
date=datetime.strptime(dateentry, format).date()) |
|
207
|
|
|
db.session.add(balance) |
|
208
|
|
|
db.session.commit() |
|
209
|
|
|
|
|
210
|
|
|
return redirect(url_for('main.index')) |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
@main.route('/changepw', methods=('GET', 'POST')) |
|
214
|
|
|
@login_required |
|
215
|
|
|
def changepw(): |
|
216
|
|
|
# change the users password from the profile page |
|
217
|
|
|
if request.method == 'POST': |
|
218
|
|
|
curr_user = current_user.id |
|
219
|
|
|
my_user = User.query.filter_by(id=curr_user).first() |
|
220
|
|
|
password = request.form['password'] |
|
221
|
|
|
my_user.password = generate_password_hash(password, method='scrypt') |
|
222
|
|
|
db.session.commit() |
|
223
|
|
|
|
|
224
|
|
|
return redirect(url_for('main.profile')) |
|
225
|
|
|
|
|
226
|
|
|
return redirect(url_for('main.profile')) |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
@main.route('/settings', methods=('GET', 'POST')) |
|
230
|
|
|
@login_required |
|
231
|
|
|
def settings(): |
|
232
|
|
|
# set the settings options, in this case disable signups, from the profile page |
|
233
|
|
|
if request.method == 'POST': |
|
234
|
|
|
signupsettingname = Settings.query.filter_by(name='signup').first() |
|
235
|
|
|
|
|
236
|
|
|
if signupsettingname: |
|
237
|
|
|
signupvalue = request.form['signupvalue'] |
|
238
|
|
|
signupsettingname.value = eval(signupvalue) |
|
239
|
|
|
db.session.commit() |
|
240
|
|
|
|
|
241
|
|
|
return redirect(url_for('main.profile')) |
|
242
|
|
|
|
|
243
|
|
|
# store the signup option value in the database to check when the user clicks signup |
|
244
|
|
|
signupvalue = request.form['signupvalue'] |
|
245
|
|
|
signupvalue = eval(signupvalue) |
|
246
|
|
|
settings = Settings(name="signup", |
|
247
|
|
|
value=signupvalue) |
|
248
|
|
|
db.session.add(settings) |
|
249
|
|
|
db.session.commit() |
|
250
|
|
|
|
|
251
|
|
|
return redirect(url_for('main.profile')) |
|
252
|
|
|
|
|
253
|
|
|
return redirect(url_for('main.profile')) |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
@main.route('/transactions') |
|
257
|
|
|
@login_required |
|
258
|
|
|
def transactions(): |
|
259
|
|
|
total = Transactions.query |
|
260
|
|
|
|
|
261
|
|
|
return render_template('transactions_table.html', total=total) |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
@main.route('/email', methods=('GET', 'POST')) |
|
265
|
|
|
@login_required |
|
266
|
|
|
def email(): |
|
267
|
|
|
# set the users email address, password, and server for the auto email balance update |
|
268
|
|
|
if request.method == 'POST': |
|
269
|
|
|
emailsettings = Email.query.filter_by(id=1).first() |
|
270
|
|
|
|
|
271
|
|
|
if emailsettings: |
|
272
|
|
|
email = request.form['email'] |
|
273
|
|
|
password = request.form['password'] |
|
274
|
|
|
server = request.form['server'] |
|
275
|
|
|
subjectstr = request.form['subject_str'] |
|
276
|
|
|
startstr = request.form['start_str'] |
|
277
|
|
|
endstr = request.form['end_str'] |
|
278
|
|
|
emailsettings.email = email |
|
279
|
|
|
emailsettings.password = password |
|
280
|
|
|
emailsettings.server = server |
|
281
|
|
|
emailsettings.subjectstr = subjectstr |
|
282
|
|
|
emailsettings.startstr = startstr |
|
283
|
|
|
emailsettings.endstr = endstr |
|
284
|
|
|
db.session.commit() |
|
285
|
|
|
|
|
286
|
|
|
return redirect(url_for('main.profile')) |
|
287
|
|
|
|
|
288
|
|
|
email = request.form['email'] |
|
289
|
|
|
password = request.form['password'] |
|
290
|
|
|
server = request.form['server'] |
|
291
|
|
|
subjectstr = request.form['subject_str'] |
|
292
|
|
|
startstr = request.form['start_str'] |
|
293
|
|
|
endstr = request.form['end_str'] |
|
294
|
|
|
emailentry = Email(email=email, password=password, server=server, subjectstr=subjectstr, startstr=startstr, |
|
295
|
|
|
endstr=endstr) |
|
296
|
|
|
db.session.add(emailentry) |
|
297
|
|
|
db.session.commit() |
|
298
|
|
|
|
|
299
|
|
|
return redirect(url_for('main.profile')) |
|
300
|
|
|
|
|
301
|
|
|
return redirect(url_for('main.profile')) |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
@main.route('/manifest.json') |
|
305
|
|
|
def serve_manifest(): |
|
306
|
|
|
return send_file('manifest.json', mimetype='application/manifest+json') |
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
@main.route('/sw.js') |
|
310
|
|
|
def serve_sw(): |
|
311
|
|
|
return send_file('sw.js', mimetype='application/javascript') |
|
312
|
|
|
|