From c02472fb1647dba63b3b9c365f590c63f952c89b Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 4 Mar 2023 20:10:10 -0500 Subject: [PATCH] Send schdules via API --- app/classes/shared/tasks.py | 1 - app/classes/web/panel_handler.py | 330 ------------------ .../api/servers/server/tasks/task/index.py | 2 + .../templates/panel/server_schedule_edit.html | 120 +++++-- .../templates/panel/server_schedules.html | 2 +- 5 files changed, 101 insertions(+), 354 deletions(-) diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 5848e005..45c1d088 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -450,7 +450,6 @@ class TasksManager: # created task a child of itself. if str(job_data.get("parent")) == str(sch_id): job_data["parent"] = None - HelpersManagement.update_scheduled_task(sch_id, job_data) if not ( diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index fe7b8946..9adec950 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -1652,336 +1652,6 @@ class PanelHandler(BaseHandler): self.redirect("/panel/config_json") - if page == "new_schedule": - server_id = self.check_server_id() - if not server_id: - return - - if ( - not permissions["Schedule"] - in self.controller.server_perms.get_user_id_permissions_list( - exec_user["user_id"], server_id - ) - and not superuser - ): - self.redirect( - "/panel/error?error=Unauthorized access: User not authorized" - ) - return - - difficulty = bleach.clean(self.get_argument("difficulty", None)) - server_obj = self.controller.servers.get_server_obj(server_id) - enabled = bleach.clean(self.get_argument("enabled", "0")) - name = bleach.clean(self.get_argument("name", "")) - if difficulty == "basic": - action = bleach.clean(self.get_argument("action", None)) - interval = bleach.clean(self.get_argument("interval", None)) - interval_type = bleach.clean(self.get_argument("interval_type", None)) - # only check for time if it's number of days - if interval_type == "days": - sch_time = bleach.clean(self.get_argument("time", None)) - if int(interval) > 30: - self.redirect( - "/panel/error?error=Invalid argument." - " Days must be 30 or fewer." - ) - return - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - - elif difficulty == "reaction": - interval_type = "reaction" - action = bleach.clean(self.get_argument("action", None)) - delay = bleach.clean(self.get_argument("delay", None)) - parent = bleach.clean(self.get_argument("parent", None)) - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - - else: - interval_type = "" - cron_string = bleach.clean(self.get_argument("cron", "")) - if not croniter.is_valid(cron_string): - self.redirect( - "/panel/error?error=INVALID FORMAT: Invalid Cron Format." - ) - return - action = bleach.clean(self.get_argument("action", None)) - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - if bleach.clean(self.get_argument("enabled", "0")) == "1": - enabled = True - else: - enabled = False - if bleach.clean(self.get_argument("one_time", "0")) == "1": - one_time = True - else: - one_time = False - - if interval_type == "days": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": interval, - "command": command, - "start_time": sch_time, - "enabled": enabled, - "one_time": one_time, - "cron_string": "", - "parent": None, - "delay": 0, - } - elif difficulty == "reaction": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": "", - # We'll base every interval off of a midnight start time. - "start_time": "", - "command": command, - "cron_string": "", - "enabled": enabled, - "one_time": one_time, - "parent": parent, - "delay": delay, - } - elif difficulty == "advanced": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": "", - "interval": "", - # We'll base every interval off of a midnight start time. - "start_time": "", - "command": command, - "cron_string": cron_string, - "enabled": enabled, - "one_time": one_time, - "parent": None, - "delay": 0, - } - else: - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": interval, - "command": command, - "enabled": enabled, - # We'll base every interval off of a midnight start time. - "start_time": "00:00", - "one_time": one_time, - "cron_string": "", - "parent": None, - "delay": 0, - } - - self.tasks_manager.schedule_job(job_data) - - self.controller.management.add_to_audit_log( - exec_user["user_id"], - f"Edited server {server_id}: added scheduled job", - server_id, - self.get_remote_ip(), - ) - self.tasks_manager.reload_schedule_from_db() - self.redirect(f"/panel/server_detail?id={server_id}&subpage=schedules") - - if page == "edit_schedule": - server_id = self.check_server_id() - if not server_id: - return - - if ( - not permissions["Schedule"] - in self.controller.server_perms.get_user_id_permissions_list( - exec_user["user_id"], server_id - ) - and not superuser - ): - self.redirect( - "/panel/error?error=Unauthorized access: User not authorized" - ) - return - - sch_id = self.get_argument("sch_id", None) - if sch_id is None: - self.redirect("/panel/error?error=Invalid Schedule ID") - - difficulty = bleach.clean(self.get_argument("difficulty", None)) - server_obj = self.controller.servers.get_server_obj(server_id) - enabled = bleach.clean(self.get_argument("enabled", "0")) - name = bleach.clean(self.get_argument("name", "")) - if difficulty == "basic": - action = bleach.clean(self.get_argument("action", None)) - interval = bleach.clean(self.get_argument("interval", None)) - interval_type = bleach.clean(self.get_argument("interval_type", None)) - # only check for time if it's number of days - if interval_type == "days": - sch_time = bleach.clean(self.get_argument("time", None)) - if int(interval) > 30: - self.redirect( - "/panel/error?error=Invalid argument." - " Days must be 30 or fewer." - ) - return - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - elif difficulty == "reaction": - interval_type = "reaction" - action = bleach.clean(self.get_argument("action", None)) - delay = bleach.clean(self.get_argument("delay", None)) - parent = bleach.clean(self.get_argument("parent", None)) - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - parent = bleach.clean(self.get_argument("parent", None)) - else: - interval_type = "" - cron_string = bleach.clean(self.get_argument("cron", "")) - if not croniter.is_valid(cron_string): - self.redirect( - "/panel/error?error=INVALID FORMAT: Invalid Cron Format." - ) - return - action = bleach.clean(self.get_argument("action", None)) - if action == "command": - command = self.get_argument("command", None) - elif action == "start": - command = "start_server" - elif action == "stop": - command = "stop_server" - elif action == "restart": - command = "restart_server" - elif action == "backup": - command = "backup_server" - if bleach.clean(self.get_argument("enabled", "0")) == "1": - enabled = True - else: - enabled = False - if bleach.clean(self.get_argument("one_time", "0")) == "1": - one_time = True - else: - one_time = False - - if interval_type == "days": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": interval, - "command": command, - "start_time": sch_time, - "enabled": enabled, - "one_time": one_time, - "cron_string": "", - "parent": None, - "delay": 0, - } - elif difficulty == "advanced": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": "", - "interval": "", - # We'll base every interval off of a midnight start time. - "start_time": "", - "command": command, - "cron_string": cron_string, - "delay": "", - "parent": "", - "enabled": enabled, - "one_time": one_time, - } - elif difficulty == "reaction": - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": "", - # We'll base every interval off of a midnight start time. - "start_time": "", - "command": command, - "cron_string": "", - "enabled": enabled, - "one_time": one_time, - "parent": parent, - "delay": delay, - } - else: - job_data = { - "name": name, - "server_id": server_id, - "action": action, - "interval_type": interval_type, - "interval": interval, - "command": command, - "enabled": enabled, - # We'll base every interval off of a midnight start time. - "start_time": "00:00", - "delay": "", - "parent": "", - "one_time": one_time, - "cron_string": "", - } - self.tasks_manager.update_job(sch_id, job_data) - - self.controller.management.add_to_audit_log( - exec_user["user_id"], - f"Edited server {server_id}: updated schedule", - server_id, - self.get_remote_ip(), - ) - self.tasks_manager.reload_schedule_from_db() - self.redirect(f"/panel/server_detail?id={server_id}&subpage=schedules") - elif page == "edit_user": if bleach.clean(self.get_argument("username", None)).lower() == "system": self.redirect( diff --git a/app/classes/web/routes/api/servers/server/tasks/task/index.py b/app/classes/web/routes/api/servers/server/tasks/task/index.py index b046dc17..a9a1f5d9 100644 --- a/app/classes/web/routes/api/servers/server/tasks/task/index.py +++ b/app/classes/web/routes/api/servers/server/tasks/task/index.py @@ -35,6 +35,7 @@ task_patch_schema = { "", ], }, + "name": {"type": "string"}, "start_time": {"type": "string", "pattern": r"\d{1,2}:\d{1,2}"}, "command": {"type": ["string", "null"]}, "one_time": {"type": "boolean", "default": False}, @@ -133,6 +134,7 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler): if str(data.get("parent")) == str(task_id) and data.get("parent") is not None: data["parent"] = None + data["server_id"] = server_id self.tasks_manager.update_job(task_id, data) self.controller.management.add_to_audit_log( diff --git a/app/frontend/templates/panel/server_schedule_edit.html b/app/frontend/templates/panel/server_schedule_edit.html index 298e4cc7..9e535d67 100644 --- a/app/frontend/templates/panel/server_schedule_edit.html +++ b/app/frontend/templates/panel/server_schedule_edit.html @@ -37,15 +37,12 @@
{% if data['new_schedule'] == True %} -
{% else %} - {% end %} - {% raw xsrf_form_html() %} - -
@@ -89,7 +86,7 @@ class="text-muted ml-1"> - {{ translate('serverScheduleConfig', 'interval-explain' , data['lang']) }} + value="{{ data['schedule']['interval'] }}" placeholder="Interval" required min="1">

