Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
13 changes: 13 additions & 0 deletions examples/bootstrap4/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.fileadmin import FileAdmin
from flask_admin.contrib.sqla import ModelView
from flask_admin.menu import MenuDivider
from flask_admin.menu import MenuLink
Expand Down Expand Up @@ -64,6 +65,13 @@ class UserAdmin(CustomView):
page_size = 7


class FileAdminModal(FileAdmin):
rename_modal = True
edit_modal = True
mkdir_modal = True
upload_modal = True


def build_sample_db():
"""
Populate a small db with some example entries.
Expand Down Expand Up @@ -223,6 +231,11 @@ def build_sample_db():
)
)

admin.add_view(FileAdmin("files/", name="Local Files", category="Menu"))
admin.add_view(
FileAdminModal("files/", name="Local Files with Modals", category="Menu")
)

admin.add_link(
MenuLink(
name="link1",
Expand Down
6 changes: 6 additions & 0 deletions flask_admin/contrib/fileadmin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,10 @@ def rename(self) -> T_RESPONSE | str:
form = self.name_form()

path = form.path.data # type: ignore[attr-defined]

if request.method == "GET" and hasattr(form, "name"):
form.name.data = op.basename(path)

if path:
base_path, full_path, path = self._normalize_path(path)

Expand Down Expand Up @@ -1241,6 +1245,8 @@ def rename(self) -> T_RESPONSE | str:
return redirect(return_url)
else:
helpers.flash_errors(form, message="Failed to rename: %(error)s")
if hasattr(form, "name"):
form.name.data = op.basename(path)

if self.rename_modal and request.args.get("modal"):
template = self.rename_modal_template
Expand Down
1 change: 1 addition & 0 deletions flask_admin/tests/fileadmin/test_fileadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MyFileAdmin(fileadmin_class): # type: ignore[valid-type, misc]
rv = client.get("/admin/myfileadmin/rename/?path=dummy.txt")
assert rv.status_code == 200
assert "dummy.txt" in rv.data.decode("utf-8")
assert 'value="dummy.txt"' in rv.data.decode("utf-8")

rv = client.post(
"/admin/myfileadmin/rename/?path=dummy.txt",
Expand Down