148 lines
6.4 KiB
Python
148 lines
6.4 KiB
Python
import customtkinter as ctk
|
|
from tkinter import messagebox, filedialog
|
|
import os
|
|
import sys
|
|
|
|
# Add the project root to the path
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
|
|
|
from src.database import DatabaseManager
|
|
|
|
|
|
class DatabaseManagementDialog:
|
|
def __init__(self, parent, db_manager):
|
|
self.db_manager = db_manager
|
|
|
|
# Create the dialog window
|
|
self.dialog = ctk.CTkToplevel(parent)
|
|
self.dialog.title("Database Management")
|
|
self.dialog.geometry("500x400")
|
|
self.dialog.transient(parent)
|
|
self.dialog.grab_set()
|
|
|
|
# Center the dialog
|
|
self.dialog.update_idletasks()
|
|
width = self.dialog.winfo_width()
|
|
height = self.dialog.winfo_height()
|
|
x = (self.dialog.winfo_screenwidth() // 2) - (width // 2)
|
|
y = (self.dialog.winfo_screenheight() // 2) - (height // 2)
|
|
self.dialog.geometry(f'{width}x{height}+{x}+{y}')
|
|
|
|
# Create UI elements
|
|
self.create_widgets()
|
|
|
|
def create_widgets(self):
|
|
"""Create the UI elements for the dialog"""
|
|
# Title
|
|
title_label = ctk.CTkLabel(self.dialog, text="Database Management",
|
|
font=("Arial", 20, "bold"))
|
|
title_label.pack(pady=20)
|
|
|
|
# Main frame
|
|
main_frame = ctk.CTkFrame(self.dialog)
|
|
main_frame.pack(fill="both", expand=True, padx=20, pady=10)
|
|
|
|
# Database info
|
|
info_frame = ctk.CTkFrame(main_frame)
|
|
info_frame.pack(fill="x", padx=10, pady=10)
|
|
|
|
ctk.CTkLabel(info_frame, text="Database Information",
|
|
font=("Arial", 14, "bold")).pack(anchor="w", padx=10, pady=5)
|
|
|
|
ctk.CTkLabel(info_frame, text=f"Database Type: {self.db_manager.db_type}").pack(anchor="w", padx=20, pady=2)
|
|
ctk.CTkLabel(info_frame, text=f"Database Name: {self.db_manager.db_name}").pack(anchor="w", padx=20, pady=2)
|
|
|
|
# Actions frame
|
|
actions_frame = ctk.CTkFrame(main_frame)
|
|
actions_frame.pack(fill="x", padx=10, pady=10)
|
|
|
|
ctk.CTkLabel(actions_frame, text="Database Actions",
|
|
font=("Arial", 14, "bold")).pack(anchor="w", padx=10, pady=5)
|
|
|
|
# Buttons
|
|
button_frame = ctk.CTkFrame(actions_frame, fg_color="transparent")
|
|
button_frame.pack(fill="x", padx=20, pady=10)
|
|
|
|
ctk.CTkButton(button_frame, text="Backup Database",
|
|
command=self.backup_database, width=150, height=32).pack(pady=5)
|
|
|
|
ctk.CTkButton(button_frame, text="Restore Database",
|
|
command=self.restore_database, width=150, height=32).pack(pady=5)
|
|
|
|
ctk.CTkButton(button_frame, text="Initialize Database",
|
|
command=self.initialize_database, width=150, height=32).pack(pady=5)
|
|
|
|
ctk.CTkButton(button_frame, text="Check Database Status",
|
|
command=self.check_database_status, width=150, height=32).pack(pady=5)
|
|
|
|
# Close button
|
|
close_button = ctk.CTkButton(self.dialog, text="Close",
|
|
command=self.dialog.destroy, width=100, height=32)
|
|
close_button.pack(pady=20)
|
|
|
|
def backup_database(self):
|
|
"""Backup the database"""
|
|
try:
|
|
# Ask user for backup location
|
|
backup_path = filedialog.asksaveasfilename(
|
|
defaultextension=".db",
|
|
filetypes=[("Database files", "*.db"), ("All files", "*.*")],
|
|
title="Save Database Backup As"
|
|
)
|
|
|
|
if backup_path:
|
|
result = self.db_manager.backup_database(backup_path)
|
|
if result:
|
|
messagebox.showinfo("Success", f"Database backed up successfully to {result}")
|
|
else:
|
|
messagebox.showerror("Error", "Failed to backup database")
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"Failed to backup database: {str(e)}")
|
|
|
|
def restore_database(self):
|
|
"""Restore the database from a backup"""
|
|
try:
|
|
# Ask user for backup file to restore
|
|
backup_path = filedialog.askopenfilename(
|
|
filetypes=[("Database files", "*.db"), ("All files", "*.*")],
|
|
title="Select Database Backup File"
|
|
)
|
|
|
|
if backup_path:
|
|
# Confirm with user before restoring
|
|
if messagebox.askyesno("Confirm Restore",
|
|
"Are you sure you want to restore the database? "
|
|
"This will overwrite the current database."):
|
|
result = self.db_manager.restore_database(backup_path)
|
|
if result:
|
|
messagebox.showinfo("Success", "Database restored successfully")
|
|
else:
|
|
messagebox.showerror("Error", "Failed to restore database")
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"Failed to restore database: {str(e)}")
|
|
|
|
def initialize_database(self):
|
|
"""Initialize the database"""
|
|
try:
|
|
# Confirm with user before initializing
|
|
if messagebox.askyesno("Confirm Initialization",
|
|
"Are you sure you want to initialize the database? "
|
|
"This will create all necessary tables."):
|
|
result = self.db_manager.initialize_database()
|
|
if result:
|
|
messagebox.showinfo("Success", "Database initialized successfully")
|
|
else:
|
|
messagebox.showerror("Error", "Failed to initialize database")
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"Failed to initialize database: {str(e)}")
|
|
|
|
def check_database_status(self):
|
|
"""Check the database status"""
|
|
try:
|
|
is_initialized = self.db_manager.is_database_initialized()
|
|
if is_initialized:
|
|
messagebox.showinfo("Database Status", "Database is properly initialized")
|
|
else:
|
|
messagebox.showwarning("Database Status", "Database is not initialized")
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"Failed to check database status: {str(e)}") |