Skip to content

Commit b1c605a

Browse files
authored
Merge pull request #14 from OpenIPC/master
Add Delete button in the settings page
2 parents 130684f + c4449e3 commit b1c605a

File tree

3 files changed

+77
-42
lines changed

3 files changed

+77
-42
lines changed

py_config_gs/app.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def load_config():
5959
raise
6060

6161

62-
load_config()
62+
load_config()
6363
VIDEO_DIR = os.path.expanduser(settings["VIDEO_DIR"])
6464
SERVER_PORT = settings["SERVER_PORT"]
6565

@@ -153,6 +153,12 @@ def edit(filename):
153153

154154
@app.route("/videos")
155155
def videos():
156+
157+
global settings
158+
load_config()
159+
VIDEO_DIR = os.path.expanduser(settings["VIDEO_DIR"])
160+
161+
156162
"""List video files in the video directory."""
157163
video_files = [
158164
f for f in os.listdir(VIDEO_DIR) if f.endswith((".mp4", ".mkv", ".avi"))
@@ -274,25 +280,49 @@ def settings_view():
274280

275281
if request.method == "POST":
276282
try:
277-
config_files_names = request.form.getlist("config_files_names")
278-
config_files_paths = request.form.getlist("config_files_paths")
283+
current_config_files = settings.get("config_files", [])
284+
updated_config_files = current_config_files.copy() # Start with the current files
285+
286+
# Log all form data for debugging
287+
logger.debug(f"Form Data: {request.form}")
288+
289+
# Handle deletion
290+
files_to_delete = request.form.getlist("delete_files")
291+
logger.debug(f"Files to delete: {files_to_delete}")
292+
if files_to_delete:
293+
updated_config_files = [
294+
file for file in updated_config_files if file["name"] not in files_to_delete
295+
]
296+
297+
# Count how many config file inputs there are
298+
new_file_count = sum(1 for key in request.form.keys() if key.startswith("config_files[") and "][name]" in key)
299+
logger.debug(f"New config file count: {new_file_count}")
300+
301+
for i in range(new_file_count):
302+
name = request.form.get(f'config_files[{i}][name]')
303+
path = request.form.get(f'config_files[{i}][path]')
304+
305+
# Only add if both fields are filled, the name does not already exist, and it's not marked for deletion
306+
if name and path and name not in files_to_delete and not any(file['name'] == name for file in updated_config_files):
307+
updated_config_files.append({"name": name, "path": path})
308+
logger.debug(f"Added new config file: {name}, {path}")
309+
310+
# Additional settings
279311
video_dir = request.form.get("VIDEO_DIR")
280312
server_port = request.form.get("SERVER_PORT")
281-
282-
config_files = [
283-
{"name": name, "path": path}
284-
for name, path in zip(config_files_names, config_files_paths)
285-
]
286-
313+
314+
# Update the settings data
287315
settings_data = {
288316
"VIDEO_DIR": video_dir,
289317
"SERVER_PORT": server_port,
290-
"config_files": config_files,
318+
"config_files": updated_config_files,
291319
}
292-
320+
321+
# Save the updated settings to the settings file
293322
with open(SETTINGS_FILE, "w") as f:
294323
json.dump(settings_data, f, indent=4)
295324

325+
logger.debug("Settings saved successfully.")
296326
flash("Settings updated successfully.")
297327
except Exception as e:
298328
flash(f"Error saving settings: {e}", "error")

py_config_gs/py-config-gs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
],
2626

27-
"VIDEO_DIR": "/media",
27+
"VIDEO_DIR": "~/media",
2828
"SERVER_PORT": 5001
2929

3030
}

py_config_gs/templates/settings.html

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
{% extends "base.html" %}
22

33
{% block content %}
4-
54
<div class="config-section" id="configuration">
6-
<h1>Settings</h1>
5+
<h1>Settings</h1>
76

8-
<form method="POST">
9-
<h2>Configuration Files</h2>
10-
<div id="config-files">
11-
{% for file in settings.config_files %}
12-
<div>
13-
<label for="file_name_{{ loop.index0 }}">Name:</label>
14-
<input type="text" name="config_files_names" id="file_name_{{ loop.index0 }}" value="{{ file.name }}">
15-
<label for="file_path_{{ loop.index0 }}">Path:</label>
16-
<input type="text" name="config_files_paths" id="file_path_{{ loop.index0 }}" value="{{ file.path }}">
7+
<form method="POST">
8+
<h2>Configuration Files</h2>
9+
<div id="config-files">
10+
{% for file in settings.config_files %}
11+
<div>
12+
<input type="checkbox" name="delete_files" value="{{ file.name }}">
13+
<label for="file_{{ loop.index0 }}">Name:</label>
14+
<input type="text" name="config_files[{{ loop.index0 }}][name]" id="file_{{ loop.index0 }}" value="{{ file.name }}">
15+
<label for="path_{{ loop.index0 }}">Path:</label>
16+
<input type="text" name="config_files[{{ loop.index0 }}][path]" id="path_{{ loop.index0 }}" value="{{ file.path }}">
17+
</div>
18+
{% endfor %}
1719
</div>
18-
{% endfor %}
19-
</div>
20-
<button type="button" id="add-file">Add</button>
20+
21+
<button type="button" id="add-file">Add</button>
22+
<button type="submit">Delete Selected</button>
2123

22-
<h2>Additional Settings</h2>
23-
<div>
24-
<label for="VIDEO_DIR">VIDEO_DIR:</label>
25-
<input type="text" name="VIDEO_DIR" id="VIDEO_DIR" value="{{ settings.VIDEO_DIR }}">
26-
</div>
27-
<div>
28-
<label for="SERVER_PORT">SERVER_PORT:</label>
29-
<input type="number" name="SERVER_PORT" id="SERVER_PORT" value="{{ settings.SERVER_PORT }}">
30-
</div>
24+
<h2>Additional Settings</h2>
25+
<div>
26+
<label for="VIDEO_DIR">VIDEO_DIR:</label>
27+
<input type="text" name="VIDEO_DIR" id="VIDEO_DIR" value="{{ settings.VIDEO_DIR }}">
28+
</div>
29+
<div>
30+
<label for="SERVER_PORT">SERVER_PORT:</label>
31+
<input type="number" name="SERVER_PORT" id="SERVER_PORT" value="{{ settings.SERVER_PORT }}">
32+
</div>
3133

32-
<button type="submit">Save Settings</button>
33-
</form>
34+
<button type="submit">Save Settings</button>
35+
</form>
3436
</div>
3537

3638
<script>
39+
3740
document.getElementById('add-file').addEventListener('click', function () {
3841
const configFilesDiv = document.getElementById('config-files');
39-
const index = configFilesDiv.children.length;
42+
const index = configFilesDiv.children.length; // This should match the length of existing inputs
4043
const newFileDiv = document.createElement('div');
4144
newFileDiv.innerHTML = `
42-
<label for="file_name_${index}">Name:</label>
43-
<input type="text" name="config_files_names" id="file_name_${index}" value="">
44-
<label for="file_path_${index}">Path:</label>
45-
<input type="text" name="config_files_paths" id="file_path_${index}" value="">
45+
<input type="checkbox" name="delete_files" value="">
46+
<label for="file_${index}">Name:</label>
47+
<input type="text" name="config_files[${index}][name]" id="file_${index}" value="">
48+
<label for="path_${index}">Path:</label>
49+
<input type="text" name="config_files[${index}][path]" id="path_${index}" value="">
4650
`;
4751
configFilesDiv.appendChild(newFileDiv);
52+
4853
});
4954
</script>
5055

0 commit comments

Comments
 (0)