172 lines
5.9 KiB
Python
172 lines
5.9 KiB
Python
import json
|
|
import os
|
|
from typing import List, Optional
|
|
from pydantic import BaseModel
|
|
from pathlib import Path
|
|
|
|
|
|
class ServiceShortcut(BaseModel):
|
|
id: str
|
|
name: str
|
|
url: str
|
|
icon: str # Base64 or emoji or URL
|
|
description: Optional[str] = None
|
|
category: str = "other"
|
|
color: str = "#3B82F6" # Couleur personnalisée
|
|
order: int = 0
|
|
|
|
|
|
class ShortcutsConfig(BaseModel):
|
|
version: str = "1.0"
|
|
shortcuts: List[ServiceShortcut] = []
|
|
|
|
|
|
class ShortcutsService:
|
|
"""Service pour gérer les raccourcis vers les services self-hosted"""
|
|
|
|
CONFIG_FILE = "/home/innotex/Documents/Projet/innotexboard/backend/config/shortcuts.json"
|
|
|
|
@staticmethod
|
|
def _ensure_config_dir():
|
|
"""S'assurer que le répertoire de config existe"""
|
|
config_dir = os.path.dirname(ShortcutsService.CONFIG_FILE)
|
|
Path(config_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
@staticmethod
|
|
def _load_config() -> ShortcutsConfig:
|
|
"""Charger la configuration des raccourcis"""
|
|
ShortcutsService._ensure_config_dir()
|
|
|
|
if not os.path.exists(ShortcutsService.CONFIG_FILE):
|
|
return ShortcutsConfig()
|
|
|
|
try:
|
|
with open(ShortcutsService.CONFIG_FILE, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
return ShortcutsConfig(**data)
|
|
except Exception as e:
|
|
print(f"Erreur lors du chargement de la config: {e}")
|
|
return ShortcutsConfig()
|
|
|
|
@staticmethod
|
|
def _save_config(config: ShortcutsConfig):
|
|
"""Sauvegarder la configuration des raccourcis"""
|
|
ShortcutsService._ensure_config_dir()
|
|
|
|
try:
|
|
with open(ShortcutsService.CONFIG_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(config.model_dump(), f, indent=2, ensure_ascii=False)
|
|
except Exception as e:
|
|
raise Exception(f"Erreur lors de la sauvegarde: {str(e)}")
|
|
|
|
@staticmethod
|
|
def get_all_shortcuts() -> List[ServiceShortcut]:
|
|
"""Récupère tous les raccourcis"""
|
|
config = ShortcutsService._load_config()
|
|
# Trier par ordre
|
|
return sorted(config.shortcuts, key=lambda x: x.order)
|
|
|
|
@staticmethod
|
|
def get_shortcuts_by_category(category: str) -> List[ServiceShortcut]:
|
|
"""Récupère les raccourcis d'une catégorie"""
|
|
shortcuts = ShortcutsService.get_all_shortcuts()
|
|
return [s for s in shortcuts if s.category == category]
|
|
|
|
@staticmethod
|
|
def add_shortcut(shortcut: ServiceShortcut) -> ServiceShortcut:
|
|
"""Ajoute un nouveau raccourci"""
|
|
config = ShortcutsService._load_config()
|
|
|
|
# Générer un ID unique si nécessaire
|
|
if not shortcut.id:
|
|
shortcut.id = f"shortcut_{len(config.shortcuts) + 1}"
|
|
|
|
# S'assurer qu'il n'existe pas déjà
|
|
if any(s.id == shortcut.id for s in config.shortcuts):
|
|
raise Exception(f"Un raccourci avec l'ID '{shortcut.id}' existe déjà")
|
|
|
|
# Définir l'ordre
|
|
if shortcut.order == 0:
|
|
shortcut.order = len(config.shortcuts)
|
|
|
|
config.shortcuts.append(shortcut)
|
|
ShortcutsService._save_config(config)
|
|
|
|
return shortcut
|
|
|
|
@staticmethod
|
|
def update_shortcut(shortcut_id: str, shortcut: ServiceShortcut) -> ServiceShortcut:
|
|
"""Met à jour un raccourci existant"""
|
|
config = ShortcutsService._load_config()
|
|
|
|
for i, s in enumerate(config.shortcuts):
|
|
if s.id == shortcut_id:
|
|
shortcut.id = shortcut_id # Garder l'ID
|
|
config.shortcuts[i] = shortcut
|
|
ShortcutsService._save_config(config)
|
|
return shortcut
|
|
|
|
raise Exception(f"Raccourci avec l'ID '{shortcut_id}' non trouvé")
|
|
|
|
@staticmethod
|
|
def delete_shortcut(shortcut_id: str) -> dict:
|
|
"""Supprime un raccourci"""
|
|
config = ShortcutsService._load_config()
|
|
|
|
initial_count = len(config.shortcuts)
|
|
config.shortcuts = [s for s in config.shortcuts if s.id != shortcut_id]
|
|
|
|
if len(config.shortcuts) == initial_count:
|
|
raise Exception(f"Raccourci avec l'ID '{shortcut_id}' non trouvé")
|
|
|
|
# Réorganiser les ordres
|
|
for i, s in enumerate(config.shortcuts):
|
|
s.order = i
|
|
|
|
ShortcutsService._save_config(config)
|
|
|
|
return {"message": "Raccourci supprimé", "id": shortcut_id}
|
|
|
|
@staticmethod
|
|
def reorder_shortcuts(shortcut_ids: List[str]) -> List[ServiceShortcut]:
|
|
"""Réordonne les raccourcis"""
|
|
config = ShortcutsService._load_config()
|
|
|
|
# Créer un dict pour accès rapide
|
|
shortcuts_dict = {s.id: s for s in config.shortcuts}
|
|
|
|
# Réorganiser selon l'ordre donné
|
|
reordered = []
|
|
for i, shortcut_id in enumerate(shortcut_ids):
|
|
if shortcut_id in shortcuts_dict:
|
|
s = shortcuts_dict[shortcut_id]
|
|
s.order = i
|
|
reordered.append(s)
|
|
|
|
config.shortcuts = reordered
|
|
ShortcutsService._save_config(config)
|
|
|
|
return reordered
|
|
|
|
@staticmethod
|
|
def export_shortcuts() -> dict:
|
|
"""Exporte les raccourcis en JSON"""
|
|
config = ShortcutsService._load_config()
|
|
return config.model_dump()
|
|
|
|
@staticmethod
|
|
def import_shortcuts(shortcuts_data: List[dict]) -> List[ServiceShortcut]:
|
|
"""Importe des raccourcis depuis JSON"""
|
|
config = ShortcutsConfig()
|
|
|
|
for i, data in enumerate(shortcuts_data):
|
|
try:
|
|
shortcut = ServiceShortcut(**data)
|
|
shortcut.order = i
|
|
config.shortcuts.append(shortcut)
|
|
except Exception as e:
|
|
print(f"Erreur lors de l'import du raccourci {i}: {e}")
|
|
|
|
ShortcutsService._save_config(config)
|
|
return config.shortcuts
|