-
Notifications
You must be signed in to change notification settings - Fork 0
/
budg_page.py
314 lines (238 loc) · 9.98 KB
/
budg_page.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
'''Budget Page'''
#webpage
#uses flask to create working page
from budg_functions import delAccount, delCashFlow
from budg_tables import Account, CashFlow, Expense, Actual, Transfer, create_a_thing
import forms
from appHolder import db, app
import datetime
import re
from flask import Flask, render_template,redirect,url_for, flash, request, session
@app.route('/')
def welcome():
#standard welcome, you're logged in or you're not
results=Account.query
return render_template("budg_welcome.html", results=results,tDate=datetime.date.today())
@app.route('/deleteAccount/<title>')
def deleteAccount(title):
#standard welcome, you're logged in or you're not
db.session.delete(Account.query.filter_by(title=nTitle).first())
db.session.commit()
return redirect(url_for('welcome'))
@app.route('/deleteExpense/<id><accID>')
def deleteExpense(id,accID):
#deletes the selected cashflow
db.session.delete(Expense.query.filter_by(id=id).first())
db.session.commit()
#print Account.query.filter_by(id=accID).first()
return redirect(url_for('displayAccount',acData=Account.query.filter_by(id=accID).all()[0].id))
@app.route('/edExpense/<id>',methods=['GET','POST'])
def edExpense(id):
'''
only called with a url link
'''
expData=Expense.query.filter_by(id=id).first()
#make form and assign default values
form=forms.addExpenseForm(title=expData.title,entVal=expData.value,eDate=expData.date,account=expData.account_id)
form.account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
expData.title=form.title.data
expData.value=form.entVal.data
expData.entDate=form.eDate.data
expData.account_id=form.account.data
db.session.add(expData)
db.session.commit()
flash("Expense %s Edit Success!"%expData.title)
return redirect(url_for('welcome'))
return render_template('budg_Expense.html',expData=expData, form=form,expAdd="edit")
@app.route('/adExpense',methods=['GET','POST'])
def adExpense():
'''add a single expense to an account'''
form=forms.addExpenseForm() #set up theform
form.account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
#if the form data is validated
create_a_thing(Expense,[form.account.data,form.title.data,form.entVal.data,form.eDate.data])
return redirect(url_for('welcome'))
#send in the accounts to populate the dropdown menu
return render_template('budg_Expense.html',form=form,edAdd="add")
@app.route('/edAccount/<id>',methods=['GET','POST'])
def edAccount(id):
'''this should use add account template with filled in values'''
accData=Account.query.filter_by(id=id).first()
#make form and assign default values
form=forms.addAccountForm(title=accData.title.lower(),entVal=accData.entVal,entDate=accData.entDate,
entLow=accData.lowVal)
form.title.default=accData.title.lower()
form.title.validators=[forms.Required(),forms.unique_title_edit(Account)]
if form.validate_on_submit():
accData.title=form.title.data
accData.entVal=form.entVal.data
accData.entDate=form.entDate.data
accData.lowVal=form.entLow.data
db.session.add(accData)
db.session.commit()
flash("Account Edit Success!")
return redirect(url_for('welcome'))
return render_template('budg_Account.html',accData=accData, form=form, edAdd='edit')
@app.route('/adAccount',methods=['GET','POST'])
def adAccount():
'''adds an account'''
form=forms.addAccountForm()
#if request.method=='POST':
#the form data has been posted
if form.validate_on_submit():
create_a_thing(Account,[form.title.data.lower(),form.entVal.data,form.entDate.data,form.entLow.data])
return redirect(url_for('welcome'))
return render_template('budg_Account.html',form=form, edAdd='add')
@app.route('/adTransfer',methods=['GET','POST'])
def adTransfer():
'''add a transfer to two accounts'''
form=forms.transferForm()
form.f_account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
form.t_account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
create_a_thing(Transfer,[form.title.data,form.value.data,
form.f_account.data,form.t_account.data,form.date.data])
return redirect(url_for('welcome'))
return render_template('budg_Transfer.html',form=form,edAdd="add")
@app.route('/deleteTransfer/<id><accID>')
def deleteTransfer(id,accID):
#deletes the selected transfer
db.session.delete(Transfer.query.filter_by(id=id).first())
db.session.commit()
return redirect(url_for('displayAccount',acData=Account.query.filter_by(id=accID).all()[0].id))
@app.route('/edTransfer/<id>',methods=['GET','POST'])
def edTransfer(id):
'''this should use add account template with filled in values'''
#pick up the transferwe're editing
tfData=Transfer.query.filter_by(id=id).first()
form=forms.transferForm(title=tfData.title, value=tfData.value, f_account=tfData.f_account.id,
t_account=tfData.t_account.id, date=tfData.date)
#assign the drop downs
form.f_account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
form.t_account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
#assign the values
tfData.title=form.title.data
tfData.value=form.value.data
tfData.t_account=Account.query.filter_by(id=form.t_account.data).first()
tfData.f_account=Account.query.filter_by(id=form.f_account.data).first()
tfData.date=form.date.data
db.session.add(tfData)
db.session.commit()
flash("Transfer edit complete")
return redirect(url_for('welcome'))
return render_template('budg_Transfer.html',form=form,edAdd="edit", tfData=tfData)
@app.route('/adCashFlow',methods=['GET','POST'])
def adCashFlow():
'''add a cashflow to an account'''
form=forms.addCashFlowForm()
form.account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
create_a_thing(CashFlow,[form.account.data,form.title.data,form.entVal.data,form.sDate.data,\
form.rType.data,form.rRate.data,form.eDate.data,form.est.data])
return redirect(url_for('welcome'))
return render_template('budg_CashFlow.html',form=form,edAdd="add")
@app.route('/deleteCashFlow/<id><accID>')
def deleteCashFlow(id,accID):
#deletes the selected cashflow
db.session.delete(CashFlow.query.filter_by(id=id).first())
db.session.commit()
#print Account.query.filter_by(id=accID).first()
return redirect(url_for('displayAccount',acData=Account.query.filter_by(id=accID).all()[0].id))
@app.route('/edCashFlow/<id>',methods=['GET','POST'])
def edCashFlow(id):
'''
only called with a url link
'''
cfData=CashFlow.query.filter_by(id=id).first()
#make form and assign default values
form=forms.addCashFlowForm(title=cfData.title,entVal=cfData.value,sDate=cfData.date,
account=cfData.account_id, rType=cfData.recurType, rRate=cfData.recurRate,
eDate=cfData.recurEnd, est=cfData.estimate)
#add the account choices
form.account.choices=[(acc.id,acc.title) for acc in Account.query.order_by('title')]
if form.validate_on_submit():
# assign values and submit
cfData.title=form.title.data
cfData.value=form.entVal.data
cfData.date=form.sDate.data
cfData.account_id=form.account.data
cfData.recurType=form.rType.data
cfData.recurRate=form.rRate.data
cfData.recurEnd=form.eDate.data
cfData.estimate=form.est.data
db.session.add(cfData)
db.session.commit()
flash("CashFlow %s Edit Success!"%cfData.title)
return redirect(url_for('welcome'))
return render_template('budg_CashFlow.html',cfData=cfData, form=form,edAdd="edit")
@app.route('/adActual/<cfID>',methods=['POST'])
def adActual(cfID):
'''
an internal method to create an actual value
creates the actual and returns to the list
'''
cfData=CashFlow.query.filter_by(id=cfID).first()
#here is where we will make the new or edit the existing actual data
return redirect(url_for('cfBreakdown',id=cfData.id, accID=cfData.account_id))
@app.route('/cfBreakdown/<id><accID>',methods=['GET','POST'])
def cfBreakdown(id,accID):
'''
breaksdown a cashflow into expenses
if the cashflow is an estimate allows addition of actual values
'''
cfData=CashFlow.query.filter_by(id=id).first()
#need to be able to generate a hyperlink which opens a form to edit/add actual data
#form will send data to adActual, which will create the actual and redirect here
'''
if False==True:
return redirect(url_for('budg_account_data',acData=accID))
'''
return render_template('budg_cfBreakdown.html',cfData=cfData)
@app.route('/displayAccount/<acData>', methods=['GET','POST'])
@app.route('/displayAccount', methods=['GET','POST'])
def displayAccount(acData):
#this will display an account and show the cashflows specific to it
#eventually this will display future, past, etc options
ddList=Account.query.all()
if acData=='None':
#sent an empty
acData=ddList[0]
else:
acData=Account.query.filter_by(id=acData).first()
if request.method=='POST':
#something was posted
acData=Account.query.filter_by(id=request.form['account']).first()
cfData=acData.cashFlows
expData=acData.expenses
(tf_in,tf_out)=acData.getTransfers() #[(tfIn,tfOut)]
else:
cfData=acData.cashFlows
expData=acData.expenses
(tf_in,tf_out)=acData.getTransfers() #[(tfIn,tfOut)]
#otherwise we return with the option to select the accoutn data
return render_template('budg_account_data.html',acData=acData,
ddList=ddList,cfData=cfData,expData=expData,
tf_in=tf_in, tf_out=tf_out,tDate=datetime.date.today())
#simple python scripts made part of jinja template
@app.template_test('less10')
def less10(value):
'''takes in a value and dertmines if it is less than 10'''
return (value<10)
app.jinja_env.tests['less10']=less10
#simple python scripts made part of jinja template
@app.template_filter('url_ext')
def url_ext(*value):
'''takes in a value and dertmines if it is less than 10'''
print value
print type(value)
for thing in value:
for stuff in thing:
print stuff
return url_for(value[0][0],title=value[0][1])
app.jinja_env.filters['url_ext']=url_ext
if __name__=='__main__':
db.create_all()
app.run()