@@ -127,7 +124,7 @@ -
@@ -234,8 +231,99 @@ return r ? r[1] : undefined; } + function replacer(key, value) { + if (key != "start_time" && key != "cron_string" && key != "interval_type") { + if (typeof value == "boolean") { + return value + } else { + return (isNaN(value) ? value : +value); + } + } else { + if (value === "" && key == "start_time"){ + return "00:00"; + }else{ + return value; + } + } + } + + const serverId = new URLSearchParams(document.location.search).get('id'); + const schId = new URLSearchParams(document.location.search).get('sch_id'); $(document).ready(function () { console.log("ready!"); + $("#new_schedule_form").on("submit", async function (e) { + e.preventDefault(); + var token = getCookie("_xsrf") + let schForm = document.getElementById("new_schedule_form"); + + let formData = new FormData(schForm); + formData.delete("difficulty"); + //Create an object from the form data entries + let formDataObject = Object.fromEntries(formData.entries()); + //We need to make sure these are sent regardless of whether or not they're checked + formDataObject.enabled = $("#enabled").prop('checked'); + formDataObject.one_time = $("#one_time").prop('checked'); + if ($("#difficulty").val() == "reaction"){ + formDataObject.interval_type = "reaction"; + } + if (formDataObject.cron_string != ""){ + formDataObject.interval_type = ''; + } + console.log(formDataObject); + // Format the plain form data as JSON + let formDataJsonString = JSON.stringify(formDataObject, replacer); + + console.log(formDataJsonString); + + let res = await fetch(`/api/v2/servers/${serverId}/tasks/`, { + method: 'POST', + headers: { + 'X-XSRFToken': token + }, + body: formDataJsonString, + }); + let responseData = await res; + if (responseData.statusText === "OK") { + window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`; + } + }); + + $("#schedule_form").on("submit", async function (e) { + e.preventDefault(); + var token = getCookie("_xsrf") + let schForm = document.getElementById("schedule_form"); + + let formData = new FormData(schForm); + formData.delete("difficulty"); + //Create an object from the form data entries + let formDataObject = Object.fromEntries(formData.entries()); + //We need to make sure these are sent regardless of whether or not they're checked + formDataObject.enabled = $("#enabled").prop('checked'); + formDataObject.one_time = $("#one_time").prop('checked'); + if ($("#difficulty").val() == "reaction"){ + formDataObject.interval_type = "reaction"; + } + if (formDataObject.cron_string != ""){ + formDataObject.interval_type = ''; + } + console.log(formDataObject); + // Format the plain form data as JSON + let formDataJsonString = JSON.stringify(formDataObject, replacer); + + console.log(formDataJsonString); + + let res = await fetch(`/api/v2/servers/${serverId}/tasks/${schId}`, { + method: 'PATCH', + headers: { + 'X-XSRFToken': token + }, + body: formDataJsonString, + }); + let responseData = await res; + if (responseData.statusText === "OK") { + window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`; + } + }); }); @@ -265,6 +353,7 @@ document.getElementById("parent").required = true; document.getElementById("interval").required = false; document.getElementById("time").required = false; + $("#cron").val(""); } else { document.getElementById("ifAdvanced").style.display = "none"; @@ -274,6 +363,7 @@ document.getElementById("parent").required = false; document.getElementById("interval").required = true; document.getElementById("time").required = true; + $("#cron").val(""); } } function ifDays() { @@ -286,20 +376,6 @@ } } - async function del_task(sch_id, id) { - var token = getCookie("_xsrf") - - let res = await fetch(`/api/v2/servers/${id}/tasks/${sch_id}`, { - method: 'DELETE', - headers: { - 'token': token, - }, - }); - let responseData = await res; - if (responseData.statusText === "OK") { - window.location.reload(); - } - } function startup() { try { document.getElementById("{{ data['schedule']['interval_type'] }}").setAttribute('selected', true); diff --git a/app/frontend/templates/panel/server_schedules.html b/app/frontend/templates/panel/server_schedules.html index 5d776bbd..9f92fc6e 100644 --- a/app/frontend/templates/panel/server_schedules.html +++ b/app/frontend/templates/panel/server_schedules.html @@ -90,7 +90,7 @@

{{schedule.command}}

- {% if schedule.interval != '' %} + {% if schedule.interval_type != '' and schedule.interval_type != 'reaction' %}

{{ translate('serverSchedules', 'every', data['lang']) }}

{{schedule.interval}} {{schedule.interval_type}}

{% elif schedule.interval_type == 'reaction' %}