150 Zeilen
5.2 KiB
Python
150 Zeilen
5.2 KiB
Python
"""
|
|
Controller für die Verwaltung von Accounts.
|
|
"""
|
|
|
|
import logging
|
|
import csv
|
|
from datetime import datetime
|
|
from PyQt5.QtWidgets import QFileDialog, QMessageBox
|
|
from PyQt5.QtCore import QObject
|
|
|
|
logger = logging.getLogger("account_controller")
|
|
|
|
class AccountController(QObject):
|
|
"""Controller für die Verwaltung von Accounts."""
|
|
|
|
def __init__(self, db_manager):
|
|
super().__init__()
|
|
self.db_manager = db_manager
|
|
self.parent_view = None
|
|
|
|
def set_parent_view(self, view):
|
|
"""Setzt die übergeordnete View für Dialoge."""
|
|
self.parent_view = view
|
|
|
|
def on_account_created(self, platform: str, account_data: dict):
|
|
"""Wird aufgerufen, wenn ein Account erstellt wurde."""
|
|
account = {
|
|
"platform": platform.lower(),
|
|
"username": account_data.get("username", ""),
|
|
"password": account_data.get("password", ""),
|
|
"email": account_data.get("email", ""),
|
|
"phone": account_data.get("phone", ""),
|
|
"full_name": account_data.get("full_name", ""),
|
|
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
}
|
|
|
|
self.db_manager.add_account(account)
|
|
logger.info(f"Account in Datenbank gespeichert: {account['username']}")
|
|
|
|
# Erfolgsmeldung anzeigen
|
|
if self.parent_view:
|
|
QMessageBox.information(
|
|
self.parent_view,
|
|
"Erfolg",
|
|
f"Account erfolgreich erstellt!\n\nBenutzername: {account['username']}\nPasswort: {account['password']}\nE-Mail/Telefon: {account['email'] or account['phone']}"
|
|
)
|
|
|
|
def load_accounts(self, platform=None):
|
|
"""Lädt Accounts aus der Datenbank."""
|
|
try:
|
|
if platform and hasattr(self.db_manager, "get_accounts_by_platform"):
|
|
accounts = self.db_manager.get_accounts_by_platform(platform.lower())
|
|
else:
|
|
accounts = self.db_manager.get_all_accounts()
|
|
if platform:
|
|
accounts = [acc for acc in accounts if acc.get("platform", "").lower() == platform.lower()]
|
|
|
|
return accounts
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Laden der Accounts: {e}")
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Fehler beim Laden der Accounts:\n{str(e)}"
|
|
)
|
|
return []
|
|
|
|
def export_accounts(self, platform=None):
|
|
"""Exportiert Accounts in eine CSV-Datei."""
|
|
parent = self.parent_view or None
|
|
|
|
file_path, _ = QFileDialog.getSaveFileName(
|
|
parent,
|
|
"Konten exportieren",
|
|
"",
|
|
"CSV-Dateien (*.csv);;Alle Dateien (*)"
|
|
)
|
|
|
|
if not file_path:
|
|
return
|
|
|
|
try:
|
|
# Accounts laden
|
|
accounts = self.load_accounts(platform)
|
|
|
|
with open(file_path, "w", newline="", encoding="utf-8") as f:
|
|
writer = csv.writer(f)
|
|
|
|
# Header
|
|
writer.writerow([
|
|
"ID", "Plattform", "Benutzername", "Passwort",
|
|
"E-Mail", "Telefon", "Name", "Erstellt am"
|
|
])
|
|
|
|
# Daten
|
|
for account in accounts:
|
|
writer.writerow([
|
|
account.get("id", ""),
|
|
account.get("platform", ""),
|
|
account.get("username", ""),
|
|
account.get("password", ""),
|
|
account.get("email", ""),
|
|
account.get("phone", ""),
|
|
account.get("full_name", ""),
|
|
account.get("created_at", "")
|
|
])
|
|
|
|
logger.info(f"Accounts erfolgreich nach {file_path} exportiert")
|
|
|
|
if parent:
|
|
QMessageBox.information(
|
|
parent,
|
|
"Export erfolgreich",
|
|
f"Konten wurden erfolgreich nach {file_path} exportiert."
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Exportieren der Accounts: {e}")
|
|
if parent:
|
|
QMessageBox.critical(
|
|
parent,
|
|
"Export fehlgeschlagen",
|
|
f"Fehler beim Exportieren der Konten:\n{str(e)}"
|
|
)
|
|
|
|
def delete_account(self, account_id):
|
|
"""Löscht einen Account aus der Datenbank."""
|
|
try:
|
|
success = self.db_manager.delete_account(account_id)
|
|
|
|
if not success:
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Konto mit ID {account_id} konnte nicht gelöscht werden."
|
|
)
|
|
|
|
return success
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Löschen des Accounts: {e}")
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Fehler beim Löschen des Kontos:\n{str(e)}"
|
|
)
|
|
return False
|