Merge pull request 'Fixed Rename and Delete' (#9) from feature/backend_implementation into main

Reviewed-on: meulilivio/AISE1_Project#9
This commit is contained in:
Livio Meuli 2026-04-10 00:15:01 +02:00
commit 70b4772230
2 changed files with 62 additions and 25 deletions

View File

@ -134,7 +134,7 @@ class FileManager:
return False
def delete_file(self, relative_path):
file_path = (relative_path).resolve()
file_path = Path(relative_path).resolve()
if not str(file_path).startswith(str(self.base_path.resolve())):
st.error(f"Access denied: {relative_path}")

View File

@ -12,6 +12,64 @@ LANG_MAP = {
".json": "json", ".yaml": "yaml", ".yml": "yaml"
}
# ── Modals ────────────────────────────────────────────────────────────────────
@st.dialog("Rename File")
def _rename_dialog(file_path: str):
fm = FileManager()
st.write(f"Current name: **{Path(file_path).name}**")
new_name = st.text_input("New name:", value=Path(file_path).stem)
col1, col2 = st.columns(2)
with col1:
if st.button("Confirm", type="primary", use_container_width=True):
if not new_name.strip():
st.warning("Please enter a name.")
elif "/" in new_name or "\\" in new_name:
st.warning("Name must not contain slashes.")
else:
if fm.rename_file(file_path, new_name.strip()):
ext = Path(file_path).suffix
new_file_path = str(Path(file_path).parent / (Path(new_name.strip()).stem + ext))
i = st.session_state.open_files.index(file_path)
st.session_state.open_files[i] = new_file_path
st.session_state.files_content[new_file_path] = \
st.session_state.files_content.pop(file_path)
if st.session_state.active_file == file_path:
st.session_state.active_file = new_file_path
st.rerun()
else:
st.error("Rename failed. Check that the file still exists.")
with col2:
if st.button("Cancel", use_container_width=True):
st.rerun()
@st.dialog("Delete File")
def _delete_dialog(file_path: str):
fm = FileManager()
st.warning(f"Delete **{Path(file_path).name}**? This cannot be undone.")
col1, col2 = st.columns(2)
with col1:
if st.button("Delete", type="primary", use_container_width=True):
if fm.delete_file(file_path):
st.session_state.open_files.remove(file_path)
st.session_state.files_content.pop(file_path, None)
if st.session_state.active_file == file_path:
st.session_state.active_file = (
st.session_state.open_files[0]
if st.session_state.open_files else None
)
st.rerun()
else:
st.error("Delete failed. Check that the file still exists.")
with col2:
if st.button("Cancel", use_container_width=True):
st.rerun()
def run_active_file():
active_file = st.session_state.active_file
@ -101,32 +159,11 @@ def render_editor():
with cols[2]:
if st.button("Rename File", key=f"rename_{file_path}"):
new_name = st.text_input("New File Name", key=f"new_name_{file_path}")
if "/" in new_name or "\\" in new_name:
st.warning("Do not include slashes in names!")
elif new_name:
try:
fm.rename_file(file_path, new_name)
st.success(f"File renamed to '{new_name}' successfully!")
st.rerun()
except Exception as e:
st.error(f"Error renaming file: {e}")
_rename_dialog(file_path)
with cols[3]:
if st.button("Delete File", key=f"delete_{file_path}"):
try:
fm.delete_file(file_path)
st.success(f"File '{Path(file_path).name}' deleted successfully!")
st.session_state.open_files.remove(file_path)
del st.session_state.files_content[file_path]
st.session_state.active_file = (
st.session_state.open_files[0]
if st.session_state.open_files
else None
)
st.rerun()
except Exception as e:
st.error(f"Error deleting file: {e}")
_delete_dialog(file_path)
if st.button("▶ Run Code", key="run_code"):
result = run_active_file()