-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
346 lines (295 loc) · 15.3 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
import numpy as np
import pandas as pd
import requests as r
import xlsxwriter as x
import math, datetime, json
from calculator import material, BaseConverter, GeneralConverter
from finance import get_stock_data, get_stock_info, get_current_price
from apitk import IEX_CLOUD_API_TOKEN
from flask import Flask, render_template, request, redirect, flash, url_for, abort
from flask_login import LoginManager, login_required, UserMixin, login_user, logout_user, current_user
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_mail import Mail, Message
from sqlalchemy.sql import func
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database2.db'
app.config['SECRET_KEY'] = "secret"
db = SQLAlchemy(app)
app.app_context().push()
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'wplpnspxnnnlgvwh'
mail = Mail(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique = True)
password = db.Column(db.String(20), nullable = False)
email = db.Column(db.String(20), nullable = True)
transactions=db.relationship('Dashinfo', backref="user_id", uselist=True,lazy="select")
class Dashinfo(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
ticker = db.Column(db.String(10), nullable=False, unique=False)
price = db.Column(db.Float, nullable=False, unique=False)
date = db.Column(db.DateTime, nullable=False, unique=False)
type = db.Column(db.String(10), nullable=False)
user = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
amount = db.Column(db.Integer)
total = db.Column(db.Float)
class RegisterForm(FlaskForm):
username = StringField(validators=[InputRequired(), Length(min=4, max=40)], render_kw={"placeholder":"Username"})
password = StringField(validators=[InputRequired(), Length(min=4, max=40)], render_kw={"placeholder":"Password"})
email = StringField(validators=[InputRequired(), Length(min=4, max=40)], render_kw={"placeholder":"email"})
submit = SubmitField("Register")
def validade_username(self, username):
existing_user_name = User.query.filter_by(username=username.data).first()
if existing_user_name:
raise ValidationError("User name is taken")
class LoginForm(FlaskForm):
username = StringField(validators=[InputRequired(), Length(min=4, max=40)], render_kw={"placeholder":"Username"})
password = StringField(validators=[InputRequired(), Length(min=4, max=40)], render_kw={"placeholder":"Password"})
email = StringField( render_kw={"placeholder":"email"})
submit = SubmitField("Register")
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if user.password == form.password.data:
login_user(user)
flash('Logged in successfully.')
next = request.args.get('next')
return redirect(next or url_for('/about/'))
return render_template('/sign-in.html', form=form)
@app.route("/sign-in/", methods=["GET", "POST"])
def signin():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if user.password == form.password.data:
login_user(user)
return redirect("/dashboard/")
else:
return "Bad Password"
return render_template("/sign-in.html", form=form)
def user_on_mobile() -> bool:
user_agent = request.headers.get("User-Agent")
user_agent = user_agent.lower()
mobile = ["android", "iphone"]
if any(x in user_agent for x in mobile):
return True
return False
@app.route("/alamo/")
@app.route("/alamo")
def alamo():
return redirect("https://github.com/solidsgroup/alamo/tree/flame")
@app.route("/classification")
@app.route("/classification/")
def classification():
return redirect("https://github.com/meierms1/Supervised-Dimension-Reduction-For-Optical-Vapor-Sensing.git")
@app.route("/", methods=["GET","POST"])
def home():
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
msg = Message(subject = f"Mail From Flask page, from {name} ",body = f"name = {name} \n \n email = {email} \n\n {message}",sender = '[email protected]',recipients = ["[email protected]", "[email protected]"])
mail.send(msg)
return render_template("about.html", success=True)
return render_template("about.html")
@app.route("/about/", methods=["GET","POST"])
def about():
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
msg = Message(subject = f"Mail From Flask page, from {name} ",body = f"name = {name} \n \n email = {email} \n\n {message}",sender = '[email protected]',recipients = ["[email protected]","[email protected]"])
mail.send(msg)
return render_template("about.html", success=True)
return render_template("about.html")
@app.route("/resume/")
def resume():
if (user_on_mobile()): return render_template("resume-mobile.html")
return render_template("resume.html")
@app.route("/finance/", methods=["GET", "POST"])
def finance():
if request.method == "POST":
ticker = request.form.get("ticker_name")
period = request.form.get("period")
start = request.form.get("start_date")
end = request.form.get("end_date")
print(period)
print(request.form)
if period != "":
period = request.form.get("period")
label, price = get_stock_data(ticker, period)
elif start != "":
start = request.form.get("start_date")
end = request.form.get("end_date")
label, price = get_stock_data(ticker, start, end)
else:
print("HERE")
label, price = get_stock_data(ticker)
info_data=get_stock_info(ticker)
if (user_on_mobile()): return render_template("finance-mobile.html", labels=label, values=price, stock_info = info_data, hide_block=False)
return render_template("finance.html", labels=label, values=price, stock_info = info_data, hide_block=False)
label = ["1", "2"]
price = [1,1]
info_data=["-","-","-","-","-","-","-"]
if (user_on_mobile()): return render_template("finance-mobile.html", labels=label, values=price, stock_info = info_data, hide_block=True)
return render_template("finance.html", labels=label, values=price, stock_info = info_data, hide_block=True)
@app.route("/projects/")
def projects():
if (user_on_mobile()): return render_template("projects-mobile.html")
return render_template("projects.html")
@app.route("/calculator/", methods=["GET","POST"])
def unittool():
if (request.method == "POST"):
try:
e = -1; g = -1; k = -1; l = -1; v = -1
first = request.form.get("first_property_name")
second = request.form.get("second_property_name")
if first == "young": e = float(request.form.get("first_property_value"))
elif first == "shear": g = float(request.form.get("first_property_value"))
elif first == "bulk": k = float(request.form.get("first_property_value"))
elif first == "lame": l = float(request.form.get("first_property_value"))
elif first == "poisson": v = float(request.form.get("first_property_value"))
if second == "young": e = float(request.form.get("second_property_value"))
elif second == "shear": g = float(request.form.get("second_property_value"))
elif second == "bulk": k = float(request.form.get("second_property_value"))
elif second == "lame": l = float(request.form.get("second_property_value"))
elif second == "poisson": v = float(request.form.get("second_property_value"))
calc = material(K = k, E = e, lame = l, G = g, Poisson = v)
return render_template("tools.html", success_compute=True, E=round(calc.E, 3), G=round(calc.G, 3), K=round(calc.K, 3), L=round(calc.lame, 3), V=round(calc.Poisson, 3),value="Value")
except:
input_value = float(request.form.get("input_value"))
input_unit = request.form.get("input_unit")
output_unit = request.form.get("output_unit")
if len(input_unit) > 50 or len(output_unit) > 50:
flash("String is too long. Stopping for security")
return render_template("tools.html", success_convert=True, value="String is too long")
try:
convert = GeneralConverter(input_value, input_unit, output_unit)
output_value = convert.converted_value
print(output_value)
return render_template("tools.html", success_convert=True, value=str(output_value))
except:
return render_template("tools.html", success_convert=True, value="Incompatible units")
return render_template("tools.html", value= "Value")
@app.route("/dashboard/", methods=["GET", "POST"])
@login_required
def dashboard():
c_user = current_user.id
if request.method == "POST":
if "add_buy" in request.form:
ticker = request.form.get("ticker_name")
price = float(request.form.get("ticker_price"))
date = request.form.get("action_date")
date = datetime.datetime.strptime(date, '%Y-%m-%d')
amount = int(request.form.get("ticker_amount"))
total = amount * price
user = c_user
new_transaction = Dashinfo(ticker = ticker, price = price,date = date,type = "BUY", amount = amount,user = user, total=total)
db.session.add(new_transaction)
db.session.commit()
elif "add_sell" in request.form:
ticker = request.form.get("ticker_name")
price = float(request.form.get("ticker_price"))
date = request.form.get("action_date")
date = datetime.datetime.strptime(date, '%Y-%m-%d')
amount = int(request.form.get("ticker_amount"))
user = c_user
current_stock_amount_buy = db.session.query(func.sum(Dashinfo.amount)).filter(Dashinfo.user==c_user, Dashinfo.ticker==ticker, Dashinfo.type=="BUY").scalar()
current_stock_amount_sell = db.session.query(func.sum(Dashinfo.amount)).filter(Dashinfo.user==c_user, Dashinfo.ticker==ticker, Dashinfo.type=="SELL").scalar()
if current_stock_amount_buy is None: current_stock_amount_buy = 0
if current_stock_amount_sell is None: current_stock_amount_sell = 0
current_ballance = current_stock_amount_buy - current_stock_amount_sell
if current_ballance < int(amount):
flash("You can not sell more than you have")
else:
new_transaction = Dashinfo(ticker = ticker, price = price*(-1),date = date,type = "SELL", amount = amount*(-1), user = user)
db.session.add(new_transaction)
db.session.commit()
elif "remove_transaction" in request.form:
_id = request.form.get("remove_from_db")
user = c_user
transaction_id = Dashinfo.query.filter(Dashinfo.id==_id).first()
if transaction_id.user == user:
db.session.delete(transaction_id)
db.session.commit()
else:
flash("Declined: This transaction doesn't belong to you.")
data = Dashinfo.query.filter(Dashinfo.user==c_user).all()
data_config=[]
names = []
for i in data:
if (user_on_mobile()):
data_config.append([i.id,i.ticker, np.abs(i.price), np.abs(i.amount), i.type])
else:
data_config.append([i.id,i.ticker, np.abs(i.price), i.date, np.abs(i.amount), i.type])
if i.ticker not in names:
names.append(i.ticker)
sum_price = []
local_changes = []
for i in names:
data = Dashinfo.query.filter(Dashinfo.user==c_user, Dashinfo.ticker==i).all()
p_buy = db.session.query(func.sum(Dashinfo.price)).filter(Dashinfo.user==c_user, Dashinfo.ticker==i, Dashinfo.type=="BUY").scalar()
p_sell = db.session.query(func.sum(Dashinfo.price)).filter(Dashinfo.user==c_user, Dashinfo.ticker==i, Dashinfo.type=="SELL").scalar()
a_buy = db.session.query(func.sum(Dashinfo.amount)).filter(Dashinfo.user==c_user, Dashinfo.ticker==i, Dashinfo.type=="BUY").scalar()
a_sell = db.session.query(func.sum(Dashinfo.amount)).filter(Dashinfo.user==c_user, Dashinfo.ticker==i, Dashinfo.type=="SELL").scalar()
if p_buy is None: p_buy = 0;
if p_sell is None: p_sell = 0;
if a_buy is None: a_buy = 0;
if a_sell is None: a_sell = 0;
value = (p_buy*a_buy - p_sell*a_sell)
print(f"{i} price {value}")
avg_prepare = [j.price for j in data if j.type == "BUY"]
avg_prepare2 = [j.amount for j in data if j.type == "BUY"]
avg_prepare3 = []
remeinder = np.abs(a_sell)
print(remeinder)
print(a_sell)
total_buy = sum([i*j for i,j in zip(avg_prepare, avg_prepare2)])
price_avg = total_buy / a_buy
print(f"{i} avg price {price_avg}")
current_price = get_current_price(i)
current_cap = (a_buy - a_sell) * price_avg
current_market = (a_buy - a_sell) * current_price
print(current_price)
sum_price.append(value)
local_changes.append(current_market - current_cap)
total_capital = round(sum(sum_price), 2)
total_change = round(sum(local_changes),2)
if total_change is None: total_change=0
if total_capital is None or total_capital==0: total_capital=1
if (user_on_mobile()): return render_template('/dashboard-mobile.html', data_table=data_config, tickers_list=names, sum_price=sum_price, local_changes=local_changes, total_capital=total_capital, total_change=total_change)
return render_template('/dashboard.html', data_table=data_config, tickers_list=names, sum_price=sum_price, local_changes=local_changes, total_capital=total_capital, total_change=total_change)
@app.route("/register/", methods=["GET", "POST"])
def signup():
form = RegisterForm()
if form.validate_on_submit():
new_user = User(username=form.username.data, password=form.password.data, email=form.email.data)
db.session.add(new_user)
db.session.commit()
return redirect("/sign-in/")
return render_template('/register.html', form=form)
@app.route("/Logout/")
@login_required
def logout():
logout_user()
return redirect("/about/")
if __name__ == "__main__":
app.run(debug=True) #False, port=5000, host="0.0.0.0")