cleanup admin feature (#1168)

This commit is contained in:
Thomas Brandého 2023-06-09 21:48:18 +02:00 committed by GitHub
parent 765fac940d
commit a275b4ad6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 34 deletions

View File

@ -5,7 +5,7 @@ repos:
- id: ruff - id: ruff
- repo: https://github.com/RobertCraigie/pyright-python - repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.297 rev: v1.1.313
hooks: hooks:
- id: pyright - id: pyright
args: [pynecone, tests] args: [pynecone, tests]

View File

@ -297,7 +297,11 @@ class App(Base):
# Add meta information to the component. # Add meta information to the component.
compiler_utils.add_meta( 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 # Add script tags if given
@ -391,7 +395,11 @@ class App(Base):
component = component if isinstance(component, Component) else component() component = component if isinstance(component, Component) else component()
compiler_utils.add_meta( 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 froute = format.format_route
@ -408,7 +416,7 @@ class App(Base):
"""Setup the admin dash.""" """Setup the admin dash."""
# Get the config. # Get the config.
config = get_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 # Build the admin dashboard
admin = ( admin = (
config.admin_dash.admin config.admin_dash.admin

View File

@ -174,9 +174,6 @@ class Config(Base):
# Additional frontend packages to install. # Additional frontend packages to install.
frontend_packages: List[str] = [] frontend_packages: List[str] = []
# Enable the admin dash.
enable_admin: bool = False
# The Admin Dash # The Admin Dash
admin_dash: Optional[AdminDash] = None admin_dash: Optional[AdminDash] = None

View File

@ -8,23 +8,26 @@ from pynecone.base import Base
from pynecone.config import get_config from pynecone.config import get_config
def get_engine(): def get_engine(url: Optional[str] = None):
"""Get the database engine. """Get the database engine.
Args:
url: the DB url to use.
Returns: Returns:
The database engine. The database engine.
Raises: Raises:
ValueError: If the database url is None. ValueError: If the database url is None.
""" """
url = get_config().db_url conf = get_config()
enable_admin = get_config().enable_admin url = url or conf.db_url
if not url: if url is None:
raise ValueError("No database url in config") raise ValueError("No database url configured")
return sqlmodel.create_engine( return sqlmodel.create_engine(
url, url,
echo=False, 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) return sqlmodel.select(cls)
def session(url=None): def session(url: Optional[str] = None) -> sqlmodel.Session:
"""Get a session to interact with the database. """Get a session to interact with the database.
Args: Args:
@ -92,13 +95,4 @@ def session(url=None):
Returns: Returns:
A database session. A database session.
""" """
enable_admin = get_config().enable_admin return sqlmodel.Session(get_engine(url))
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)

View File

@ -340,10 +340,9 @@ def is_latest_template() -> bool:
def check_admin_settings(): def check_admin_settings():
"""Check if admin settings are set and valid for logging in cli app.""" """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 admin_dash = get_config().admin_dash
current_time = datetime.now() current_time = datetime.now()
if admin_enabled and admin_dash: if admin_dash:
if not admin_dash.models: if not admin_dash.models:
console.print( 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}" 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( console.print(
"Admin dashboard running at: [bold green]http://localhost:8000/admin[/bold green]" "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}"
)