Przeglądaj źródła

Allow view overrides for registering models with admin (#1249)

Siddhant Goel 1 rok temu
rodzic
commit
c6c4410db5
3 zmienionych plików z 26 dodań i 1 usunięć
  1. 1 0
      reflex/admin.py
  2. 4 1
      reflex/app.py
  3. 21 0
      tests/test_app.py

+ 1 - 0
reflex/admin.py

@@ -10,4 +10,5 @@ class AdminDash:
     """Data used to build the admin dashboard."""
 
     models: list = field(default_factory=list)
+    view_overrides: dict = field(default_factory=dict)
     admin: Optional[Admin] = None

+ 4 - 1
reflex/app.py

@@ -428,8 +428,11 @@ class App(Base):
                     logo_url="https://pynecone.io/logo.png",
                 )
             )
+
             for model in config.admin_dash.models:
-                admin.add_view(ModelView(model))
+                view = config.admin_dash.view_overrides.get(model, ModelView)
+                admin.add_view(view(model))
+
             admin.mount_to(self.api)
 
     def compile(self):

+ 21 - 0
tests/test_app.py

@@ -13,6 +13,7 @@ import sqlmodel
 from fastapi import UploadFile
 from starlette_admin.auth import AuthProvider
 from starlette_admin.contrib.sqla.admin import Admin
+from starlette_admin.contrib.sqla.view import ModelView
 
 from reflex import AdminDash, constants
 from reflex.app import App, DefaultState, process, upload
@@ -266,6 +267,26 @@ def test_initialize_with_custom_admin_dashboard(
     assert app.admin_dash.admin.auth_provider == test_custom_auth_admin
 
 
+def test_initialize_admin_dashboard_with_view_overrides(test_model):
+    """Test setting the admin dashboard of an app with view class overriden.
+
+    Args:
+        test_model: The default model.
+    """
+
+    class TestModelView(ModelView):
+        pass
+
+    app = App(
+        admin_dash=AdminDash(
+            models=[test_model], view_overrides={test_model: TestModelView}
+        )
+    )
+    assert app.admin_dash is not None
+    assert app.admin_dash.models == [test_model]
+    assert app.admin_dash.view_overrides[test_model] == TestModelView
+
+
 def test_initialize_with_state(test_state):
     """Test setting the state of an app.