- Filtre Jinja2 day_type_fr (WORK→Travail, TT→Télétravail, etc.) - Appliqué dans entry_list.html et dashboard.html - Passage de vehicles au template dashboard pour afficher vehicle.name - Mise à jour du test test_entry_list en conséquence - Ajout du plan docs/plans/2026-03-11-bareme-kilometrique.md
98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
import tomllib
|
|
import os
|
|
import sqlalchemy as sa
|
|
|
|
db = SQLAlchemy()
|
|
|
|
|
|
def _migrate_db(app):
|
|
"""Applique les migrations de schéma manquantes (pas d'Alembic)."""
|
|
import sqlite3
|
|
db_path = os.path.join(app.instance_path, "worklog.db")
|
|
if not os.path.exists(db_path):
|
|
return # Nouvelle DB, create_all() s'en charge
|
|
conn = sqlite3.connect(db_path)
|
|
tables = {row[0] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
|
if "work_entries" not in tables:
|
|
conn.close()
|
|
return # Table pas encore créée, create_all() s'en charge
|
|
columns = {row[1] for row in conn.execute("PRAGMA table_info(work_entries)").fetchall()}
|
|
conn.close()
|
|
|
|
with app.app_context():
|
|
engine = db.engine
|
|
if "motor_vehicle_id" not in columns:
|
|
with engine.connect() as conn:
|
|
conn.execute(sa.text(
|
|
"ALTER TABLE work_entries ADD COLUMN motor_vehicle_id VARCHAR(64)"
|
|
))
|
|
conn.commit()
|
|
|
|
|
|
_JOURS_FR = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"]
|
|
_MOIS_FR = ["", "janvier", "février", "mars", "avril", "mai", "juin",
|
|
"juillet", "août", "septembre", "octobre", "novembre", "décembre"]
|
|
|
|
|
|
_DAY_TYPE_LABELS = {
|
|
"WORK": "Travail",
|
|
"TT": "Télétravail",
|
|
"GARDE": "Garde",
|
|
"ASTREINTE": "Astreinte",
|
|
"FORMATION": "Formation",
|
|
"RTT": "RTT",
|
|
"CONGE": "Congé",
|
|
"MALADE": "Maladie",
|
|
"FERIE": "Férié",
|
|
}
|
|
|
|
|
|
def _day_type_fr(code):
|
|
return _DAY_TYPE_LABELS.get(code, code)
|
|
|
|
|
|
def _date_fr(d):
|
|
"""Formate une date en français : 'mercredi 11 mars 2026'."""
|
|
from datetime import date as date_type
|
|
jour = _JOURS_FR[d.weekday()]
|
|
mois = _MOIS_FR[d.month]
|
|
return f"{jour} {d.day} {mois} {d.year}"
|
|
|
|
|
|
def create_app(config_path=None):
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
|
|
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{os.path.join(app.instance_path, 'worklog.db')}"
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "dev-secret-change-in-prod")
|
|
|
|
# Load TOML config
|
|
if config_path is None:
|
|
config_path = os.path.join(os.path.dirname(app.root_path), "config.toml")
|
|
if os.path.exists(config_path):
|
|
with open(config_path, "rb") as f:
|
|
app.config["TOML"] = tomllib.load(f)
|
|
else:
|
|
app.config["TOML"] = {}
|
|
|
|
db.init_app(app)
|
|
app.jinja_env.filters["date_fr"] = _date_fr
|
|
app.jinja_env.filters["day_type_fr"] = _day_type_fr
|
|
|
|
from app.routes.dashboard import bp as dashboard_bp
|
|
from app.routes.entries import bp as entries_bp
|
|
from app.routes.reports import bp as reports_bp
|
|
app.register_blueprint(dashboard_bp)
|
|
app.register_blueprint(entries_bp)
|
|
app.register_blueprint(reports_bp)
|
|
|
|
with app.app_context():
|
|
_migrate_db(app)
|
|
db.create_all()
|
|
|
|
return app
|