98 lines
3.6 KiB
Python
98 lines
3.6 KiB
Python
|
|
from flask import Blueprint, flash, redirect, render_template, request, url_for
|
||
|
|
from sqlalchemy import text
|
||
|
|
|
||
|
|
from app import db
|
||
|
|
from app.decorators import login_required
|
||
|
|
|
||
|
|
actions_bp = Blueprint("actions", __name__)
|
||
|
|
|
||
|
|
|
||
|
|
@actions_bp.route("/gestion-actions", methods=["GET", "POST"])
|
||
|
|
@login_required
|
||
|
|
def gestion_actions():
|
||
|
|
if request.method == "POST":
|
||
|
|
action_type = request.form.get("action")
|
||
|
|
action_id = request.form.get("id")
|
||
|
|
isin = request.form.get("isin") or None
|
||
|
|
ticker = request.form.get("ticker")
|
||
|
|
company_name = request.form.get("company_name")
|
||
|
|
exchange = request.form.get("exchange")
|
||
|
|
pays = request.form.get("pays")
|
||
|
|
currency = request.form.get("currency")
|
||
|
|
|
||
|
|
if action_type == "creer":
|
||
|
|
try:
|
||
|
|
query = text(
|
||
|
|
"INSERT INTO actions (isin, ticker, company_name, exchange, "
|
||
|
|
"pays, currency, updated_at) "
|
||
|
|
"VALUES (:isin, :ticker, :company_name, :exchange, :pays, "
|
||
|
|
":currency, NOW())"
|
||
|
|
)
|
||
|
|
db.session.execute(
|
||
|
|
query,
|
||
|
|
{
|
||
|
|
"isin": isin,
|
||
|
|
"ticker": ticker,
|
||
|
|
"company_name": company_name,
|
||
|
|
"exchange": exchange,
|
||
|
|
"pays": pays,
|
||
|
|
"currency": currency,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
db.session.commit()
|
||
|
|
flash("Action créée avec succès", "success")
|
||
|
|
except Exception:
|
||
|
|
db.session.rollback()
|
||
|
|
flash("Erreur lors de la création de l'action.", "danger")
|
||
|
|
|
||
|
|
elif action_type == "modifier" and action_id:
|
||
|
|
try:
|
||
|
|
query = text(
|
||
|
|
"UPDATE actions SET isin = :isin, ticker = :ticker, "
|
||
|
|
"company_name = :company_name, exchange = :exchange, "
|
||
|
|
"pays = :pays, currency = :currency, updated_at = NOW() "
|
||
|
|
"WHERE id = :id"
|
||
|
|
)
|
||
|
|
db.session.execute(
|
||
|
|
query,
|
||
|
|
{
|
||
|
|
"id": action_id,
|
||
|
|
"isin": isin,
|
||
|
|
"ticker": ticker,
|
||
|
|
"company_name": company_name,
|
||
|
|
"exchange": exchange,
|
||
|
|
"pays": pays,
|
||
|
|
"currency": currency,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
db.session.commit()
|
||
|
|
flash("Modification effectuée", "success")
|
||
|
|
except Exception:
|
||
|
|
db.session.rollback()
|
||
|
|
flash("Erreur lors de la modification.", "danger")
|
||
|
|
|
||
|
|
return redirect(url_for("actions.gestion_actions"))
|
||
|
|
|
||
|
|
actions_query = text(
|
||
|
|
"SELECT id, isin, ticker, company_name, exchange, pays, currency, "
|
||
|
|
"updated_at FROM actions ORDER BY company_name ASC"
|
||
|
|
)
|
||
|
|
result = db.session.execute(actions_query)
|
||
|
|
|
||
|
|
actions_list = [
|
||
|
|
{
|
||
|
|
"id": row.id,
|
||
|
|
"isin": row.isin if row.isin else "",
|
||
|
|
"ticker": row.ticker if row.ticker else "",
|
||
|
|
"company_name": row.company_name if row.company_name else "Sans nom",
|
||
|
|
"exchange": row.exchange if row.exchange else "",
|
||
|
|
"pays": row.pays if row.pays else "",
|
||
|
|
"currency": row.currency if row.currency else "",
|
||
|
|
"updated_at": row.updated_at.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
|
if row.updated_at
|
||
|
|
else "",
|
||
|
|
}
|
||
|
|
for row in result
|
||
|
|
]
|
||
|
|
|
||
|
|
return render_template("gestion_actions.html", actions=actions_list)
|