From 87229a654e95b09b652f8f7c216e6d3704f85514 Mon Sep 17 00:00:00 2001 From: Nick Leeman Date: Fri, 7 Oct 2022 22:53:23 +0200 Subject: [PATCH] added backup system! --- models/database.py | 8 ++++++++ services/search.py | 1 - services/utils.py | 16 +++++++++++++++- views/backup_menu.py | 42 ++++++++++++++++++++++++++++++++++++++++++ views/main_menu.py | 17 +++++++++++++++++ 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 views/backup_menu.py diff --git a/models/database.py b/models/database.py index 6b02654..c0e0dcb 100644 --- a/models/database.py +++ b/models/database.py @@ -15,6 +15,14 @@ class Database: Database._init_log_table() Database._init_user_table() Database._init_city_table() + + @staticmethod + def delete_tables(): + cursor = Database.connection.cursor() + cursor.execute("DROP TABLE logs") + cursor.execute("DROP TABLE users") + cursor.execute("DROP TABLE cities") + cursor.close() @staticmethod def _init_log_table(): diff --git a/services/search.py b/services/search.py index 22e19a1..e37bec4 100644 --- a/services/search.py +++ b/services/search.py @@ -77,7 +77,6 @@ class Search: payload = [] for row in rows: - print(row) city = City(Database.connection)._set_row_values(row) payload.append(city) diff --git a/services/utils.py b/services/utils.py index d9c1e17..84d8dd5 100644 --- a/services/utils.py +++ b/services/utils.py @@ -1,7 +1,21 @@ import os +from models.database import Database + class Utils: @staticmethod def clear_screen(): - os.system('cls' if os.name == 'nt' else 'clear') \ No newline at end of file + os.system('cls' if os.name == 'nt' else 'clear') + + @staticmethod + def export_db(file): + with open(f"./{file}", 'w') as f: + for line in Database.connection.iterdump(): + f.write('%s\n' % line) + + @staticmethod + def import_db(file): + with open(f"./{file}", 'r') as f: + str = f.read() + Database.connection.executescript(str) \ No newline at end of file diff --git a/views/backup_menu.py b/views/backup_menu.py new file mode 100644 index 0000000..19d4ae1 --- /dev/null +++ b/views/backup_menu.py @@ -0,0 +1,42 @@ + +from os import path +from models.database import Database +from models.user import User +from services.search import Search +from services.utils import Utils +from ui.input_menu import InputMenu, Validator +from ui.selection_menu import SelectionMenu + +class BackupMenu: + @staticmethod + def create_backup(): + input_menu = InputMenu("Export Backup File").add_option("FILE", "Filename", "STR", "backup.sql", 1, 50, None).do_input() + backup_name = input_menu.get_value("FILE") + print(f"Exporting database to file: {backup_name}") + Utils.export_db(backup_name) + input(f"Exported database. Press any key to return.") + + @staticmethod + def import_backup(): + menu = SelectionMenu("Import Backup? This will delete current database!") + menu.add_option("Yes", True) + menu.add_option("No", False) + option = menu.display().input_option() + + if option == False: + return + + input_menu = InputMenu("Import Backup File").add_option("FILE", "Filename", "STR", "backup.sql", 1, 50, None).do_input() + backup_name = input_menu.get_value("FILE") + + if not path.exists(f"./{backup_name}"): + input(f"Backup file not found. Press any key to return.") + return + + print("Cleared current database.") + Database.delete_tables() + + print(f"Importing database from file: {backup_name}") + Utils.import_db(backup_name) + input(f"Imported database. Press any key to return.") + \ No newline at end of file diff --git a/views/main_menu.py b/views/main_menu.py index 75ba6c6..2efa6d5 100644 --- a/views/main_menu.py +++ b/views/main_menu.py @@ -3,6 +3,7 @@ from services.state import State from services.utils import Utils from ui.selection_menu import SelectionMenu from views.advisor_menu import AdvisorMenu +from views.backup_menu import BackupMenu from views.member_menu import MemberMenu from views.system_admin_menu import SystemAdminMenu @@ -24,6 +25,7 @@ class MainMenu: if State.CURRENT_USER.role == "SUPER_ADMIN": main_menu.add_option("Manage Admins", MainMenu.manage_admins) + main_menu.add_option("Manage Backups", MainMenu.manage_backups) main_menu.add_option("Exit...", None) @@ -84,4 +86,19 @@ class MainMenu: selected_option() + @staticmethod + def manage_backups(): + Utils.clear_screen() + + menu = SelectionMenu(f"Manage Backups") + menu.add_option("Create Backup", BackupMenu.create_backup) + menu.add_option("Import Backup", BackupMenu.import_backup) + menu.add_option("Back to main menu", None) + + selected_option = menu.display().input_option() + if selected_option == None: + return + + selected_option() +