Passed
Push — master ( 8aaba9...f03258 )
by William
03:40
created

app.main.signups()   A

Complexity

Conditions 3

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nop 0
dl 0
loc 26
rs 9.45
c 0
b 0
f 0
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, Skip
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 update_cash, plot_cash
11
from .auth import admin_required
12
13
14
main = Blueprint('main', __name__)
15
16
17
@main.route('/', methods=('GET', 'POST'))
18
@login_required
19
def index():
20
    # get today's date
21
    todaydate = datetime.today().strftime('%A, %B %d, %Y')
22
23
    # query the latest balance information
24
    balance = Balance.query.order_by(desc(Balance.date), desc(Balance.id)).first()
25
26
    update_cash(balance)
27
28
    # plot cash flow results
29
    minbalance, graphJSON = plot_cash()
30
31
    if current_user.admin:
32
        return render_template('index.html', title='Index', todaydate=todaydate, balance=balance.amount,
33
                           minbalance=minbalance, graphJSON=graphJSON)
34
    else:
35
        return render_template('index_guest.html', title='Index', todaydate=todaydate, balance=balance.amount,
36
                           minbalance=minbalance, graphJSON=graphJSON)
37
38
39
@main.route('/profile')
40
@login_required
41
def profile():
42
43
    if current_user.admin:
44
        return render_template('profile.html')
45
    else:
46
        return render_template('profile_guest.html')
47
48
49
@main.route('/settings')
50
@login_required
51
@admin_required
52
def settings():
53
54
    return render_template('settings.html')
55
56
57
@main.route('/schedule')
58
@login_required
59
@admin_required
60
def schedule():
61
    schedule = Schedule.query.order_by(asc(extract('day', Schedule.startdate)))
62
63
    return render_template('schedule_table.html', title='Schedule Table', schedule=schedule)
64
65
66
@main.route('/holds')
67
@login_required
68
@admin_required
69
def holds():
70
    hold = Hold.query
71
    skip = Skip.query
72
73
    return render_template('holds_table.html', title='Holds Table', hold=hold, skip=skip)
74
75
76
@main.route('/create', methods=('GET', 'POST'))
77
@login_required
78
@admin_required
79
def create():
80
    # create a new schedule item
81
    format = '%Y-%m-%d'
82
    if request.method == 'POST':
83
        name = request.form['name']
84
        amount = request.form['amount']
85
        frequency = request.form['frequency']
86
        startdate = request.form['startdate']
87
        type = request.form['type']
88
        schedule = Schedule(name=name,
89
                          type=type,
90
                          amount=amount,
91
                          frequency=frequency,
92
                          startdate=datetime.strptime(startdate, format).date())
93
        existing = Schedule.query.filter_by(name=name).first()
94
        if existing:
95
            flash("Schedule already exists")
96
            return redirect(url_for('main.schedule'))
97
        db.session.add(schedule)
98
        db.session.commit()
99
        flash("Added Successfully")
100
101
        return redirect(url_for('main.schedule'))
102
103
    return redirect(url_for('main.schedule'))
104
105
106
@main.route('/update', methods=['GET', 'POST'])
107
@login_required
108
@admin_required
109
def update():
110
    # update an existing schedule item
111
    format = '%Y-%m-%d'
112
113
    if request.method == 'POST':
114
        current = Schedule.query.filter_by(id=request.form['id']).first()
115
        existing = Schedule.query.filter_by(name=request.form['name']).first()
116
        if existing:
117
            if current.name != request.form['name']:
118
                flash("Schedule name already exists")
119
                return redirect(url_for('main.schedule'))
120
        my_data = Schedule.query.get(request.form.get('id'))
121
        my_data.name = request.form['name']
122
        my_data.amount = request.form['amount']
123
        my_data.type = request.form['type']
124
        my_data.frequency = request.form['frequency']
125
        my_data.startdate = request.form['startdate']
126
        startdate = request.form['startdate']
127
        my_data.startdate = datetime.strptime(startdate, format).date()
128
        db.session.commit()
129
        flash("Updated Successfully")
130
131
        return redirect(url_for('main.schedule'))
132
133
    return redirect(url_for('main.schedule'))
134
135
136
@main.route('/addhold/<id>')
137
@login_required
138
@admin_required
139
def addhold(id):
140
    # add a hold item from the schedule
141
    schedule = Schedule.query.filter_by(id=id).first()
142
    hold = Hold(name=schedule.name, type=schedule.type, amount=schedule.amount)
