added backup system!
This commit is contained in:
parent
879ee79a90
commit
87229a654e
@ -16,6 +16,14 @@ class Database:
|
||||
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():
|
||||
cursor = Database.connection.cursor()
|
||||
|
@ -77,7 +77,6 @@ class Search:
|
||||
|
||||
payload = []
|
||||
for row in rows:
|
||||
print(row)
|
||||
city = City(Database.connection)._set_row_values(row)
|
||||
payload.append(city)
|
||||
|
||||
|
@ -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')
|
||||
|
||||
@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)
|
42
views/backup_menu.py
Normal file
42
views/backup_menu.py
Normal file
@ -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.")
|
||||
|
@ -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()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user