Menu 1 4 5 7
This commit is contained in:
+115
-1
@@ -1,7 +1,9 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, session, flash
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, session, flash, Response, session
|
||||
import time
|
||||
import requests
|
||||
import mysql.connector
|
||||
import csv
|
||||
import io
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
from sqlalchemy import text
|
||||
from app import db
|
||||
@@ -296,3 +298,115 @@ def gestion_societes():
|
||||
return render_template('gestion_societes.html', societes=societes)
|
||||
|
||||
|
||||
# Routes pour la gestion des imports exports des actions csv
|
||||
@main.route('/gestion-import-export-actions-csv', methods=['GET'])
|
||||
def gestion_import_export_actions_csv():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('main.index'))
|
||||
return render_template('gestion_import_export_actions_csv.html')
|
||||
|
||||
@main.route('/export-actions-csv', methods=['GET'])
|
||||
def export_actions_csv():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
try:
|
||||
# Récupération de toutes les données de la table actions
|
||||
query = text("SELECT id, isin, ticker, company_name, exchange, pays, currency, updated_at FROM actions")
|
||||
result = db.session.execute(query)
|
||||
|
||||
# Création du fichier CSV en mémoire
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output, delimiter=';', quoting=csv.QUOTE_MINIMAL)
|
||||
|
||||
# Écriture de l'en-tête du CSV
|
||||
writer.writerow(['id', 'isin', 'ticker', 'company_name', 'exchange', 'pays', 'currency', 'updated_at'])
|
||||
|
||||
# Écriture des lignes
|
||||
for row in result:
|
||||
writer.writerow([
|
||||
row.id,
|
||||
row.isin or '',
|
||||
row.ticker or '',
|
||||
row.company_name or '',
|
||||
row.exchange or '',
|
||||
row.pays or '',
|
||||
row.currency or '',
|
||||
row.updated_at.strftime('%Y-%m-%d %H:%M:%S') if row.updated_at else ''
|
||||
])
|
||||
|
||||
output.seek(0)
|
||||
|
||||
# Envoi du fichier en téléchargement
|
||||
return Response(
|
||||
output.getvalue(),
|
||||
mimetype="text/csv",
|
||||
headers={"Content-Disposition": "attachment;filename=actions_export.csv"}
|
||||
)
|
||||
except Exception as e:
|
||||
flash("Erreur lors de l'exportation du fichier CSV.", "danger")
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
|
||||
@main.route('/import-actions-csv', methods=['POST'])
|
||||
def import_actions_csv():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
if 'file' not in request.files:
|
||||
flash("Aucun fichier sélectionné.", "danger")
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
|
||||
file = request.files['file']
|
||||
|
||||
if file.filename == '':
|
||||
flash("Aucun fichier sélectionné.", "danger")
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
|
||||
if file and file.filename.endswith('.csv'):
|
||||
try:
|
||||
stream = io.TextIOWrapper(file.stream, encoding="utf-8")
|
||||
csv_reader = csv.reader(stream, delimiter=';')
|
||||
|
||||
header = next(csv_reader, None)
|
||||
if not header:
|
||||
flash("Le fichier CSV est vide.", "danger")
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
|
||||
count = 0
|
||||
for row in csv_reader:
|
||||
# Vérifie qu'on a au moins les 6 colonnes de base
|
||||
if len(row) >= 6:
|
||||
isin = row[0].strip() if row[0] != '' else None
|
||||
ticker = row[1].strip() if row[1] != '' else None
|
||||
company_name = row[2].strip() if row[2] != '' else None
|
||||
exchange = row[3].strip() if row[3] != '' else ''
|
||||
pays = row[4].strip() if row[4] != '' else ''
|
||||
currency = row[5].strip() if row[5] != '' else ''
|
||||
|
||||
if not isin or not ticker:
|
||||
continue # Ignore les lignes mal formatées
|
||||
|
||||
# Insertion ou mise à jour si le code ISIN existe déjà
|
||||
query = text("""
|
||||
INSERT INTO actions (isin, ticker, company_name, exchange, pays, currency, updated_at)
|
||||
VALUES (:isin, :ticker, :company_name, :exchange, :pays, :currency, NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
ticker = :ticker, company_name = :company_name,
|
||||
exchange = :exchange, pays = :pays, currency = :currency, updated_at = NOW()
|
||||
""")
|
||||
db.session.execute(query, {
|
||||
"isin": isin, "ticker": ticker, "company_name": company_name,
|
||||
"exchange": exchange, "pays": pays, "currency": currency
|
||||
})
|
||||
count += 1
|
||||
|
||||
db.session.commit()
|
||||
flash(f"Importation réussie : {count} actions traitées.", "success")
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash(f"Erreur lors de l'importation du fichier : {str(e)}", "danger")
|
||||
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
else:
|
||||
flash("Veuillez fournir un fichier au format .csv valide.", "warning")
|
||||
return redirect(url_for('main.gestion_import_export_actions_csv'))
|
||||
@@ -0,0 +1,192 @@
|
||||
{% extends "base.html" %} {% block title %}Import / Export Actions - Bolsa{%
|
||||
endblock %} {% block header_title %}Gestion CSV{% endblock %} {% block content
|
||||
%}
|
||||
<div class="flex flex-col items-center justify-center py-6 relative">
|
||||
<!-- Système de Toasts (pour les messages flash de succès/erreur) -->
|
||||
<div
|
||||
id="toast-container"
|
||||
class="fixed top-6 left-1/2 transform -translate-x-1/2 z-50 space-y-3 w-full max-w-md px-4 pointer-events-none"
|
||||
>
|
||||
{% set messages = get_flashed_messages(with_categories=true) %} {% if
|
||||
messages %} {% for category, message in messages %}
|
||||
<div
|
||||
class="toast-message flex items-center px-4 py-3 rounded-xl shadow-2xl text-white text-sm font-medium transition-all duration-300 transform translate-y-0 opacity-100 {% if category == 'success' %} bg-emerald-600 border border-emerald-500 {% elif category == 'warning' %} bg-amber-600 border border-amber-500 {% else %} bg-rose-600 border border-rose-500 {% endif %}"
|
||||
>
|
||||
<span>{{ message }}</span>
|
||||
</div>
|
||||
{% endfor %} {% endif %}
|
||||
</div>
|
||||
|
||||
<!-- Box centrée principale -->
|
||||
<div
|
||||
class="bg-gray-900 border border-gray-800 rounded-2xl shadow-2xl w-full max-w-3xl overflow-hidden"
|
||||
>
|
||||
<!-- 1. En-tête de la boîte (Titre) -->
|
||||
<div
|
||||
class="flex justify-between items-center px-8 py-6 border-b border-gray-800 bg-gray-950/50"
|
||||
>
|
||||
<div>
|
||||
<h2 class="text-xl font-extrabold text-white">
|
||||
Import & Export - Table Actions
|
||||
</h2>
|
||||
<p class="text-gray-400 text-sm mt-0.5">
|
||||
Gestion des transferts de données au format CSV.
|
||||
</p>
|
||||
</div>
|
||||
<a
|
||||
href="{{ url_for('main.menu') }}"
|
||||
class="px-4 py-2 bg-gray-800 hover:bg-gray-700 text-gray-200 text-sm font-medium rounded-lg transition duration-200"
|
||||
>
|
||||
Retour au menu
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- 2. Corps de la boîte (Procédure et Boutons) -->
|
||||
<div class="p-8 space-y-6">
|
||||
<div class="bg-gray-950 border border-gray-800 rounded-xl p-6 space-y-4">
|
||||
<h3
|
||||
class="text-sm font-semibold text-gray-200 uppercase tracking-wider"
|
||||
>
|
||||
Procédure d'utilisation
|
||||
</h3>
|
||||
|
||||
<div class="text-sm text-gray-300 space-y-3 leading-relaxed">
|
||||
<div>
|
||||
<strong class="text-white">1. Exportation des données :</strong>
|
||||
<p class="text-gray-400 mt-0.5">
|
||||
Cliquez sur le bouton <strong>Exporter en CSV</strong> pour
|
||||
télécharger une copie de sauvegarde de la table
|
||||
<code
|
||||
class="text-blue-400 bg-gray-900 px-1.5 py-0.5 rounded font-mono"
|
||||
>actions</code
|
||||
>
|
||||
sur votre poste. Le fichier contiendra l'ensemble des colonnes
|
||||
actuelles (<code class="text-gray-300 font-mono"
|
||||
>isin, ticker, company_name, exchange, pays, currency</code
|
||||
>).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong class="text-white">2. Importation des données :</strong>
|
||||
<p class="text-gray-400 mt-0.5">
|
||||
Préparez votre fichier au format CSV en respectant scrupuleusement
|
||||
l'ordre et le nom des en-têtes de colonnes (les champs
|
||||
<code
|
||||
class="text-blue-400 bg-gray-900 px-1.5 py-0.5 rounded font-mono"
|
||||
>id</code
|
||||
>
|
||||
et
|
||||
<code
|
||||
class="text-blue-400 bg-gray-900 px-1.5 py-0.5 rounded font-mono"
|
||||
>updated_at</code
|
||||
>
|
||||
sont facultatifs et gérés automatiquement) :
|
||||
<code
|
||||
class="text-blue-400 bg-gray-900 px-1.5 py-0.5 rounded font-mono"
|
||||
>isin;ticker;company_name;exchange;pays;currency</code
|
||||
>. Les données d'une même ligne doivent être séparées par un
|
||||
point-virgule (<strong>;</strong>) et chaque enregistrement doit
|
||||
simplement se trouver sur une nouvelle ligne. Cliquez sur
|
||||
<strong>Importer un CSV</strong> pour sélectionner et téléverser
|
||||
votre fichier vers la base de données.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="bg-amber-500/10 border border-amber-500/30 rounded-lg p-3 text-amber-300 text-xs flex items-start space-x-2"
|
||||
>
|
||||
<svg
|
||||
class="w-4 h-4 text-amber-400 shrink-0 mt-0.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
||||
></path>
|
||||
</svg>
|
||||
<span
|
||||
><strong>Attention :</strong> Assurez-vous de l'intégrité de vos
|
||||
données avant de procéder à un import massif pour éviter toute
|
||||
incohérence dans le référentiel boursier.</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Boutons d'action dans le corps -->
|
||||
<div
|
||||
class="flex flex-col sm:flex-row items-center justify-center gap-4 pt-2"
|
||||
>
|
||||
<!-- Formulaire ou lien pour l'Export (réduit) -->
|
||||
<a
|
||||
href="{{ url_for('main.export_actions_csv') }}"
|
||||
class="w-full sm:w-1/4 flex items-center justify-center px-4 py-3 bg-blue-600 hover:bg-blue-500 text-white text-sm font-semibold rounded-xl shadow-lg transition duration-200"
|
||||
>
|
||||
<svg
|
||||
class="w-4 h-4 mr-2 shrink-0"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"
|
||||
></path>
|
||||
</svg>
|
||||
Exporter
|
||||
</a>
|
||||
|
||||
<!-- Formulaire pour l'Import (prend toute la place restante) -->
|
||||
<form
|
||||
action="{{ url_for('main.import_actions_csv') }}"
|
||||
method="POST"
|
||||
enctype="multipart/form-data"
|
||||
class="w-full sm:w-3/4 flex items-center gap-2"
|
||||
>
|
||||
<input
|
||||
type="file"
|
||||
name="file"
|
||||
accept=".csv"
|
||||
required
|
||||
class="block w-full text-xs text-gray-400 file:mr-4 file:py-3 file:px-4 file:rounded-xl file:border-0 file:text-sm file:font-semibold file:bg-emerald-600 file:text-white hover:file:bg-emerald-500 file:cursor-pointer cursor-pointer bg-gray-950 border border-gray-800 rounded-xl"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
class="px-5 py-3 bg-emerald-600 hover:bg-emerald-500 text-white text-sm font-semibold rounded-xl shadow-lg transition duration-200 whitespace-nowrap"
|
||||
>
|
||||
Envoyer
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 3. Pied de boîte -->
|
||||
<div
|
||||
class="px-8 py-4 bg-gray-950 border-t border-gray-800 text-xs text-gray-400 italic text-center"
|
||||
>
|
||||
Module de maintenance sécurisé — Giraud Finance - 2026
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Gestion de la disparition automatique des messages flash (Toasts)
|
||||
const toasts = document.querySelectorAll(".toast-message");
|
||||
toasts.forEach((toast) => {
|
||||
setTimeout(() => {
|
||||
toast.style.opacity = "0";
|
||||
toast.style.transform = "translateY(10px)";
|
||||
setTimeout(() => toast.remove(), 300);
|
||||
}, 4000);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -63,7 +63,7 @@ header_title %}Tableau de bord{% endblock %} {% block content %}
|
||||
|
||||
<!-- btn 7 -->
|
||||
<a
|
||||
href="#"
|
||||
href="{{ url_for('main.gestion_import_export_actions_csv') }}"
|
||||
class="py-6 px-6 bg-gray-950 hover:bg-blue-600/30 border-2 border-gray-800 hover:border-blue-500 rounded-xl text-white text-lg font-bold tracking-wide transition-all duration-200 flex items-center justify-center shadow-lg hover:scale-[1.01]"
|
||||
>
|
||||
7. Import/Export Actions CSV
|
||||
|
||||
Reference in New Issue
Block a user