forked from ingridkvcs/cfg_p2p_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
161 lines (121 loc) · 5.23 KB
/
app.py
File metadata and controls
161 lines (121 loc) · 5.23 KB
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
from Lendr import LoginManager, login_required, current_user
from Lendr import fg_oya_score, fg_oya_rating, fg_oma_rating
from Lendr import fg_oma_score, fg_owa_score, fg_owa_rating, fg_pc_rating, \
fg_pc_score
from Lendr import create_db, create_tables, create_populate_users, create_populate_orders, \
create_app, match_orders
from Lendr import redirect, SQLAlchemy, or_, and_, delete
from Lendr import render_template, request, url_for, flash
from Lendr import User, Order, Contract, create_populate_contracts, db_session
# from Lendr import logging
# Temporary while debugging
# logging.basicConfig()
# logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
# logging.getLogger("sqlalchemy.pool").setLevel(logging.DEBUG)
if __name__ == '__main__':
create_db()
create_tables()
app = create_app()
app.app_context().push()
if __name__ == '__main__':
create_populate_users()
create_populate_orders()
create_populate_contracts()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/favicon.ico')
def favicon():
return app.send_static_file('img/favicon.ico')
@app.route('/')
def main_page():
users = db_session.query(User).count()
return render_template("index.html", user_count=users)
@app.route('/fear-and-greed')
def fear_and_greed_index_page():
return render_template("fear_greed.html", prevclose_score=fg_pc_score, prevclose_rating=fg_pc_rating,
oneweek_score=fg_owa_score, oneweek_rating=fg_owa_rating, onemonth_score=fg_oma_score,
onemonth_rating=fg_oma_rating, oneyear_score=fg_oya_score, oneyear_rating=fg_oya_rating)
@app.route('/my-account')
@login_required
def my_account_page():
contracts = db_session.query(Contract) \
.filter(or_(Contract.borrower_id == current_user.id, Contract.lender_id == current_user.id)) \
.order_by(Contract.date_created.desc()) \
.all()
for contract in contracts:
if current_user.id == contract.borrower_id:
contract.type = "borrow"
elif current_user.id == contract.lender_id:
contract.type = "lend"
else:
raise Exception("The specified user is not part of this contract.")
orders = db_session.query(Order) \
.filter(Order.user_id == current_user.id) \
.all()
return render_template("my_account.html", first_name=current_user.first_name, last_name=current_user.last_name,
contracts=contracts, orders=orders)
# ("my_account.html", first_name=current_user.first_name, last_name=current_user.last_name,
# headers=headers, orders=orders)
@app.route('/order-book')
@login_required
def order_book_page():
# Get lend orders with the lowest interest rate
lend_orders = db_session.query(Order) \
.filter(Order.order_type == 'lend') \
.order_by(Order.interest_rate.asc()) \
.limit(5) \
.all()[::-1]
# Get borrow orders with the highest interest rate
borrow_orders = db_session.query(Order) \
.filter(Order.order_type == 'borrow') \
.order_by(Order.interest_rate.desc()) \
.limit(5) \
.all()
return render_template('order_book.html', lend_orders=lend_orders, borrow_orders=borrow_orders)
@app.route('/create-order', methods=['POST'])
@login_required
def create_order():
user_id = db_session.query(current_user.id)
order_type = request.form.get('order_type')
amount = request.form.get('amount')
interest_rate = request.form.get('interest_rate')
if not amount or not amount.isnumeric() or int(amount) <= 0:
flash('Amount must be greater than 0.')
return redirect(url_for('order_book_page'))
if not interest_rate or float(interest_rate) <= 0:
flash('Interest rate must be greater than 0.')
return redirect(url_for('order_book_page'))
order = Order()
order.user_id = user_id
order.order_type = order_type
order.amount = int(amount)
order.interest_rate = float(interest_rate)
match_orders(db_session, order)
# Only add the order to the database if the order wasn't fully matched
if order.amount > 0:
db_session.add(order)
# Save all the changes to the database and rollback all the changes if there's any error.
try:
db_session.commit()
flash('Congratulations! Your order has been successfully created and is awaiting a match.')
except SQLAlchemy.exc.IntegrityError:
db_session.rollback()
flash('There was an error with your order request. Please try again.')
return redirect(url_for('order_book_page'))
@app.route('/delete-order', methods=['POST'])
@login_required
def delete_order():
id = request.form.get('id')
delete_command = delete(Order).where(and_(Order.id == id, Order.user_id == current_user.id))
result = db_session.execute(delete_command)
db_session.commit()
if result.rowcount == 0:
flash("Couldn't delete order")
return redirect(url_for('my_account_page'))
if __name__ == '__main__':
# app.run(debug=true)
app.run(debug=True)