Add ability to reorder servers based on user

This commit is contained in:
Andrew 2022-02-26 18:00:54 -05:00
parent 1ab4ffd8f7
commit 09335fc3d9
8 changed files with 128 additions and 10 deletions

View File

@ -30,6 +30,14 @@ class Users_Controller:
def get_user_by_id(user_id): def get_user_by_id(user_id):
return users_helper.get_user(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 @staticmethod
def user_query(user_id): def user_query(user_id):
return users_helper.user_query(user_id) return users_helper.user_query(user_id)

View File

@ -173,7 +173,10 @@ class Permissions_Servers:
else: else:
roles_list = users_helper.get_user_roles_id(user.user_id) 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() 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 return permissions_mask
@staticmethod @staticmethod

View File

@ -1,3 +1,5 @@
from email.policy import default
from http import server
import sys import sys
import logging import logging
import datetime import datetime
@ -42,6 +44,7 @@ class Users(Model):
lang = CharField(default="en_EN") lang = CharField(default="en_EN")
support_logs = CharField(default = '') support_logs = CharField(default = '')
valid_tokens_from = DateTimeField(default=datetime.datetime.now) valid_tokens_from = DateTimeField(default=datetime.datetime.now)
server_order = CharField(default="")
class Meta: class Meta:
table_name = "users" table_name = "users"
@ -173,6 +176,14 @@ class helper_users:
if up_data: if up_data:
Users.update(up_data).where(Users.user_id == user_id).execute() 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 @staticmethod
def get_super_user_list(): def get_super_user_list():
final_users = [] final_users = []

View File

@ -258,6 +258,10 @@ class AjaxHandler(BaseHandler):
# Create the directory # Create the directory
os.mkdir(dir_path) 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": elif page == "unzip_file":
if not permissions['Files'] in user_perms: if not permissions['Files'] in user_perms:
if not superuser: if not superuser:

View File

@ -307,6 +307,29 @@ class PanelHandler(BaseHandler):
list(filter(lambda x: x['stats']['running'], page_data['servers']))) list(filter(lambda x: x['stats']['running'], page_data['servers'])))
page_data['server_stats']['stopped'] = len(page_data['servers']) - page_data['server_stats']['running'] 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']: for data in page_data['servers']:
data['stats']['crashed'] = self.controller.servers.is_crashed( data['stats']['crashed'] = self.controller.servers.is_crashed(
str(data['stats']['server_id']['server_id'])) str(data['stats']['server_id']['server_id']))

View File

@ -123,7 +123,7 @@
</thead> </thead>
<tbody> <tbody>
{% for server in data['servers'] %} {% for server in data['servers'] %}
<tr> <tr id="{{server['server_data']['server_id']}}" draggable="true" ondragstart="start()" ondragover="dragover()" ondragend="dragend()">
<td> <td>
<i class="fas fa-server"></i> <i class="fas fa-server"></i>
<a href="/panel/server_detail?id={{server['server_data']['server_id']}}"> <a href="/panel/server_detail?id={{server['server_data']['server_id']}}">
@ -554,5 +554,55 @@
}); });
</script> </script>
<script>
var row;
function start(){
row = event.target;
}
function dragover(){
var e = event;
e.preventDefault();
let children= Array.from(e.target.parentNode.parentNode.children);
if(children.indexOf(e.target.parentNode)>children.indexOf(row))
e.target.parentNode.after(row);
else
e.target.parentNode.before(row);
}
function dragend(){
var id_string = '';
const table = document.querySelector("table");
for (const row of table.rows) {
if (row.getAttribute('id') != null){
if (id_string != ''){
id_string += ',' + String(row.getAttribute('id'));
}else{
id_string +=String(row.getAttribute('id'));
}
}
}
console.log(id_string)
sendOrder(id_string)
}
function sendOrder(id_string) {
var token = getCookie("_xsrf")
$.ajax({
type: "POST",
headers: {'X-XSRFToken': token},
url: '/ajax/send_order?order='+id_string,
data: {
order: id_string,
},
success: function(data){
console.log("got response:");
console.log(data);
},
});
}
</script>
{% end %} {% end %}

View File

@ -134,18 +134,21 @@
server_version = document.getElementById('version'); server_version = document.getElementById('version');
server_input_motd = document.getElementById('input_motd'); 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 */ /* TODO Update each element */
if (server.running) if (server.running){
{ if (server['stats']['waiting_start']){
if (server.int_ping_results) 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']) }}`;
} }
else else
{ {
server_status.setAttribute("class", "text-warning"); server_status.setAttribute("class", "text-success");
server_status.innerHTML = `{{ translate('serverStats', 'starting', data['lang']) }}`; server_status.innerHTML = `{{ translate('serverStats', 'online', data['lang']) }}`;
} }
startedUTC = server.started; startedUTC = server.started;

View File

@ -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.
"""