From a275b4ad6efc32a2e608aa058bd9e27237e0f0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Fri, 9 Jun 2023 21:48:18 +0200 Subject: [PATCH] cleanup admin feature (#1168) --- .pre-commit-config.yaml | 2 +- pynecone/app.py | 14 +++++++++++--- pynecone/config.py | 3 --- pynecone/model.py | 28 +++++++++++----------------- pynecone/utils/prerequisites.py | 11 +---------- 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ae7881dc5..518191953 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: ruff - repo: https://github.com/RobertCraigie/pyright-python - rev: v1.1.297 + rev: v1.1.313 hooks: - id: pyright args: [pynecone, tests] diff --git a/pynecone/app.py b/pynecone/app.py index 67c2c07a0..26b3dcb09 100644 --- a/pynecone/app.py +++ b/pynecone/app.py @@ -297,7 +297,11 @@ class App(Base): # Add meta information to the component. compiler_utils.add_meta( - component, title=title, image=image, description=description, meta=meta + component, + title=title, + image=image, + description=description, + meta=meta, ) # Add script tags if given @@ -391,7 +395,11 @@ class App(Base): component = component if isinstance(component, Component) else component() compiler_utils.add_meta( - component, title=title, image=image, description=description, meta=meta + component, + title=title, + image=image, + description=description, + meta=meta, ) froute = format.format_route @@ -408,7 +416,7 @@ class App(Base): """Setup the admin dash.""" # Get the config. config = get_config() - if config.enable_admin and config.admin_dash and config.admin_dash.models: + if config.admin_dash and config.admin_dash.models: # Build the admin dashboard admin = ( config.admin_dash.admin diff --git a/pynecone/config.py b/pynecone/config.py index ca2e29ebd..861e3beba 100644 --- a/pynecone/config.py +++ b/pynecone/config.py @@ -174,9 +174,6 @@ class Config(Base): # Additional frontend packages to install. frontend_packages: List[str] = [] - # Enable the admin dash. - enable_admin: bool = False - # The Admin Dash admin_dash: Optional[AdminDash] = None diff --git a/pynecone/model.py b/pynecone/model.py index b5959ce5c..845c2220d 100644 --- a/pynecone/model.py +++ b/pynecone/model.py @@ -8,23 +8,26 @@ from pynecone.base import Base from pynecone.config import get_config -def get_engine(): +def get_engine(url: Optional[str] = None): """Get the database engine. + Args: + url: the DB url to use. + Returns: The database engine. Raises: ValueError: If the database url is None. """ - url = get_config().db_url - enable_admin = get_config().enable_admin - if not url: - raise ValueError("No database url in config") + conf = get_config() + url = url or conf.db_url + if url is None: + raise ValueError("No database url configured") return sqlmodel.create_engine( url, echo=False, - connect_args={"check_same_thread": False} if enable_admin else {}, + connect_args={"check_same_thread": False} if conf.admin_dash else {}, ) @@ -83,7 +86,7 @@ class Model(Base, sqlmodel.SQLModel): return sqlmodel.select(cls) -def session(url=None): +def session(url: Optional[str] = None) -> sqlmodel.Session: """Get a session to interact with the database. Args: @@ -92,13 +95,4 @@ def session(url=None): Returns: A database session. """ - enable_admin = get_config().enable_admin - if url is not None: - return sqlmodel.Session( - sqlmodel.create_engine( - url, - connect_args={"check_same_thread": False} if enable_admin else {}, - ), - ) - engine = get_engine() - return sqlmodel.Session(engine) + return sqlmodel.Session(get_engine(url)) diff --git a/pynecone/utils/prerequisites.py b/pynecone/utils/prerequisites.py index 9abef3967..296b83425 100644 --- a/pynecone/utils/prerequisites.py +++ b/pynecone/utils/prerequisites.py @@ -340,10 +340,9 @@ def is_latest_template() -> bool: def check_admin_settings(): """Check if admin settings are set and valid for logging in cli app.""" - admin_enabled = get_config().enable_admin admin_dash = get_config().admin_dash current_time = datetime.now() - if admin_enabled and admin_dash: + if admin_dash: if not admin_dash.models: console.print( f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard enabled, but no models defined in [bold magenta]pcconfig.py[/bold magenta]. Time: {current_time}" @@ -355,11 +354,3 @@ def check_admin_settings(): console.print( "Admin dashboard running at: [bold green]http://localhost:8000/admin[/bold green]" ) - elif admin_enabled: - console.print( - f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin enabled, but no admin dashboard defined in [bold magenta]pcconfig.py[/bold magenta]. Time: {current_time}" - ) - elif admin_dash: - console.print( - f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard defined, but admin is not enabled in [bold magenta]pcconfig.py[/bold magenta]. Time: {current_time}" - )