-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
323 lines (234 loc) · 10.1 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
from flask import Flask
from flask import request, redirect, url_for, render_template
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from wtforms.validators import InputRequired, Length, Email, ValidationError
from flask_login import UserMixin, LoginManager, login_user, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, FloatField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
import requests
import redis
import json
import logging
from datetime import datetime
from sqlalchemy.orm import Session
import os
#Dont forgit add API_Key
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy()
db.init_app(app)
app.config['SECRET_KEY'] = 'thisisasecretkey'
# Créez une instance de connexion Redis
redis_client = redis.StrictRedis(host='localhost', port=5000, db=0)
log_filename = 'app_log.json'
logging.basicConfig(filename=log_filename, level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
login_manager = LoginManager(app)
bcrypt = Bcrypt(app)
migrate = Migrate(app, db)
session = Session()
login_manager.login_view = "login"
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique=True)
email = db.Column(db.String(120), nullable=False, unique=True) # Champ email ajouté
password = db.Column(db.String(80), nullable=False)
class Alert(db.Model):
__tablename__ = 'alerts'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
asset = db.Column(db.String(10), nullable=False)
current_price = db.Column(db.Float)
target_price = db.Column(db.Float, nullable=False)
is_open = db.Column(db.Boolean, default=True)
open_date = db.Column(db.DateTime, default=datetime.utcnow) # Date d'ouverture
def __init__(self, user_id, asset, current_price, target_price, is_open=True):
self.user_id = user_id
self.asset = asset
self.current_price = current_price
self.target_price = target_price
self.is_open = is_open
self.open_date = datetime.utcnow() if is_open else None
login_manager.login_view = "login"
@login_manager.user_loader
def load_user(user_id):
user = User.query.get(int(user_id)) # Utilisez User.query au lieu de session.get
return user
class RegisterForm(FlaskForm):
username = StringField(validators=[InputRequired(), Length(min=4, max=20)], render_kw={"placeholder": "Username"})
email = StringField(validators=[InputRequired(), Email(), Length(max=120)], render_kw={"placeholder": "Email"}) # Champ email ajouté
password = PasswordField(validators=[InputRequired(), Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Register')
def validate_username(self, username):
existing_user_username = User.query.filter_by(username=username.data).first()
print(existing_user_username)
if existing_user_username:
raise ValidationError(
'That username already exists. Please choose a different one.')
def validate_email(self, email):
existing_user_email = User.query.filter_by(email=email.data).first()
print(existing_user_email)
if existing_user_email:
raise ValidationError('That email address is already registered. Please choose a different one.')
class LoginForm(FlaskForm):
username = StringField(validators=[InputRequired(),
Length(min=4, max=20)], render_kw={"placeholder": "Username"})
password = PasswordField(validators=[InputRequired(),
Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Login')
class EditAlertForm(FlaskForm):
asset = StringField('Actif', validators=[InputRequired()])
target_price = FloatField('Prix cible', validators=[InputRequired()])
#ADD YOUR API_KEY
api_key = "CA89529B-FA3C-44B1-B65D-3BED3D6AAE70"
# api_key = "60F8A35F-8603-4E56-8A20-8C82BBAA99EA"
#ADD MOR ASSETS
assets = ['BTC', 'ETH', 'XRP']
@app.route('/erreur_assets.html')
def erreur_assets():
return render_template('erreur_assets.html')
def get_current_price(asset):
url = f"https://rest.coinapi.io/v1/exchangerate/{asset}/USD"
headers = {
"X-CoinAPI-Key": api_key
}
response = requests.get(url, headers=headers)
data = response.json()
print(data)
if 'rate' in data:
return data['rate']
else:
return None
def crypto_portfolio():
exchange_rates = {}
# Récupérer les taux de change pour chaque crypto-monnaie par rapport
for asset in assets:
asset_data = get_current_price(asset)
exchange_rates[asset] = asset_data
return exchange_rates
@app.route("/")
def accueil():
exchange_rates = crypto_portfolio()
return render_template("index.html", exchange_rates=exchange_rates)
@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 bcrypt.check_password_hash(user.password, form.password.data):
login_user(user)
return redirect(url_for('dashboard'))
return render_template('login.html', form=form)
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
return render_template('dashboard.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
new_user = User(username=form.username.data, email=form.email.data, password=hashed_password) # Ajout de l'email
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html', form=form)
@app.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/set_alert_page')
@login_required
def set_alert_page():
return render_template('set_alert.html')
@app.route('/set_alert', methods=['POST'])
@login_required
def set_alert():
asset = request.form.get('asset')
target_price = float(request.form.get('target_price'))
is_open = True
try:
current_price = get_current_price(asset)
if current_price is None:
return redirect(url_for('erreur_assets'))
new_alert = Alert(
user_id=current_user.id,
asset=asset,
current_price=current_price, # Ajoutez la valeur du current_price à l'alerte
target_price=target_price,
is_open=is_open
)
db.session.add(new_alert)
db.session.commit()
alert_message = f"Notification : Votre alerte {new_alert.id} a été créée avec un current_price de {current_price}"
print(alert_message)
with open('alerts_open.json', 'a') as log_file:
log_file.write(alert_message + '\n')
return redirect(url_for('mes_alertes'))
except Exception as e:
return redirect(url_for('erreur_assets'))
@app.route('/mes_alertes')
@login_required
def mes_alertes():
user = current_user
# Mise à jour l'état de toutes les alertes en fonction du prix actuel
alerts = Alert.query.filter_by(user_id=user.id).all()
for alert in alerts:
current_price_live = get_current_price(alert.asset)
print(current_price_live)
if current_price_live is not None:
if alert.is_open:
if alert.target_price > alert.current_price:
# Passez l'alerte en "close" si le current price devient égal ou supérieur au Prix cible
if current_price_live >= alert.target_price:
alert.is_open = False
elif alert.target_price < alert.current_price:
# Passez l'alerte en "close" si le current price devient inférieur ou égal au Prix cible
if current_price_live <= alert.target_price:
alert.is_open = False
db.session.commit() # Mettez à jour la base de données
open_alerts = [alert for alert in alerts if alert.is_open]
closed_alerts = [alert for alert in alerts if not alert.is_open]
with open('alerts_close.json', 'r') as log_file:
existing_alerts = log_file.read()
for alert in closed_alerts:
# Vérifiez si l'alerte a déjà été enregistrée dans le fichier
alert_message = f"Notification : Votre alerte {alert.id} a été exécutée"
if alert_message not in existing_alerts:
print(alert_message)
# Enregistrez le message dans le fichier journal
with open('alerts_close.json', 'a') as log_file:
log_file.write(alert_message + '\n')
return render_template('mes_alertes.html', open_alerts=open_alerts, closed_alerts=closed_alerts)
@app.route('/edit_alert/<int:alert_id>', methods=['GET', 'POST'])
@login_required
def edit_alert(alert_id):
alert = Alert.query.get(alert_id)
if not alert:
return redirect(url_for('mes_alertes'))
form = EditAlertForm()
if form.validate_on_submit():
alert.asset = form.asset.data
alert.target_price = form.target_price.data
db.session.commit()
return redirect(url_for('mes_alertes'))
form.asset.data = alert.asset
form.target_price.data = alert.target_price
return render_template('edit_alert.html', form=form, alert=alert)
@app.route('/delete_alert/<int:alert_id>', methods=['POST'])
@login_required
def delete_alert(alert_id):
alert = Alert.query.get(alert_id)
if alert:
db.session.delete(alert)
db.session.commit()
return redirect(url_for('mes_alertes'))
if __name__ == '__main__':
app.run(debug=True)