generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
378 lines (310 loc) · 14.7 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import os
from flask import (
Flask, flash, render_template,
redirect, request,
session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from alpha_vantage.timeseries import TimeSeries
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import date, datetime, timedelta
if os.path.exists("env.py"):
import env
from common import yesterday, getUsername, wallet, transactions
from common import transaction_lst, stringify_number
from common import profit_loss
from support import app, mongo, app_alpha, stock_aapl
# Decorator created to render the index page
@app.route("/")
def index():
return render_template("index.html")
# Decorator created to store the user's registration in MongoDB
# After registering the user is redirected to profile page
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
# check if username already exists
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
flash("Username already exists")
return redirect(url_for("register"))
# Form to be inserted into Mongo's database
# These are inputs in the register form
register = {
"first_name": request.form.get("first_name"),
"last_name": request.form.get("last_name"),
"username": request.form.get("username").lower(),
"password": generate_password_hash(request.form.get("password"))
}
# Inserts "register"
mongo.db.users.insert_one(register)
# Variable created to pass in render_template to
# display on profile page
session["user"] = request.form.get("username").lower()
# Variable created to pass in render_template
# to display on profile page
first_name = mongo.db.users.find_one(
{"username": session["user"]})["first_name"]
return render_template(
"profile.html",
username=session["user"],
first_name=first_name,
transactions=transactions,
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(
profit_loss(session)))
return render_template("register.html")
# Decorator created to authenticate the login submitted byt the user
# Once verified the user is redirected to profile page otherwise a
# flash message will be displayed
@app.route("/login", methods={"GET", "POST"})
def login():
if request.method == "POST":
# check if username is in database
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# confirm password
if check_password_hash(
existing_user["password"],
request.form.get("password")):
session["user"] = request.form.get("username").lower()
first_name = mongo.db.users.find_one(
{"username": session["user"]})["first_name"]
return render_template(
"profile.html",
username=session["user"],
first_name=first_name,
transactions=transactions(),
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
else:
# if the password doesn't match
flash("Incorrect Username and/or Password")
return redirect("login")
else:
# username doesn't match
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
return render_template("login.html")
# Decorator to render the profile page and display all the info included
@app.route("/profile/<username>", methods=["GET", "POST"])
def profile(username):
first_name = mongo.db.users.find_one(
{"username": session["user"]})["first_name"]
if session["user"]:
return render_template(
"profile.html", username=getUsername(session),
first_name=first_name,
transactions=transactions,
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
return render_template("login")
# Decorator used to render the stock page
@app.route("/stocks")
def stocks():
return render_template("stocks.html",
username=getUsername(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
# Decorator used to render the page where the user can purchase stocks
@app.route("/purchase_stocks")
def purchaseStocks():
return render_template("purchase-stocks.html",
username=getUsername(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
# Decorator used to process the purchase submitted by the user
# The purchase details are store in MongoDB
@app.route('/purchase', methods=["GET", "POST"])
def purchase():
if request.method == "POST":
# The purchase will only go forward if the funds available (wallet())
# is higher that the total purchase amount
if wallet() >= float(request.form.get("money_amount")):
# Data taken from the form
purchase = {
"purchase_date": yesterday,
"ticker": request.form.get('ticker'),
"stock_amount": request.form.get("stock_amount"),
"purchase_price": request.form.get("purchase_price"),
"money_amount": request.form.get("money_amount"),
"created_by": session["user"]
}
mongo.db.transactions.insert_one(purchase)
# Data inserted into another database in order to have an
# updated amount of the funds available to the user
wallet_transaction = {
"money_amount": -float(request.form.get("money_amount")),
"created_by": session["user"]
}
mongo.db.wallet_transactions.insert_one(wallet_transaction)
flash("Purchase Successful")
return redirect(url_for("stocks"))
# If the funds available are lower than the total purchase amount
# nothing will happen and a flash message will be displayed
else:
flash("Not enough funds")
return redirect(url_for("stocks"))
# Decorator to display current open positions, in other words the stock owned
@app.route('/open-positions')
def openPositions():
return render_template("open-positions.html",
username=getUsername(session),
transactions=transactions(),
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
# Decorator used to process the sell submitted by the user
# This decorator has an if statement dividing it into three section
# depending if the amount of stocks the user wants to sell is
# smaller, higher or the same has the stocks owned
@app.route("/sell/<position_id>", methods=["GET", "POST"])
def sell(position_id):
if request.method == "POST":
# If the user tries to sell less stock that the amount
# owned the databse has to be updated
if int(request.form.get(
"stock_amount_sell")) < int(request.form.get(
"stocks_owned")):
# This calculates the amount of stocks remaining to
# update Monog's database with "update"
remaining_stock = int(
request.form.get("stocks_owned")) - int(
request.form.get("stock_amount_sell"))
update = {
"purchase_date": yesterday,
"ticker": request.form.get('ticker'),
"stock_amount": str(remaining_stock),
"purchase_price": request.form.get("purchase_price_sell"),
"money_amount": request.form.get("money_amount_sell"),
"created_by": session["user"]
}
mongo.db.transactions.update(
{"_id": ObjectId(position_id)}, update)
# Data inserted into another database in order to have an
# updated amount of the funds available to the user
wallet_transaction = {
"money_amount": float(request.form.get("money_amount_sell")),
"created_by": session["user"]
}
mongo.db.wallet_transactions.insert_one(wallet_transaction)
# The sold stocks are then store in closed_positions
# in order to have history
sell = {
"selling_date": yesterday,
"ticker": request.form.get('ticker'),
"stock_amount": request.form.get("stock_amount_sell"),
"purchase_price": request.form.get("purchase_price_sell"),
"selling_price": request.form.get("selling_price"),
"created_by": session["user"]
}
mongo.db.closed_positions.insert_one(sell)
return redirect(url_for('openPositions'))
# If the user wants to sell the same amount of stocks owned data
# has to be deleted from the database
elif int(request.form.get(
"stock_amount_sell")) == int(request.form.get(
"stocks_owned")):
mongo.db.transactions.remove(
{"_id": ObjectId(position_id)})
# The sold stocks are then store in closed_positions
# in order to have history
sell = {
"selling_date": yesterday,
"ticker": request.form.get('ticker'),
"stock_amount": request.form.get("stock_amount_sell"),
"purchase_price": request.form.get("purchase_price_sell"),
"money_amount": request.form.get("money_amount_sell"),
"selling_price": request.form.get("selling_price"),
"stocks_sold": request.form.get("money_amount_sell"),
"created_by": session["user"]
}
mongo.db.closed_positions.insert_one(sell)
# Data inserted into another database in order to have an
# updated amount of the funds available to the user
wallet_transaction = {
"money_amount": float(request.form.get("money_amount_sell")),
"created_by": session["user"]
}
mongo.db.wallet_transactions.insert_one(wallet_transaction)
return redirect(url_for('openPositions'))
# If the user wants to seel more stocks that the amount owned
# nothing will happen and a flash message will be displayed
elif int(request.form.get(
"stock_amount_sell")) > int(request.form.get(
"stocks_owned")):
flash("Insufficient stock owned")
open_position = mongo.db.transactions.find_one(
{"_id": ObjectId(position_id)})
return render_template("sell-stocks.html",
username=getUsername(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
open_position=open_position,
profit_loss=stringify_number(profit_loss(session)))
# Decorator used to render a list of closed positions
# The closed positions are stored in MongoDB
@app.route("/closed_positions")
def closedPositions():
# Retrieve all data from mongoDB's "closed transactions" entries
closed_positions = mongo.db.closed_positions.find()
return render_template("closed-positions.html",
username=getUsername(session),
transactions=transactions(),
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
closed_positions=closed_positions,
profit_loss=stringify_number(profit_loss(session)))
# Decorator used to add funds to the user's avialable funds
@app.route("/add_funds", methods=["GET", "POST"])
def add_funds():
first_name = mongo.db.users.find_one(
{"username": session["user"]})["first_name"]
if request.method == "POST":
# When the user click on the button in the profile page an additional
# 10k will be added to the available funds
wallet_transaction = {
"money_amount": float(10000),
"created_by": session["user"]
}
mongo.db.wallet_transactions.insert_one(wallet_transaction)
return render_template(
"profile.html", username=getUsername(session),
first_name=first_name,
transactions=transactions,
transaction_lst=transaction_lst(session),
stock_aapl=stock_aapl,
yesterday=yesterday,
funds_available=stringify_number(wallet()),
profit_loss=stringify_number(profit_loss(session)))
# Decorator to process logout request
@app.route("/logout")
def logout():
# remove user's session cookie
flash("You have been logged out")
session.pop("user")
return redirect(url_for("login"))
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)