-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for Esmerald * Add missing self to Esmerald routes * Adding missing Esmerald reference command * Update piccolo/apps/asgi/commands/templates/app/home/_esmerald_endpoints.py.jinja Co-authored-by: sinisaos <[email protected]> * Update piccolo/apps/asgi/commands/templates/app/_esmerald_app.py.jinja Co-authored-by: sinisaos <[email protected]> * Remove piccolo admin from esmerald routes * Add esmerald to template * Update _esmerald_app.py.jinja with cleat paths * Update piccolo/apps/asgi/commands/templates/app/_esmerald_app.py.jinja Co-authored-by: sinisaos <[email protected]> --------- Co-authored-by: sinisaos <[email protected]>
- Loading branch information
Showing
8 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
piccolo/apps/asgi/commands/templates/app/_esmerald_app.py.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import typing as t | ||
|
||
from pathlib import Path | ||
|
||
from piccolo.utils.pydantic import create_pydantic_model | ||
from piccolo.engine import engine_finder | ||
|
||
from esmerald import ( | ||
Esmerald, | ||
Include, | ||
Gateway, | ||
JSONResponse, | ||
APIView, | ||
get, | ||
post, | ||
put, | ||
delete | ||
) | ||
from esmerald.config import StaticFilesConfig | ||
|
||
from home.endpoints import home | ||
from home.piccolo_app import APP_CONFIG | ||
from home.tables import Task | ||
|
||
|
||
async def open_database_connection_pool(): | ||
try: | ||
engine = engine_finder() | ||
await engine.start_connection_pool() | ||
except Exception: | ||
print("Unable to connect to the database") | ||
|
||
|
||
async def close_database_connection_pool(): | ||
try: | ||
engine = engine_finder() | ||
await engine.close_connection_pool() | ||
except Exception: | ||
print("Unable to connect to the database") | ||
|
||
|
||
TaskModelIn: t.Any = create_pydantic_model( | ||
table=Task, | ||
model_name='TaskModelIn' | ||
) | ||
TaskModelOut: t.Any = create_pydantic_model( | ||
table=Task, | ||
include_default_columns=True, | ||
model_name='TaskModelOut' | ||
) | ||
|
||
|
||
class TaskAPIView(APIView): | ||
path: str = "/" | ||
tags: str = ["Task"] | ||
|
||
@get("/") | ||
async def tasks(self) -> t.List[TaskModelOut]: | ||
return await Task.select().order_by(Task.id) | ||
|
||
|
||
@post('/') | ||
async def create_task(self, payload: TaskModelIn) -> TaskModelOut: | ||
task = Task(**payload.dict()) | ||
await task.save() | ||
return task.to_dict() | ||
|
||
|
||
@put('/{task_id}') | ||
async def update_task(self, payload: TaskModelIn, task_id: int) -> TaskModelOut: | ||
task = await Task.objects().get(Task.id == task_id) | ||
if not task: | ||
return JSONResponse({}, status_code=404) | ||
|
||
for key, value in payload.dict().items(): | ||
setattr(task, key, value) | ||
|
||
await task.save() | ||
|
||
return task.to_dict() | ||
|
||
|
||
@delete('/{task_id}') | ||
async def delete_task(self, task_id: int) -> None: | ||
task = await Task.objects().get(Task.id == task_id) | ||
if not task: | ||
return JSONResponse({}, status_code=404) | ||
|
||
await task.remove() | ||
|
||
|
||
app = Esmerald( | ||
routes=[ | ||
Gateway("/", handler=home), | ||
Gateway("/tasks", handler=TaskAPIView) | ||
], | ||
static_files_config=StaticFilesConfig(path="/static", directory=Path("static")), | ||
on_startup=[open_database_connection_pool], | ||
on_shutdown=[close_database_connection_pool], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
piccolo/apps/asgi/commands/templates/app/home/_esmerald_endpoints.py.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
|
||
import jinja2 | ||
from esmerald import Request, Response, get | ||
from esmerald.responses import HTMLResponse | ||
|
||
ENVIRONMENT = jinja2.Environment( | ||
loader=jinja2.FileSystemLoader( | ||
searchpath=os.path.join(os.path.dirname(__file__), "templates") | ||
) | ||
) | ||
|
||
|
||
@get(path="/", include_in_schema=False) | ||
def home(request: Request) -> HTMLResponse: | ||
template = ENVIRONMENT.get_template("home.html.jinja") | ||
|
||
content = template.render(title="Piccolo + ASGI",) | ||
|
||
return HTMLResponse(content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters