From 09335fc3d9c23ad8db83b2a6d20c9b63c599d757 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 26 Feb 2022 18:00:54 -0500 Subject: [PATCH] Add ability to reorder servers based on user --- app/classes/controllers/users_controller.py | 8 +++ app/classes/models/server_permissions.py | 5 +- app/classes/models/users.py | 11 ++++ app/classes/web/ajax_handler.py | 4 ++ app/classes/web/panel_handler.py | 23 ++++++++ app/frontend/templates/panel/dashboard.html | 52 ++++++++++++++++++- .../templates/panel/parts/details_stats.html | 19 ++++--- app/migrations/20220226_server_order.py | 16 ++++++ 8 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 app/migrations/20220226_server_order.py diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index 12bb4c2c..fc5b8492 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -30,6 +30,14 @@ class Users_Controller: def get_user_by_id(user_id): return users_helper.get_user(user_id) + @staticmethod + def update_server_order(user_id, user_server_order): + users_helper.update_server_order(user_id, user_server_order) + + @staticmethod + def get_server_order(user_id): + return users_helper.get_server_order(user_id) + @staticmethod def user_query(user_id): return users_helper.user_query(user_id) diff --git a/app/classes/models/server_permissions.py b/app/classes/models/server_permissions.py index b666deba..fcf966ef 100644 --- a/app/classes/models/server_permissions.py +++ b/app/classes/models/server_permissions.py @@ -173,7 +173,10 @@ class Permissions_Servers: else: roles_list = users_helper.get_user_roles_id(user.user_id) role_server = Role_Servers.select().where(Role_Servers.role_id.in_(roles_list)).where(Role_Servers.server_id == server_id).execute() - permissions_mask = role_server[0].permissions + try: + permissions_mask = role_server[0].permissions + except IndexError: + permissions_mask = '0' * len(server_permissions.get_permissions_list()) return permissions_mask @staticmethod diff --git a/app/classes/models/users.py b/app/classes/models/users.py index e154e885..2c4c7d06 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -1,3 +1,5 @@ +from email.policy import default +from http import server import sys import logging import datetime @@ -42,6 +44,7 @@ class Users(Model): lang = CharField(default="en_EN") support_logs = CharField(default = '') valid_tokens_from = DateTimeField(default=datetime.datetime.now) + server_order = CharField(default="") class Meta: table_name = "users" @@ -173,6 +176,14 @@ class helper_users: if up_data: Users.update(up_data).where(Users.user_id == user_id).execute() + @staticmethod + def update_server_order(user_id, user_server_order): + Users.update(server_order = user_server_order).where(Users.user_id == user_id).execute() + + @staticmethod + def get_server_order(user_id): + return Users.select().where(Users.user_id == user_id) + @staticmethod def get_super_user_list(): final_users = [] diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index bcf6060b..5d739c9a 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -258,6 +258,10 @@ class AjaxHandler(BaseHandler): # Create the directory os.mkdir(dir_path) + elif page == "send_order": + self.controller.users.update_server_order(exec_user['user_id'], bleach.clean(self.get_argument('order'))) + return + elif page == "unzip_file": if not permissions['Files'] in user_perms: if not superuser: diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index c20fdd91..e5b23549 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -307,6 +307,29 @@ class PanelHandler(BaseHandler): list(filter(lambda x: x['stats']['running'], page_data['servers']))) page_data['server_stats']['stopped'] = len(page_data['servers']) - page_data['server_stats']['running'] + #set user server order + user_order = self.controller.users.get_user_by_id(exec_user['user_id']) + user_order = user_order['server_order'].split(',') + page_servers = [] + server_ids = [] + + for server_id in user_order: + for server in page_data['servers']: + if str(server['server_data']['server_id']) == str(server_id): + page_servers.append(server) + + + for server in page_data['servers']: + server_ids.append(str(server['server_data']['server_id'])) + if server not in page_servers: + page_servers.append(server) + for server_id in user_order: + #remove IDs in list that user no longer has access to + if str(server_id) not in server_ids: + user_order.remove(server_id) + page_data['servers'] = page_servers + + for data in page_data['servers']: data['stats']['crashed'] = self.controller.servers.is_crashed( str(data['stats']['server_id']['server_id'])) diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index 2bdc2d43..7f15c1c4 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -123,7 +123,7 @@ {% for server in data['servers'] %} - + @@ -554,5 +554,55 @@ }); + {% end %} \ No newline at end of file diff --git a/app/frontend/templates/panel/parts/details_stats.html b/app/frontend/templates/panel/parts/details_stats.html index bab31ee6..c5f070e5 100644 --- a/app/frontend/templates/panel/parts/details_stats.html +++ b/app/frontend/templates/panel/parts/details_stats.html @@ -134,18 +134,21 @@ server_version = document.getElementById('version'); server_input_motd = document.getElementById('input_motd'); + if (server['stats']['updating']){ + server_status.setAttribute("class", "text-success"); + server_status.innerHTML = `{{ translate('serverTerm', 'updating', data['lang']) }}`; + } + /* TODO Update each element */ - if (server.running) - { - if (server.int_ping_results) - { - server_status.setAttribute("class", "text-success"); - server_status.innerHTML = `{{ translate('serverStats', 'online', data['lang']) }}`; + if (server.running){ + if (server['stats']['waiting_start']){ + server_status.setAttribute("class", "text-warning"); + server_status.innerHTML = `{{ translate('serverStats', 'starting', data['lang']) }}`; } else { - server_status.setAttribute("class", "text-warning"); - server_status.innerHTML = `{{ translate('serverStats', 'starting', data['lang']) }}`; + server_status.setAttribute("class", "text-success"); + server_status.innerHTML = `{{ translate('serverStats', 'online', data['lang']) }}`; } startedUTC = server.started; diff --git a/app/migrations/20220226_server_order.py b/app/migrations/20220226_server_order.py new file mode 100644 index 00000000..74351e55 --- /dev/null +++ b/app/migrations/20220226_server_order.py @@ -0,0 +1,16 @@ +# Generated by database migrator +import peewee + +def migrate(migrator, database, **kwargs): + migrator.add_columns('users', server_order=peewee.CharField(default='')) + """ + Write your migrations here. + """ + + + +def rollback(migrator, database, **kwargs): + migrator.drop_columns('users', ['server_order']) + """ + Write your rollback migrations here. + """