Skip to content

Commit

Permalink
support pydantic v1
Browse files Browse the repository at this point in the history
  • Loading branch information
usefulalgorithm committed Nov 15, 2023
1 parent 023214e commit 8bd22d1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Options:
-n, --name TEXT Name of your awesome project
-V, --version Prints current version
--force Owerrite directory if it exists
--quite Do not ask for features during generation
--quiet Do not ask for features during generation
--api-type [rest|graphql] Select API type for your application
--db [none|sqlite|mysql|postgresql|mongodb]
Select a database for your app
Expand Down
8 changes: 4 additions & 4 deletions fastapi_template/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def disable_orm(ctx: BuilderContext) -> MenuEntry:
return None


def do_not_ask_features_if_quite(ctx: BuilderContext) -> Optional[List[MenuEntry]]:
if ctx.quite:
def do_not_ask_features_if_quiet(ctx: BuilderContext) -> Optional[List[MenuEntry]]:
if ctx.quiet:
return [SKIP_ENTRY]
return None

Expand Down Expand Up @@ -342,7 +342,7 @@ def checker(ctx: BuilderContext) -> bool:
code="features",
description="Additional project features",
multiselect=True,
before_ask=do_not_ask_features_if_quite,
before_ask=do_not_ask_features_if_quiet,
entries=[
MenuEntry(
code="pydanticv1",
Expand Down Expand Up @@ -627,7 +627,7 @@ def run_command(callback: Callable[[BuilderContext], None]) -> None:
help="Owerrite directory if it exists",
),
Option(
["--quite"],
["--quiet"],
is_flag=True,
help="Do not ask for features during generation",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ async def setup_db() -> AsyncGenerator[None, None]:
:yield: nothing.
"""
client = AsyncIOMotorClient(settings.db_url.human_repr())
client = AsyncIOMotorClient(settings.db_url.human_repr()) # type: ignore
from {{cookiecutter.project_name}}.db.models import load_all_models # noqa: WPS433
await beanie.init_beanie(
database=client[settings.db_base],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

{%- endif %}
{%- if cookiecutter.db_info.name == "mongodb" %}
{%- if cookiecutter.pydanticv1 == "True" %}
from pydantic import validator
{%- else %}
from pydantic import field_validator
{%- endif %}
from bson import ObjectId
{%- endif %}

Expand All @@ -25,8 +29,12 @@ class DummyModelDTO(BaseModel):
name: str

{%- if cookiecutter.db_info.name == "mongodb" %}
{%- if cookiecutter.pydanticv1 == "True" %}
@validator("id")
{%- else %}
@field_validator("id", mode="before")
@classmethod
{%- endif %}
def parse_object_id(cls, document_id: ObjectId) -> str:
"""
Validator that converts `ObjectId` to json serializable `str`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _setup_db(app: FastAPI) -> None: # pragma: no cover
from motor.motor_asyncio import AsyncIOMotorClient
from {{cookiecutter.project_name}}.db.models import load_all_models
async def _setup_db(app: FastAPI) -> None:
client = AsyncIOMotorClient(str(settings.db_url))
client = AsyncIOMotorClient(str(settings.db_url)) # type: ignore
app.state.db_client = client
await beanie.init_beanie(
database=client[settings.db_base],
Expand Down

0 comments on commit 8bd22d1

Please sign in to comment.