-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMain.py
More file actions
252 lines (193 loc) · 8.14 KB
/
Copy pathMain.py
File metadata and controls
252 lines (193 loc) · 8.14 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
from flask import Flask, render_template, request, redirect, session, \
url_for
from flask_sqlalchemy import SQLAlchemy
from argon2 import PasswordHasher
import json
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
app.config['SECRET_KEY'] = 'the random string'
db = SQLAlchemy(app)
ph = PasswordHasher()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
password = db.Column(db.String(100))
cash_in_hand = db.Column(db.Integer, default=500)
stock = db.relationship('Stock', backref='owner')
class Stock(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
qty = db.Column(db.Integer)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
price = db.Column(db.Integer)
class Transcation(db.Model):
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(50))
name = db.Column(db.String(50))
qty = db.Column(db.Integer)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def getQuotePrice(symbol):
base_url = 'https://financialmodelingprep.com/api/v3/stock/real-time-price/'
content = urllib.urlopen(base_url + symbol).read()
print ('content is ', content)
json_content = json.loads(content)
print ('m is ', json_content['symbol'], ' price',
json_content['price'])
if content:
return json_content['price']
else:
return render_template('404.html', display_content='Invalid symbol. No quote available')
@app.route('/', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
try:
email = request.form['email']
password = request.form['password']
data = User.query.filter_by(email=email).first()
if (data is not None) and ph.verify(data.password, password):
session['user'] = data.id
print (session['user'])
return redirect(url_for('home'))
return render_template('incorrect_login.html')
except:
return render_template('incorrect_login.html')
@app.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
hashedPassword = ph.hash(request.form['password'])
new_user = User(email=request.form['email'],
password=hashedPassword)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session.pop('email', None)
return redirect(url_for('login'))
#############################################################################
@app.route('/home')
def home():
# s = Stock.query.filter_by(owner_id =user_id).first()
user_id = session['user']
u = User.query.get(user_id)
stock = Stock.query.all()
return render_template('home.html', stock=stock, user=user_id, cash=u.cash_in_hand)
@app.route('/show')
def show():
show_user = User.query.all()
return render_template('show.html', show_user=show_user)
@app.route('/stock')
def stock():
stock = Stock.query.all()
return render_template('show.html', stock=stock)
#############################################################################
@app.route('/quote', methods=['GET', 'POST'])
def quote():
if request.method == 'POST':
if not request.form.get('quote'):
return render_template('404.html',
display_content='No quote provided')
symbol = request.form['quote']
try:
quote = getQuotePrice(symbol)
return render_template('quote.html', quote=quote)
except:
return render_template('404.html', display_content='Invalid symbol. No quote available' )
else:
# GET method
return render_template('quote.html')
#############################################################################
@app.route('/buy', methods=['GET', 'POST'])
def buy():
if request.method == 'POST':
symbol = request.form['symbol']
shares = request.form['shares']
if not request.form.get('symbol'):
return render_template('404.html', display_content='No symbol provided')
try:
price = getQuotePrice(symbol)
total_cash_spend = price * int(shares)
user_id = session['user']
u = User.query.get(user_id)
final_cash_in_hand = u.cash_in_hand - total_cash_spend
print ('final= ', final_cash_in_hand)
if final_cash_in_hand > 0:
u.cash_in_hand = final_cash_in_hand
s = Stock.query.filter_by(owner_id=user_id, name=symbol).first()
if s:
print ('final= ', final_cash_in_hand)
u.cash_in_hand = final_cash_in_hand
final_qty = s.qty + int(shares)
print ('final qty', final_qty)
s.qty = final_qty
db.session.commit()
else:
new_stock = Stock(name=symbol, qty=shares,
owner_id=u.id, price=price)
print ('new stock', new_stock)
db.session.add(new_stock)
db.session.commit()
print ('commited')
new_transcation = Transcation(type='Bought',name=symbol, qty=shares, owner_id=u.id)
db.session.add(new_transcation)
db.session.commit()
else:
return render_template('404.html',display_content='Insufficient balance in the amount' )
return redirect(url_for('home'))
except:
return render_template('404.html', display_content='Invalid symbol. No quote available')
else:
# GET method
return render_template('buy.html')
#############################################################################
@app.route('/sell', methods=['GET', 'POST'])
def sell():
if request.method == 'POST':
symbol = request.form['symbol']
shares = request.form['shares']
if not request.form.get('symbol'):
return render_template('404.html',display_content='No symbol provided')
try:
price = getQuotePrice(symbol)
total_cash_spend = price * int(shares)
user_id = session['user']
u = User.query.get(user_id)
s = Stock.query.filter_by(owner_id=user_id, name=symbol).first()
print (s.name, 'and symbol - ', symbol)
if s.name == symbol and s.qty > 0:
final_cash_in_hand = u.cash_in_hand + total_cash_spend
print ('final= ', final_cash_in_hand)
u.cash_in_hand = final_cash_in_hand
final_qty = s.qty - int(shares)
print ('final qty', final_qty)
s.qty = final_qty
new_transcation = Transcation(type='Sold', name=symbol, qty=shares, owner_id=u.id)
db.session.add(new_transcation)
db.session.commit()
return redirect(url_for('home'))
else:
return render_template('404.html',display_content='Insufficient shares to sell')
except:
return render_template('404.html', display_content='123Invalid symbol. No quote available')
else:
# GET method
return render_template('sell.html')
#############################################################################
@app.route('/history')
def history():
transcation = Transcation.query.all()
user_id = session['user']
return render_template('history.html', transcation=transcation, user=user_id)
#############################################################################
if __name__ == '__main__':
#Added secret key that the top app.secret_key = 'super secret key'
db.create_all()
app.run(debug=True)