Skip to content

Commit 44eec30

Browse files
committed
Fixed post_gen and fastapi-users integration.
1 parent 1ba16ce commit 44eec30

7 files changed

Lines changed: 68 additions & 8 deletions

File tree

fastapi_template/template/hooks/post_gen_project.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def run_cmd(cmd: str, ignore_error: bool = False):
8181
cprint(out.stdout.decode(errors="replace"), "red")
8282
if out.stderr:
8383
cprint(out.stderr.decode(errors="replace"), "red")
84-
exit(1)
84+
raise ValueError()
8585

8686

8787
def init_repo():
@@ -102,4 +102,7 @@ def init_repo():
102102
if __name__ == "__main__":
103103
delete_resources_for_disabled_features()
104104
replace_resources()
105-
init_repo()
105+
try:
106+
init_repo()
107+
except ValueError:
108+
pass

fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ name = "User models"
123123
enabled = "{{cookiecutter.add_users}}"
124124
resources = [
125125
"{{cookiecutter.project_name}}/web/api/users",
126-
"{{cookiecutter.project_name}}/db_sa/models/users.py"
126+
"{{cookiecutter.project_name}}/db_sa/models/users.py",
127+
"{{cookiecutter.project_name}}/db_sa/migrations/versions/2026-05-05-14-37_8caca4abd7b4.py",
127128
]
128129

129130
[[features]]

fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ lint.ignore = [
290290
"ANN401", # typing.Any are disallowed in `**kwargs
291291
"PLR0913", # Too many arguments for function call
292292
"D106", # Missing docstring in public nested class
293+
"UP043", # Unnecessary default argument specified (conflicts with mypy).
293294
]
294295
exclude = [
295296
{%- if cookiecutter.orm in ["ormar", "sqlalchemy", "piccolo", "tortoise"] %}

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_sa/migrations/script.py.mako

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ Create Date: ${create_date}
77
"""
88
from alembic import op
99
import sqlalchemy as sa
10+
import fastapi_users_db_sqlalchemy.generics
11+
import fastapi_users_db_sqlalchemy
1012
${imports if imports else ""}
1113

14+
1215
# revision identifiers, used by Alembic.
1316
revision = ${repr(up_revision)}
1417
down_revision = ${repr(down_revision)}

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_sa/migrations/versions/2021-08-16-16-55_2b7380507a71.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def upgrade() -> None:
2020
op.create_table(
2121
"dummy_model",
2222
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
23-
sa.Column("name", sa.String(length=200), nullable=True),
23+
sa.Column("name", sa.String(length=200), nullable=False),
2424
sa.PrimaryKeyConstraint("id"),
2525
)
2626
# ### end Alembic commands ###
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Added users models.
2+
3+
Revision ID: 8caca4abd7b4
4+
{%- if cookiecutter.add_dummy == 'True' %}
5+
Revises: 2b7380507a71
6+
{%- else %}
7+
Revises: 819cbf6e030b
8+
{%- endif %}
9+
Create Date: 2026-05-05 14:37:30.629187
10+
11+
"""
12+
13+
import sqlalchemy as sa
14+
from alembic import op
15+
import fastapi_users_db_sqlalchemy.generics
16+
import fastapi_users_db_sqlalchemy
17+
18+
# revision identifiers, used by Alembic.
19+
revision = "8caca4abd7b4"
20+
down_revision = "2b7380507a71"
21+
{%- if cookiecutter.add_dummy == 'True' %}
22+
down_revision = "2b7380507a71"
23+
{%- else %}
24+
down_revision = "819cbf6e030b"
25+
{%- endif %}
26+
branch_labels = None
27+
depends_on = None
28+
29+
30+
def upgrade() -> None:
31+
"""Run the migration."""
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.create_table(
34+
"user",
35+
sa.Column("id", fastapi_users_db_sqlalchemy.generics.GUID(), nullable=False),
36+
sa.Column("email", sa.String(length=320), nullable=False),
37+
sa.Column("hashed_password", sa.String(length=1024), nullable=False),
38+
sa.Column("is_active", sa.Boolean(), nullable=False),
39+
sa.Column("is_superuser", sa.Boolean(), nullable=False),
40+
sa.Column("is_verified", sa.Boolean(), nullable=False),
41+
sa.PrimaryKeyConstraint("id"),
42+
)
43+
op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True)
44+
# ### end Alembic commands ###
45+
46+
47+
def downgrade() -> None:
48+
"""Undo the migration."""
49+
# ### commands auto generated by Alembic - please adjust! ###
50+
op.drop_index(op.f("ix_user_email"), table_name="user")
51+
op.drop_table("user")
52+
# ### end Alembic commands ###

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_sa/models/users.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# type: ignore
1+
from typing import AsyncGenerator
22
import uuid
33

44
from fastapi import Depends
@@ -40,7 +40,7 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
4040
verification_token_secret = settings.users_secret
4141

4242

43-
async def get_user_db(session: AsyncSession = Depends(get_db_session)) -> SQLAlchemyUserDatabase:
43+
async def get_user_db(session: AsyncSession = Depends(get_db_session)) -> AsyncGenerator[SQLAlchemyUserDatabase[User, uuid.UUID], None]:
4444
"""
4545
Yield a SQLAlchemyUserDatabase instance.
4646
@@ -50,7 +50,7 @@ async def get_user_db(session: AsyncSession = Depends(get_db_session)) -> SQLAlc
5050
yield SQLAlchemyUserDatabase(session, User)
5151

5252

53-
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)) -> UserManager:
53+
async def get_user_manager(user_db: SQLAlchemyUserDatabase[User, uuid.UUID] = Depends(get_user_db)) -> AsyncGenerator[UserManager, None]:
5454
"""
5555
Yield a UserManager instance.
5656
@@ -60,7 +60,7 @@ async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db
6060
yield UserManager(user_db)
6161

6262

63-
def get_jwt_strategy() -> JWTStrategy:
63+
def get_jwt_strategy() -> JWTStrategy[User, uuid.UUID]:
6464
"""
6565
Return a JWTStrategy in order to instantiate it dynamically.
6666

0 commit comments

Comments
 (0)