143
    db.session.add(hold)
144
    db.session.commit()
145
146
    return redirect(url_for('main.schedule'))
147
148
149
@main.route('/addskip/<id>')
150
@login_required
151
@admin_required
152
def addskip(id):
153
    # add a skip item from the schedule
154
    transaction = Transactions.query.filter_by(id=id).first()
155
    trans_type = ""
156
    if transaction.type == "Expense":
157
        trans_type = "Income"
158
    elif transaction.type == "Income":
159
        trans_type = "Expense"
160
    skip = Skip(name=transaction.name + " (SKIP)", type=trans_type, amount=transaction.amount, date=transaction.date)
161
    db.session.add(skip)
162
    db.session.commit()
163
164
    return redirect(url_for('main.transactions'))
165
166
167
@main.route('/deletehold/<id>')
168
@login_required
169
@admin_required
170
def holds_delete(id):
171
    # delete a hold item
172
    hold = Hold.query.filter_by(id=id).first()
173
174
    if hold:
175
        db.session.delete(hold)
176
        db.session.commit()
177
        flash("Deleted Successfully")
178
179
    return redirect(url_for('main.holds'))
180
181
182
@main.route('/deleteskip/<id>')
183
@login_required
184
@admin_required
185
def skips_delete(id):
186
    # delete a skip item
187
    skip = Skip.query.filter_by(id=id).first()
188
189
    if skip:
190
        db.session.delete(skip)
191
        db.session.commit()
192
        flash("Deleted Successfully")
193
194
    return redirect(url_for('main.holds'))
195
196
197
@main.route('/clearholds')
198
@login_required
199
@admin_required
200
def clear_holds():
201
    # clear holds
202
    db.session.query(Hold).delete()
203
    db.session.commit()
204
205
    return redirect(url_for('main.holds'))
206
207
208
@main.route('/clearskips')
209
@login_required
210
@admin_required
211
def clear_skips():
212
    # clear skips
213
    db.session.query(Skip).delete()
214
    db.session.commit()
215
216
    return redirect(url_for('main.index'))
217
218
219
@main.route('/delete/<id>')
220
@login_required
221
@admin_required
222
def schedule_delete(id):
223
    # delete a schedule item
224
    schedule = Schedule.query.filter_by(id=id).first()
225
226
    if schedule:
227
        db.session.delete(schedule)
228
        db.session.commit()
229
        flash("Deleted Successfully")
230
231
    return redirect(url_for('main.schedule'))
232
233
234
@main.route('/favicon')
235
def favicon():
236
    return send_from_directory(os.path.join(main.root_path, 'static'),
237
                               'favicon.ico', mimetype='image/vnd.microsoft.icon')
238
239
240
@main.route('/appleicon')
241
def appleicon():
242
    return send_from_directory(os.path.join(main.root_path, 'static'),
243
                               'apple-touch-icon.png', mimetype='image/png')
244
245
246
@main.route('/balance', methods=('GET', 'POST'))
247
@login_required
248
@admin_required
249
def balance():
250
    # manually update the balance from the balance button
251
    format = '%Y-%m-%d'
252
    if request.method == 'POST':
253
        amount = request.form['amount']
254
        dateentry = request.form['date']
255
        balance = Balance(amount=amount,
256
                          date=datetime.strptime(dateentry, format).date())
257
        db.session.add(balance)
258
        db.session.commit()
259
260
        return redirect(url_for('main.index'))
261
262
263
@main.route('/changepw', methods=('GET', 'POST'))
264
@login_required
265
def changepw():
266
    # change the users password from the profile page
267
    if request.method == 'POST':
268
        curr_user = current_user.id
269
        my_user = User.query.filter_by(id=curr_user).first()
270
        password = request.form['password']
271
        my_user.password = generate_password_hash(password, method='scrypt')
272
        db.session.commit()
273
274
        return redirect(url_for('main.profile'))
275
276
    return redirect(url_for('main.profile'))
277
278
279
@main.route('/signups', methods=('GET', 'POST'))
280
@login_required
281
@admin_required
282
def signups():
283
    # set the settings options, in this case disable signups, from the profile page
284
    if request.method == 'POST':
285
        signupsettingname = Settings.query.filter_by(name='signup').first()
286
287
        if signupsettingname:
288
            signupvalue = request.form['signupvalue']
289
            signupsettingname.value = eval(signupvalue)
290
            db.session.commit()
291
292
            return redirect(url_for('main.settings'))
