Added basic Import functions

This commit is contained in:
_iEpic 2022-03-18 23:59:10 -04:00
parent 4c14b58a2a
commit 4af62cbaca
3 changed files with 36 additions and 15 deletions

View File

@ -172,6 +172,7 @@ class helper_users:
Users.superuser: superuser, Users.superuser: superuser,
Users.created: helper.get_time_as_string() Users.created: helper.get_time_as_string()
}).execute() }).execute()
return user_id
@staticmethod @staticmethod
def update_user(user_id, up_data=None): def update_user(user_id, up_data=None):

View File

@ -59,8 +59,7 @@ class MainPrompt(cmd.Cmd):
else: else:
print(f'Name: {thread.name} Identifier: {thread.ident}') print(f'Name: {thread.name} Identifier: {thread.ident}')
@staticmethod def do_import3(self, _line):
def do_import3():
import3.start_import() import3.start_import()
def universal_exit(self): def universal_exit(self):
@ -83,4 +82,4 @@ class MainPrompt(cmd.Cmd):
@staticmethod @staticmethod
def help_import3(): def help_import3():
console.help("Import users and servers from Crafty 3") console.help("Import users and servers from Crafty 3")

View File

@ -1,33 +1,54 @@
import json import json
import os import os
import console
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from app.classes.controllers.users_controller import users_helper from app.classes.controllers.users_controller import users_helper
from app.classes.shared.main_controller import Controller from app.classes.shared.main_controller import Controller
from app.classes.shared.console import console
class import3: class import3:
def __init__(self):
self.controller = Controller()
def start_import(self): def start_import(self):
folder = os.path.normpath(input("Please input the path to the migrations folder in your installation of Crafty 3: ")) folder = os.path.normpath(input("Please input the path to the migrations folder in your installation of Crafty 3: "))
if not os.path.exists(folder): if not os.path.exists(folder):
console.log("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?") console.info("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?")
console.log("Please run the import3 command again and enter a valid path.") console.info("Please run the import3 command again and enter a valid path.")
else: else:
with open (os.path.join(folder, "users.json")) as f: with open (os.path.join(folder, "users.json")) as f:
user_json = json.load(f.read()) user_json = json.loads(f.read())
with open (os.path.join(folder, "mc_settings.json")) as f: with open (os.path.join(folder, "mc_settings.json")) as f:
servers_json = json.load(f.read()) servers_json = json.loads(f.read())
self.import_users(user_json) self.import_users(user_json)
self.import_servers(servers_json) self.import_servers(servers_json, self.controller)
@staticmethod @staticmethod
def import_users(json_data): def import_users(json_data):
for user in json_data: # If there is only one user to import json needs to call the data differently
users_helper.add_rawpass_user(user.username, user.password) if isinstance(json_data, list):
console.log(f"Imported user {user.username} from Crafty 3") for user in json_data:
logger.info(f"Imported user {user.username} from Crafty 3") users_helper.add_rawpass_user(user['username'], user['password'])
console.info(f"Imported user {user['username']} from Crafty 3")
logger.info(f"Imported user {user['username']} from Crafty 3")
else:
console.info("There is only one user detected. Cannot create duplicate Admin account.")
logger.info("There is only one user detected. Cannot create duplicate Admin account.")
@staticmethod @staticmethod
def import_servers(json_data): def import_servers(json_data, controller):
return # If there is only one server to import json needs to call the data differently
if isinstance(json_data, list):
for server in json_data:
new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], min_mem=int(server['memory_min']), max_mem=int(server['memory_max']), port=server['server_port'])
console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}")
logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}")
else:
new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], min_mem=int(json_data['memory_min']), max_mem=int(json_data['memory_max']), port=json_data['server_port'])
console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}")
logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}")
import3 = import3()