forked from arvindh75/BetterSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
126 lines (118 loc) · 3.62 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
from flask import Flask, render_template, redirect, url_for, session, request
from werkzeug.security import check_password_hash, generate_password_hash
from validators import validate_login, validate_signup, validate_quickpay
from forms import LoginForm, SignupForm, QuickPayForm
from models import db, User, Dues, Transactions
from flask_sqlalchemy import SQLAlchemy
from transactors import quick_pay
from parsers import parse_dues
app = Flask(__name__)
# Configurations
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bettersplit.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'c657a0b975d04adeb488661ceeb983cf'
db.init_app(app)
@app.route('/', methods = ['GET', 'POST'])
def home():
if 'user' not in session:
return redirect('/login')
username = session['user']
form = QuickPayForm(request.form)
errors = {}
if request.method == 'POST':
errors = validate_quickpay(
form.amount.data,
form.to.data
)
if not errors:
quick_pay(
form.amount.data,
form.to.data,
username
)
profile_query = User.query.filter_by(username = username).first()
profile = {
'username' : profile_query.username,
'name' : profile_query.name,
'phone' : profile_query.phone
}
due_query = Dues.query.filter_by(username = username).first()
dues = parse_dues(due_query)
dues = {
'plus' : dues['plus'],
'minus' : dues['minus'],
'net' : dues['net']
}
return render_template(
'home.html',
form = form,
errors = errors,
profile = profile,
dues = dues
)
@app.route('/login', methods = ['GET', 'POST'])
def login():
if 'user' in session:
return redirect('/')
form = LoginForm(request.form)
errors = {}
if request.method == 'POST':
errors = validate_login(
form.username.data,
form.password.data
)
if not errors:
session['user'] = request.form['username']
return redirect('/')
return render_template(
'login.html',
form = form,
errors = errors
)
@app.route('/signup', methods = ['GET', 'POST'])
def signup():
if 'user' in session:
return redirect('/')
form = SignupForm()
errors = {}
if request.method == 'POST':
errors = validate_signup(
form.name.data,
form.username.data,
form.password.data,
form.phone.data
)
if not errors:
gpay_bool = 'gpay' in request.form
paytm_bool = 'paytm' in request.form
hashed_password = generate_password_hash(form.password.data)
new_user = User(
name = form.name.data,
username = form.username.data,
password = hashed_password,
phone = form.phone.data,
gpay = gpay_bool,
paytm = paytm_bool
)
new_due = Dues(
username = form.username.data,
plus = 0,
minus = 0,
net = 0
)
db.session.add(new_user)
db.session.add(new_due)
db.session.commit()
session['user'] = request.form['username']
return redirect('/')
return render_template(
'signup.html',
form = form,
errors = errors
)
@app.route('/logout', methods = ['POST'])
def logout():
session.clear()
return redirect('/login')
if __name__ == '__main__':
app.run(debug = True)