Skip to content

Commit

Permalink
Merge pull request #14 from OpenIPC/master
Browse files Browse the repository at this point in the history
Add Delete button in the settings page
  • Loading branch information
mikecarr authored Oct 5, 2024
2 parents 130684f + c4449e3 commit b1c605a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 42 deletions.
52 changes: 41 additions & 11 deletions py_config_gs/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def load_config():
raise


load_config()
load_config()
VIDEO_DIR = os.path.expanduser(settings["VIDEO_DIR"])
SERVER_PORT = settings["SERVER_PORT"]

Expand Down Expand Up @@ -153,6 +153,12 @@ def edit(filename):

@app.route("/videos")
def videos():

global settings
load_config()
VIDEO_DIR = os.path.expanduser(settings["VIDEO_DIR"])


"""List video files in the video directory."""
video_files = [
f for f in os.listdir(VIDEO_DIR) if f.endswith((".mp4", ".mkv", ".avi"))
Expand Down Expand Up @@ -274,25 +280,49 @@ def settings_view():

if request.method == "POST":
try:
config_files_names = request.form.getlist("config_files_names")
config_files_paths = request.form.getlist("config_files_paths")
current_config_files = settings.get("config_files", [])
updated_config_files = current_config_files.copy() # Start with the current files

# Log all form data for debugging
logger.debug(f"Form Data: {request.form}")

# Handle deletion
files_to_delete = request.form.getlist("delete_files")
logger.debug(f"Files to delete: {files_to_delete}")
if files_to_delete:
updated_config_files = [
file for file in updated_config_files if file["name"] not in files_to_delete
]

# Count how many config file inputs there are
new_file_count = sum(1 for key in request.form.keys() if key.startswith("config_files[") and "][name]" in key)
logger.debug(f"New config file count: {new_file_count}")

for i in range(new_file_count):
name = request.form.get(f'config_files[{i}][name]')
path = request.form.get(f'config_files[{i}][path]')

# Only add if both fields are filled, the name does not already exist, and it's not marked for deletion
if name and path and name not in files_to_delete and not any(file['name'] == name for file in updated_config_files):
updated_config_files.append({"name": name, "path": path})
logger.debug(f"Added new config file: {name}, {path}")

# Additional settings
video_dir = request.form.get("VIDEO_DIR")
server_port = request.form.get("SERVER_PORT")

config_files = [
{"name": name, "path": path}
for name, path in zip(config_files_names, config_files_paths)
]


# Update the settings data
settings_data = {
"VIDEO_DIR": video_dir,
"SERVER_PORT": server_port,
"config_files": config_files,
"config_files": updated_config_files,
}


# Save the updated settings to the settings file
with open(SETTINGS_FILE, "w") as f:
json.dump(settings_data, f, indent=4)

logger.debug("Settings saved successfully.")
flash("Settings updated successfully.")
except Exception as e:
flash(f"Error saving settings: {e}", "error")
Expand Down
2 changes: 1 addition & 1 deletion py_config_gs/py-config-gs.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

],

"VIDEO_DIR": "/media",
"VIDEO_DIR": "~/media",
"SERVER_PORT": 5001

}
65 changes: 35 additions & 30 deletions py_config_gs/templates/settings.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
{% extends "base.html" %}

{% block content %}

<div class="config-section" id="configuration">
<h1>Settings</h1>
<h1>Settings</h1>

<form method="POST">
<h2>Configuration Files</h2>
<div id="config-files">
{% for file in settings.config_files %}
<div>
<label for="file_name_{{ loop.index0 }}">Name:</label>
<input type="text" name="config_files_names" id="file_name_{{ loop.index0 }}" value="{{ file.name }}">
<label for="file_path_{{ loop.index0 }}">Path:</label>
<input type="text" name="config_files_paths" id="file_path_{{ loop.index0 }}" value="{{ file.path }}">
<form method="POST">
<h2>Configuration Files</h2>
<div id="config-files">
{% for file in settings.config_files %}
<div>
<input type="checkbox" name="delete_files" value="{{ file.name }}">
<label for="file_{{ loop.index0 }}">Name:</label>
<input type="text" name="config_files[{{ loop.index0 }}][name]" id="file_{{ loop.index0 }}" value="{{ file.name }}">
<label for="path_{{ loop.index0 }}">Path:</label>
<input type="text" name="config_files[{{ loop.index0 }}][path]" id="path_{{ loop.index0 }}" value="{{ file.path }}">
</div>
{% endfor %}
</div>
{% endfor %}
</div>
<button type="button" id="add-file">Add</button>

<button type="button" id="add-file">Add</button>
<button type="submit">Delete Selected</button>

<h2>Additional Settings</h2>
<div>
<label for="VIDEO_DIR">VIDEO_DIR:</label>
<input type="text" name="VIDEO_DIR" id="VIDEO_DIR" value="{{ settings.VIDEO_DIR }}">
</div>
<div>
<label for="SERVER_PORT">SERVER_PORT:</label>
<input type="number" name="SERVER_PORT" id="SERVER_PORT" value="{{ settings.SERVER_PORT }}">
</div>
<h2>Additional Settings</h2>
<div>
<label for="VIDEO_DIR">VIDEO_DIR:</label>
<input type="text" name="VIDEO_DIR" id="VIDEO_DIR" value="{{ settings.VIDEO_DIR }}">
</div>
<div>
<label for="SERVER_PORT">SERVER_PORT:</label>
<input type="number" name="SERVER_PORT" id="SERVER_PORT" value="{{ settings.SERVER_PORT }}">
</div>

<button type="submit">Save Settings</button>
</form>
<button type="submit">Save Settings</button>
</form>
</div>

<script>

document.getElementById('add-file').addEventListener('click', function () {
const configFilesDiv = document.getElementById('config-files');
const index = configFilesDiv.children.length;
const index = configFilesDiv.children.length; // This should match the length of existing inputs
const newFileDiv = document.createElement('div');
newFileDiv.innerHTML = `
<label for="file_name_${index}">Name:</label>
<input type="text" name="config_files_names" id="file_name_${index}" value="">
<label for="file_path_${index}">Path:</label>
<input type="text" name="config_files_paths" id="file_path_${index}" value="">
<input type="checkbox" name="delete_files" value="">
<label for="file_${index}">Name:</label>
<input type="text" name="config_files[${index}][name]" id="file_${index}" value="">
<label for="path_${index}">Path:</label>
<input type="text" name="config_files[${index}][path]" id="path_${index}" value="">
`;
configFilesDiv.appendChild(newFileDiv);

});
</script>

Expand Down

0 comments on commit b1c605a

Please sign in to comment.