Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/admin html #22

Merged
merged 8 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 26 additions & 3 deletions blitz/api/blitz_admin.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
from functools import lru_cache
from fastapi import FastAPI
from starlette_admin import CustomView
from blitz.db.db import get_sqlite_engine
from starlette_admin.contrib.sqla import Admin, ModelView
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, List
from starlette_admin.views import Link

from starlette.responses import Response
from starlette.requests import Request
from blitz.settings import Settings, get_settings
from starlette.templating import Jinja2Templates

if TYPE_CHECKING:
from blitz.app import BlitzApp


class HomeView(CustomView):
def __init__(self, blitz_app: "BlitzApp", *args: Any, **kwargs: Any):
self.blitz_app = blitz_app
super().__init__(*args, **kwargs)

async def render(
self,
request: Request,
templates: Jinja2Templates,
) -> Response:
return templates.TemplateResponse("index.html", {"request": request, "blitz_app": self.blitz_app})


class BlitzAdmin:
def __init__(self, blitz_app: "BlitzApp", settings: Settings = get_settings()) -> None:
self.blitz_app = blitz_app
self.admin = Admin(
title=f"{blitz_app.name} Admin",
title=f"Blitz Admin - {blitz_app.name}",
# FIXME find a better way to get the engine
engine=get_sqlite_engine(blitz_app, in_memory=blitz_app._in_memory, file_name="app.db"),
base_url="/admin/",
templates_dir="blitz/api/templates",
index_view=HomeView(blitz_app, "Home"),
statics_dir="blitz/ui/assets/",
logo_url="/admin/statics/blitz_logo_and_text_2.png",
)
for resource in blitz_app.resources:
self.admin.add_view(ModelView(resource.model))
Expand All @@ -29,6 +50,8 @@ def __init__(self, blitz_app: "BlitzApp", settings: Settings = get_settings()) -
url=f"http://localhost:{settings.BLITZ_PORT}/dashboard/projects/{self.blitz_app.name}",
)
)
self.admin.add_view(Link(label="GitHub", icon="fa-brands fa-github", url="https://github.com/Paperz-org/blitz"))
self.admin.add_view(Link(label="Documentation", icon="fa fa-book", url="https://paperz-org.github.io/blitz/"))

def mount_to(self, app: FastAPI) -> None:
self.admin.mount_to(app)
23 changes: 23 additions & 0 deletions blitz/api/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "layout.html" %}
{% block header %}
<div class="d-flex justify-content-between align-items-center">
<div class="row g-2 align-items-center">
<div class="col">
<h1>Welcome in your Blitz Admin panel</h1>

</div>
</div>
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="{{ url_for(__name__ ~ ':index')}}">Admin</a>
</li>
<li class="breadcrumb-item active">Home</li>
</ol>
</div>
{% endblock %}
{% block content %}
<h2 class="page-title">
{{ blitz_app.name}} Admin - v{{ blitz_app.file.config.version}}
</h2>
<div>{{ blitz_app.file.config.description }}</div>
{% endblock %}
2 changes: 1 addition & 1 deletion blitz/cli/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def start_blitz(
host="localhost",
port=port,
reload=True,
reload_includes=str(blitz_app.file.path.relative_to(Path.cwd())),
reload_includes=[str(blitz_app.file.path.relative_to(Path.cwd())), "blitz/api/templates"],
pbrochar marked this conversation as resolved.
Show resolved Hide resolved
reload_excludes=["*_migration.py", "migrations/*.py"],
log_config=None,
log_level="info",
Expand Down
2 changes: 1 addition & 1 deletion blitz/ui/components/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(
self.drawer: LeftDrawer | None = None

def left_drawer(self) -> None:
with ui.left_drawer(value=self.drawer_open, fixed=True, bottom_corner=True).props("width=200").classes(
with ui.left_drawer(value=self.drawer_open, fixed=True, bottom_corner=True).props("width=240").classes(
"px-0 bg-[#14151a]"
) as self.drawer:
MenuLink("Dashboard", f"/projects/{self.current_project}", "dashboard").render()
Expand Down
Loading