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")
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)))
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)))
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)))
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")
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")
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)))
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)))