293
294
        # store the signup option value in the database to check when the user clicks signup
295
        signupvalue = request.form['signupvalue']
296
        signupvalue = eval(signupvalue)
297
        settings = Settings(name="signup",
298
                          value=signupvalue)
299
        db.session.add(settings)
300
        db.session.commit()
301
302
        return redirect(url_for('main.settings'))
303
304
    return redirect(url_for('main.settings'))
305
306
307
@main.route('/transactions')
308
@login_required
309
@admin_required
310
def transactions():
311
    total = Transactions.query
312
313
    return render_template('transactions_table.html', total=total)
314
315
316
@main.route('/email', methods=('GET', 'POST'))
317
@login_required
318
@admin_required
319
def email():
320
    # set the users email address, password, and server for the auto email balance update
321
    if request.method == 'POST':
322
        emailsettings = Email.query.filter_by(id=1).first()
323
324
        if emailsettings:
325
            email = request.form['email']
326
            password = request.form['password']
327
            server = request.form['server']
328
            subjectstr = request.form['subject_str']
329
            startstr = request.form['start_str']
330
            endstr = request.form['end_str']
331
            emailsettings.email = email
332
            emailsettings.password = password
333
            emailsettings.server = server
334
            emailsettings.subjectstr = subjectstr
335
            emailsettings.startstr = startstr
336
            emailsettings.endstr = endstr
337
            db.session.commit()
338
339
            return redirect(url_for('main.settings'))
340
341
        email = request.form['email']
342
        password = request.form['password']
343
        server = request.form['server']
344
        subjectstr = request.form['subject_str']
345
        startstr = request.form['start_str']
346
        endstr = request.form['end_str']
347
        emailentry = Email(email=email, password=password, server=server, subjectstr=subjectstr, startstr=startstr,
348
                           endstr=endstr)
349
        db.session.add(emailentry)
350
        db.session.commit()
351
352
        return redirect(url_for('main.settings'))
353
354
    return redirect(url_for('main.settings'))
355
356
357
@main.route('/users_table')
358
@login_required
359
@admin_required
360
def users():
361
    users = User.query
362
363
    return render_template('users_table.html', title='Users Table', users=users)
364
365
366
@main.route('/update_user', methods=['GET', 'POST'])
367
@login_required
368
@admin_required
369
def update_user():
370
    # update an existing user
371
    if request.method == 'POST':
372
        current = User.query.filter_by(id=request.form['id']).first()
373
        existing = User.query.filter_by(email=request.form['email']).first()
374
        if existing:
375
            if current.email != request.form['email']:
376
                flash("Email already exists")
377
                return redirect(url_for('main.users'))
378
        my_data = User.query.get(request.form.get('id'))
379
        my_data.name = request.form['name']
380
        my_data.email = request.form['email']
381
        my_data.admin = eval(request.form['admin'])
382
        db.session.commit()
383
        flash("Updated Successfully")
384
385
        return redirect(url_for('main.users'))
386
387
    return redirect(url_for('main.users'))
388
389
390
@main.route('/delete_user/<id>')
391
@login_required
392
@admin_required
393
def delete_user(id):
394
    # delete a user
395
    user = User.query.filter_by(id=id).first()
396
397
    if user:
398
        db.session.delete(user)
399
        db.session.commit()
400
        flash("Deleted Successfully")
401
402
    return redirect(url_for('main.users'))
403
404
405
@main.route('/create_user', methods=('GET', 'POST'))
406
@login_required
407
@admin_required
408
def create_user():
409
    # create a new user
410
    if request.method == 'POST':
411
        name = request.form['name']
412
        email = request.form['email']
413
        admin = eval(request.form['admin'])
414
        password = generate_password_hash(request.form['password'], method='scrypt')
415
        user = User(name=name, email=email, admin=admin, password=password)
416
        existing = User.query.filter_by(email=email).first()
417
        if existing:
418
            flash("User already exists")
419
            return redirect(url_for('main.users'))
420
        db.session.add(user)
421
        db.session.commit()
422
        flash("Added Successfully")
423
424
        return redirect(url_for('main.users'))
425
426
    return redirect(url_for('main.users'))
427
428
429
@main.route('/manifest.json')
430
def serve_manifest():
431
    return send_file('manifest.json', mimetype='application/manifest+json')
432
433
434
@main.route('/sw.js')
435
def serve_sw():
436
    return send_file('sw.js', mimetype='application/javascript')
437