Skip to content

Commit

Permalink
update Litestar template to use create_pydantic_model (#859)
Browse files Browse the repository at this point in the history
* update Litestar template to use create_pydantic_model

* Update requirements.txt

* pin fastapi version in new.py

---------

Co-authored-by: Daniel Townsend <[email protected]>
  • Loading branch information
sinisaos and dantownsend authored Jul 7, 2023
1 parent 36e80f5 commit fb3c8eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion piccolo/apps/asgi/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SERVERS = ["uvicorn", "Hypercorn"]
ROUTERS = ["starlette", "fastapi", "blacksheep", "litestar"]
ROUTER_DEPENDENCIES = {
"litestar": ["litestar==2.0.0a3"],
"fastapi": ["fastapi<0.100.0"],
}


Expand Down
44 changes: 25 additions & 19 deletions piccolo/apps/asgi/commands/templates/app/_litestar_app.py.jinja
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import typing as t

from piccolo.engine import engine_finder
from piccolo_admin.endpoints import create_admin
from home.endpoints import home
from home.piccolo_app import APP_CONFIG
from home.tables import Task
from litestar import Litestar, asgi, delete, get, patch, post
from litestar.static_files import StaticFilesConfig
from litestar.template import TemplateConfig
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.contrib.piccolo_orm import PiccoloORMPlugin
from litestar.exceptions import NotFoundException
from litestar.static_files import StaticFilesConfig
from litestar.template import TemplateConfig
from litestar.types import Receive, Scope, Send
from piccolo.engine import engine_finder
from piccolo.utils.pydantic import create_pydantic_model
from piccolo_admin.endpoints import create_admin

from home.endpoints import home
from home.piccolo_app import APP_CONFIG
from home.tables import Task
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",
)


# mounting Piccolo Admin
Expand All @@ -22,29 +31,27 @@ async def admin(scope: "Scope", receive: "Receive", send: "Send") -> None:


@get("/tasks", tags=["Task"])
async def tasks() -> t.List[Task]:
tasks = await Task.select().order_by(Task.id, ascending=False)
return tasks
async def tasks() -> t.List[TaskModelOut]:
return await Task.select().order_by(Task.id, ascending=False)


@post("/tasks", tags=["Task"])
async def create_task(data: Task) -> Task:
task = Task(**data.to_dict())
async def create_task(data: TaskModelIn) -> TaskModelOut:
task = Task(**data.dict())
await task.save()
return task
return task.to_dict()


@patch("/tasks/{task_id:int}", tags=["Task"])
async def update_task(task_id: int, data: Task) -> Task:
async def update_task(task_id: int, data: TaskModelIn) -> TaskModelOut:
task = await Task.objects().get(Task.id == task_id)
if not task:
raise NotFoundException("Task does not exist")
for key, value in data.to_dict().items():
task.id = task_id
for key, value in data.dict().items():
setattr(task, key, value)

await task.save()
return task
return task.to_dict()


@delete("/tasks/{task_id:int}", tags=["Task"])
Expand Down Expand Up @@ -80,7 +87,6 @@ app = Litestar(
update_task,
delete_task,
],
plugins=[PiccoloORMPlugin()],
template_config=TemplateConfig(
directory="home/templates", engine=JinjaTemplateEngine
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ENVIRONMENT = jinja2.Environment(
)


@get(path="/", include_in_schema=False)
@get(path="/", include_in_schema=False, sync_to_thread=False)
def home(request: Request) -> Response:
template = ENVIRONMENT.get_template("home.html.jinja")
content = template.render(title="Piccolo + ASGI")
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Jinja2>=2.11.0
targ>=0.3.7
inflection>=0.5.1
typing-extensions>=4.3.0
pydantic[email]>=1.6
pydantic[email]>=1.6,<2.0

0 comments on commit fb3c8eb

Please sign in to comment.