JSON statt txt
Dieser Commit ist enthalten in:
@ -1,11 +1,12 @@
|
||||
"""
|
||||
Profil-Export-Service für Account-Daten
|
||||
|
||||
Exportiert Account-Profile in verschiedene Formate (CSV, TXT, PDF).
|
||||
Exportiert Account-Profile in verschiedene Formate (CSV, JSON, PDF).
|
||||
"""
|
||||
|
||||
import os
|
||||
import csv
|
||||
import json
|
||||
import string
|
||||
import logging
|
||||
from io import BytesIO, StringIO
|
||||
@ -106,6 +107,50 @@ class ProfileExportService:
|
||||
logger.error(f"Fehler beim TXT-Export: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def export_to_json(account_data: Dict[str, Any]) -> bytes:
|
||||
"""
|
||||
Exportiert Account-Daten als JSON.
|
||||
|
||||
Args:
|
||||
account_data: Dictionary mit Account-Daten
|
||||
|
||||
Returns:
|
||||
JSON-Daten als bytes
|
||||
"""
|
||||
try:
|
||||
# JSON-Struktur aufbauen
|
||||
export_data = {
|
||||
"account": {
|
||||
field_key: account_data.get(field_key, "")
|
||||
for field_key in ProfileExportService.EXPORT_FIELDS.keys()
|
||||
},
|
||||
"export_info": {
|
||||
"exported_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"format_version": "1.0"
|
||||
}
|
||||
}
|
||||
|
||||
# None-Werte zu leeren Strings konvertieren
|
||||
for key, value in export_data["account"].items():
|
||||
if value is None:
|
||||
export_data["account"][key] = ""
|
||||
|
||||
# JSON mit Einrückung für Lesbarkeit
|
||||
json_content = json.dumps(
|
||||
export_data,
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
sort_keys=False
|
||||
)
|
||||
|
||||
logger.info("JSON-Export erfolgreich")
|
||||
return json_content.encode('utf-8')
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Fehler beim JSON-Export: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def export_to_pdf(account_data: Dict[str, Any]) -> bytes:
|
||||
"""
|
||||
@ -349,7 +394,7 @@ class ProfileExportService:
|
||||
|
||||
Args:
|
||||
account_data: Account-Daten zum Exportieren
|
||||
formats: Liste von Formaten ["csv", "txt", "pdf"]
|
||||
formats: Liste von Formaten ["csv", "json", "pdf"]
|
||||
password_protect: Wird ignoriert (für Rückwärtskompatibilität)
|
||||
|
||||
Returns:
|
||||
@ -366,9 +411,9 @@ class ProfileExportService:
|
||||
filename = ProfileExportService.generate_filename(account_data, "csv")
|
||||
files_dict[filename] = content
|
||||
|
||||
elif fmt == "txt":
|
||||
content = ProfileExportService.export_to_txt(account_data)
|
||||
filename = ProfileExportService.generate_filename(account_data, "txt")
|
||||
elif fmt == "json":
|
||||
content = ProfileExportService.export_to_json(account_data)
|
||||
filename = ProfileExportService.generate_filename(account_data, "json")
|
||||
files_dict[filename] = content
|
||||
|
||||
elif fmt == "pdf":
|
||||
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren