Working config.json from page

This commit is contained in:
Andrew 2023-01-15 18:57:22 -05:00
parent bea48d9059
commit 8d8b065e41
2 changed files with 58 additions and 13 deletions

View File

@ -1724,19 +1724,30 @@ class PanelHandler(BaseHandler):
try:
data = {}
with open(self.helper.settings_file, "r", encoding="utf-8") as f:
print("open")
keys = json.load(f).keys()
this_uuid = self.get_argument("uuid")
for key in keys:
print(self.get_argument(key))
data[key] = self.get_argument(key)
print("data:", data)
"""
arg_data = self.get_argument(key)
if arg_data.startswith(this_uuid):
arg_data = arg_data.split(",")
arg_data.pop(0)
data[key] = arg_data
else:
try:
data[key] = int(arg_data)
except:
if arg_data == "True":
data[key] = True
elif arg_data == "False":
data[key] = False
else:
data[key] = arg_data
with open(self.helper.settings_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
"""
except Exception as e:
logger.critical(
f"Config File Error: Unable to read {self.helper.settings_file} due to {e}"
"Config File Error: Unable to read "
f"{self.helper.settings_file} due to {e}"
)
self.redirect("/panel/config_json")

View File

@ -46,12 +46,9 @@
<small class="text-muted ml-1">
</small> </label>
{% if isinstance(item[1], list) %}
<ul>
{% for li in item[1] %}
<input style="color: black;" form="config-form" class="form-control" type="text" value="{{li}}"
name="{{item[0]}}" readonly><br />
{% end %}
</ul>
<br>
<textarea value="{{','.join(item[1])}}" type="text" name="{{item[0]}}"
class="list">{{','.join(item[1])}}</textarea>
{% elif isinstance(item[1], bool) %}
{% if item[1] == True %}
@ -107,6 +104,43 @@
{% block js %}
<script>
$("#config-form").submit(function (e) {
let uuid = uuidv4();
var token = getCookie("_xsrf")
e.preventDefault();
let class_list = document.getElementsByClassName("list");
let form_json = convertFormToJSON($("#config-form"));
for (let i = 0; i < class_list.length; i++) {
let str = String($(class_list.item(i)).val())
form_json[$(class_list.item(i)).attr("name")] = uuid + "," + str.replace(/\s/g, '');
};
form_json['uuid'] = uuid;
$.ajax({
type: "POST",
headers: { 'X-XSRFToken': token },
dataType: "json",
url: '/panel/config_json',
data: form_json,
success: function (data) {
window.location.reload();
},
});
});
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
function convertFormToJSON(form) {
const array = $(form).serializeArray(); // Encodes the set of form elements as an array of names and values.
const json = {};
$.each(array, function () {
json[this.name] = this.value || "";
});
return json;
}
$(document).ready(function () {
$('[data-toggle="popover"]').popover();
if ($(window).width() < 1000) {