diff --git a/py_config_gs/app.py b/py_config_gs/app.py index a2f9215..66ae1ec 100644 --- a/py_config_gs/app.py +++ b/py_config_gs/app.py @@ -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"] @@ -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")) @@ -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") diff --git a/py_config_gs/py-config-gs.json b/py_config_gs/py-config-gs.json index c323cd2..6a6f0c6 100644 --- a/py_config_gs/py-config-gs.json +++ b/py_config_gs/py-config-gs.json @@ -24,7 +24,7 @@ ], - "VIDEO_DIR": "/media", + "VIDEO_DIR": "~/media", "SERVER_PORT": 5001 } diff --git a/py_config_gs/templates/settings.html b/py_config_gs/templates/settings.html index 33eded2..13e8733 100644 --- a/py_config_gs/templates/settings.html +++ b/py_config_gs/templates/settings.html @@ -1,50 +1,55 @@ {% extends "base.html" %} {% block content %} -
-

Settings

+

Settings

-
-

Configuration Files

-
- {% for file in settings.config_files %} -
- - - - + +

Configuration Files

+
+ {% for file in settings.config_files %} +
+ + + + + +
+ {% endfor %}
- {% endfor %} -
- + + + -

Additional Settings

-
- - -
-
- - -
+

Additional Settings

+
+ + +
+
+ + +
- - + +