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
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.297
rev: v1.1.313
hooks:
- id: pyright
args: [pynecone, tests]

View File

@ -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

View File

@ -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

View File

@ -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))

View File

@ -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}"
)