Ajout du module gestion des actions et correction qq bugs
This commit is contained in:
+94
-7
@@ -6,7 +6,7 @@ from datetime import datetime
|
||||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
|
||||
# Route pour la page d'accueil
|
||||
@main.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
if 'user_id' in session:
|
||||
@@ -39,17 +39,20 @@ def index():
|
||||
|
||||
return render_template('index.html')
|
||||
|
||||
# Route pour la déconnexion
|
||||
@main.route('/logout')
|
||||
def logout():
|
||||
session.clear()
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
# Route pour le menu principal
|
||||
@main.route('/menu')
|
||||
def menu():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('main.index'))
|
||||
return render_template('menu.html')
|
||||
|
||||
@main.route('/logout')
|
||||
def logout():
|
||||
session.clear()
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
# Route pour la gestion des utilisateurs
|
||||
@main.route('/gestion-utilisateurs', methods=['GET', 'POST'])
|
||||
def gestion_utilisateurs():
|
||||
if 'user_id' not in session:
|
||||
@@ -136,4 +139,88 @@ def gestion_utilisateurs():
|
||||
'actif': row.actif if row.actif is not None else 1
|
||||
})
|
||||
|
||||
return render_template('gestion_utilisateurs.html', users=users)
|
||||
return render_template('gestion_utilisateurs.html', users=users)
|
||||
|
||||
# Route pour la gestion des actions
|
||||
@main.route('/gestion-actions', methods=['GET', 'POST'])
|
||||
def gestion_actions():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
if request.method == 'POST':
|
||||
action_type = request.form.get('action')
|
||||
# On récupère aussi l'id caché s'il s'agit d'une modification
|
||||
action_id = request.form.get('id')
|
||||
isin = request.form.get('isin')
|
||||
ticker = request.form.get('ticker')
|
||||
yahoo_code = request.form.get('yahoo_code')
|
||||
company_name = request.form.get('company_name')
|
||||
|
||||
if action_type == 'creer':
|
||||
if isin:
|
||||
try:
|
||||
# Vérifier si l'ISIN existe déjà
|
||||
check_query = text("SELECT COUNT(*) FROM actions WHERE isin = :isin")
|
||||
exists = db.session.execute(check_query, {"isin": isin}).scalar()
|
||||
|
||||
if exists > 0:
|
||||
flash("L'action existe déjà, pas de création.", "warning")
|
||||
else:
|
||||
# L'id s'incrémente tout seul, on ne l'inclus pas dans l'INSERT
|
||||
query = text("""
|
||||
INSERT INTO actions (isin, ticker, yahoo_code, company_name, updated_at)
|
||||
VALUES (:isin, :ticker, :yahoo_code, :company_name, NOW())
|
||||
""")
|
||||
db.session.execute(query, {
|
||||
"isin": isin,
|
||||
"ticker": ticker,
|
||||
"yahoo_code": yahoo_code,
|
||||
"company_name": company_name
|
||||
})
|
||||
db.session.commit()
|
||||
flash("Action créée", "success")
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash("Erreur lors de la création de l'action.", "danger")
|
||||
else:
|
||||
flash("L'ISIN est obligatoire.", "danger")
|
||||
|
||||
elif action_type == 'modifier' and action_id:
|
||||
try:
|
||||
# On met à jour en se basant sur l'id technique unique
|
||||
query = text("""
|
||||
UPDATE actions
|
||||
SET isin = :isin, ticker = :ticker, yahoo_code = :yahoo_code, company_name = :company_name, updated_at = NOW()
|
||||
WHERE id = :id
|
||||
""")
|
||||
db.session.execute(query, {
|
||||
"id": action_id,
|
||||
"isin": isin,
|
||||
"ticker": ticker,
|
||||
"yahoo_code": yahoo_code,
|
||||
"company_name": company_name
|
||||
})
|
||||
db.session.commit()
|
||||
flash("Modification effectuée", "success")
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash("Erreur lors de la modification (Vérifiez si l'ISIN n'est pas déjà utilisé par une autre ligne).", "danger")
|
||||
|
||||
return redirect(url_for('main.gestion_actions'))
|
||||
|
||||
# On sélectionne l'id en plus dans la requête
|
||||
actions_query = text("SELECT id, isin, ticker, yahoo_code, company_name, updated_at FROM actions ORDER BY company_name ASC")
|
||||
result = db.session.execute(actions_query)
|
||||
|
||||
actions_list = []
|
||||
for row in result:
|
||||
actions_list.append({
|
||||
'id': row.id,
|
||||
'isin': row.isin if row.isin else '',
|
||||
'ticker': row.ticker if row.ticker else '',
|
||||
'yahoo_code': row.yahoo_code if row.yahoo_code else '',
|
||||
'company_name': row.company_name if row.company_name else 'Sans nom',
|
||||
'updated_at': row.updated_at.strftime('%Y-%m-%d %H:%M:%S') if row.updated_at else ''
|
||||
})
|
||||
|
||||
return render_template('gestion_actions.html', actions=actions_list)
|
||||
Reference in New Issue
Block a user