Skip to content

Commit 79b72ec

Browse files
committed
Fixed ruff errors
1 parent 72edb24 commit 79b72ec

28 files changed

+207
-81
lines changed

backend/app/alembic/__init__.py

Whitespace-only changes.

backend/app/alembic/env.py

100755100644
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
"""Alembic configuration for database migrations."""
12
from logging.config import fileConfig
23

34
from alembic import context
45
from sqlalchemy import engine_from_config, pool
6+
from sqlmodel import SQLModel
7+
8+
from app.core.config import settings
59

610
# this is the Alembic Config object, which provides
711
# access to the values within the .ini file in use.
@@ -12,14 +16,11 @@
1216
if config.config_file_name:
1317
fileConfig(config.config_file_name)
1418

15-
16-
from app.core.config import settings # noqa
17-
from sqlmodel import SQLModel
18-
1919
target_metadata = SQLModel.metadata
2020

2121

2222
def get_url() -> str:
23+
"""Get database URL from settings."""
2324
return str(settings.SQLALCHEMY_DATABASE_URI)
2425

2526

backend/app/alembic/versions/1a31ce608336_add_cascade_delete_relationships.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Add cascade delete relationships
1+
"""Add cascade delete relationships.
22
33
Revision ID: 1a31ce608336
44
Revises: d98dd8ec85a3
@@ -17,8 +17,11 @@
1717

1818

1919
def upgrade() -> None:
20+
"""Upgrade database schema."""
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
op.alter_column("item", "owner_id", existing_type=sa.UUID(), nullable=False)
22+
op.alter_column(
23+
"item", "owner_id", existing_type=sa.UUID(), nullable=False,
24+
)
2225
op.drop_constraint("item_owner_id_fkey", "item", type_="foreignkey")
2326
op.create_foreign_key(
2427
None, "item", "user", ["owner_id"], ["id"], ondelete="CASCADE",
@@ -27,8 +30,11 @@ def upgrade() -> None:
2730

2831

2932
def downgrade() -> None:
33+
"""Downgrade database schema."""
3034
# ### commands auto generated by Alembic - please adjust! ###
3135
op.drop_constraint("item_owner_id_fkey", "item", type_="foreignkey")
32-
op.create_foreign_key("item_owner_id_fkey", "item", "user", ["owner_id"], ["id"])
36+
op.create_foreign_key(
37+
"item_owner_id_fkey", "item", "user", ["owner_id"], ["id"],
38+
)
3339
op.alter_column("item", "owner_id", existing_type=sa.UUID(), nullable=True)
3440
# ### end Alembic commands ###

backend/app/alembic/versions/9c0a54914c78_add_max_length_for_string_varchar_.py

100755100644
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Add max length for string(varchar) fields in User and Items models
1+
"""Add max length for string(varchar) fields in User and Items models.
22
33
Revision ID: 9c0a54914c78
44
Revises: e2412789c190
@@ -17,6 +17,7 @@
1717

1818

1919
def upgrade() -> None:
20+
"""Upgrade database schema."""
2021
# Adjust the length of the email field in the User table
2122
op.alter_column(
2223
"user",
@@ -55,6 +56,7 @@ def upgrade() -> None:
5556

5657

5758
def downgrade() -> None:
59+
"""Downgrade database schema."""
5860
# Revert the length of the email field in the User table
5961
op.alter_column(
6062
"user",

backend/app/alembic/versions/__init__.py

Whitespace-only changes.

backend/app/alembic/versions/d98dd8ec85a3_edit_replace_id_integers_in_all_models_.py

100755100644
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Edit replace id integers in all models to use UUID instead
1+
"""Edit replace id integers in all models to use UUID instead.
22
33
Revision ID: d98dd8ec85a3
44
Revises: 9c0a54914c78
@@ -18,6 +18,7 @@
1818

1919

2020
def upgrade() -> None:
21+
"""Upgrade database schema."""
2122
# Ensure uuid-ossp extension is available
2223
op.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
2324

@@ -39,14 +40,18 @@ def upgrade() -> None:
3940
),
4041
)
4142
op.add_column(
42-
"item", sa.Column("new_owner_id", postgresql.UUID(as_uuid=True), nullable=True),
43+
"item",
44+
sa.Column(
45+
"new_owner_id", postgresql.UUID(as_uuid=True), nullable=True,
46+
),
4347
)
4448

4549
# Populate the new columns with UUIDs
4650
op.execute('UPDATE "user" SET new_id = uuid_generate_v4()')
4751
op.execute("UPDATE item SET new_id = uuid_generate_v4()")
4852
op.execute(
49-
'UPDATE item SET new_owner_id = (SELECT new_id FROM "user" WHERE "user".id = item.owner_id)',
53+
'UPDATE item SET new_owner_id = '
54+
'(SELECT new_id FROM "user" WHERE "user".id = item.owner_id)',
5055
)
5156

5257
# Set the new_id as not nullable
@@ -69,10 +74,13 @@ def upgrade() -> None:
6974
op.create_primary_key("item_pkey", "item", ["id"])
7075

7176
# Recreate foreign key constraint
72-
op.create_foreign_key("item_owner_id_fkey", "item", "user", ["owner_id"], ["id"])
77+
op.create_foreign_key(
78+
"item_owner_id_fkey", "item", "user", ["owner_id"], ["id"],
79+
)
7380

7481

7582
def downgrade() -> None:
83+
"""Downgrade database schema."""
7684
# Reverse the upgrade process
7785
op.add_column("user", sa.Column("old_id", sa.Integer, autoincrement=True))
7886
op.add_column("item", sa.Column("old_id", sa.Integer, autoincrement=True))
@@ -81,22 +89,28 @@ def downgrade() -> None:
8189
# Populate the old columns with default values
8290
# Generate sequences for the integer IDs if not exist
8391
op.execute(
84-
'CREATE SEQUENCE IF NOT EXISTS user_id_seq AS INTEGER OWNED BY "user".old_id',
92+
'CREATE SEQUENCE IF NOT EXISTS user_id_seq AS INTEGER '
93+
'OWNED BY "user".old_id',
8594
)
8695
op.execute(
87-
"CREATE SEQUENCE IF NOT EXISTS item_id_seq AS INTEGER OWNED BY item.old_id",
96+
"CREATE SEQUENCE IF NOT EXISTS item_id_seq AS INTEGER "
97+
"OWNED BY item.old_id",
8898
)
8999

90100
op.execute(
91-
"SELECT setval('user_id_seq', COALESCE((SELECT MAX(old_id) + 1 FROM \"user\"), 1), false)",
101+
"SELECT setval('user_id_seq', "
102+
'COALESCE((SELECT MAX(old_id) + 1 FROM "user"), 1), false)',
92103
)
93104
op.execute(
94-
"SELECT setval('item_id_seq', COALESCE((SELECT MAX(old_id) + 1 FROM item), 1), false)",
105+
"SELECT setval('item_id_seq', "
106+
"COALESCE((SELECT MAX(old_id) + 1 FROM item), 1), false)",
95107
)
96108

97109
op.execute("UPDATE \"user\" SET old_id = nextval('user_id_seq')")
98110
op.execute(
99-
'UPDATE item SET old_id = nextval(\'item_id_seq\'), old_owner_id = (SELECT old_id FROM "user" WHERE "user".id = item.owner_id)',
111+
'UPDATE item SET old_id = nextval(\'item_id_seq\'), '
112+
'old_owner_id = (SELECT old_id FROM "user" '
113+
'WHERE "user".id = item.owner_id)',
100114
)
101115

102116
# Drop new columns and rename old columns back
@@ -115,4 +129,6 @@ def downgrade() -> None:
115129
op.create_primary_key("item_pkey", "item", ["id"])
116130

117131
# Recreate foreign key constraint
118-
op.create_foreign_key("item_owner_id_fkey", "item", "user", ["owner_id"], ["id"])
132+
op.create_foreign_key(
133+
"item_owner_id_fkey", "item", "user", ["owner_id"], ["id"],
134+
)

backend/app/alembic/versions/e2412789c190_initialize_models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Initialize models
1+
"""Initialize models.
22
33
Revision ID: e2412789c190
44
Revises:
@@ -18,13 +18,16 @@
1818

1919

2020
def upgrade() -> None:
21+
"""Upgrade database schema."""
2122
# ### commands auto generated by Alembic - please adjust! ###
2223
op.create_table(
2324
"user",
2425
sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
2526
sa.Column("is_active", sa.Boolean(), nullable=False),
2627
sa.Column("is_superuser", sa.Boolean(), nullable=False),
27-
sa.Column("full_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
28+
sa.Column(
29+
"full_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True,
30+
),
2831
sa.Column("id", sa.Integer(), nullable=False),
2932
sa.Column(
3033
"hashed_password",
@@ -36,7 +39,9 @@ def upgrade() -> None:
3639
op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True)
3740
op.create_table(
3841
"item",
39-
sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
42+
sa.Column(
43+
"description", sqlmodel.sql.sqltypes.AutoString(), nullable=True,
44+
),
4045
sa.Column("id", sa.Integer(), nullable=False),
4146
sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
4247
sa.Column("owner_id", sa.Integer(), nullable=False),
@@ -50,6 +55,7 @@ def upgrade() -> None:
5055

5156

5257
def downgrade() -> None:
58+
"""Downgrade database schema."""
5359
# ### commands auto generated by Alembic - please adjust! ###
5460
op.drop_table("item")
5561
op.drop_index(op.f("ix_user_email"), table_name="user")

backend/app/api/deps.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""API dependency functions."""
2+
13
from collections.abc import Generator
24
from typing import Annotated
35

@@ -19,6 +21,7 @@
1921

2022

2123
def get_db() -> Generator[Session]:
24+
"""Get database session."""
2225
with Session(engine) as session:
2326
yield session
2427

@@ -28,6 +31,7 @@ def get_db() -> Generator[Session]:
2831

2932

3033
def get_current_user(session: SessionDep, token: TokenDep) -> User:
34+
"""Get current user from JWT token."""
3135
try:
3236
payload = jwt.decode(
3337
token,
@@ -39,7 +43,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
3943
raise HTTPException(
4044
status_code=status.HTTP_403_FORBIDDEN,
4145
detail="Could not validate credentials",
42-
)
46+
) from None
4347
user = session.get(User, token_data.sub)
4448
if not user:
4549
raise HTTPException(status_code=404, detail="User not found")
@@ -52,6 +56,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
5256

5357

5458
def get_current_active_superuser(current_user: CurrentUser) -> User:
59+
"""Get current active superuser."""
5560
if not current_user.is_superuser:
5661
raise HTTPException(
5762
status_code=403,

backend/app/api/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""API router configuration."""
2+
13
from fastapi import APIRouter
24

35
from app.api.routes import items, login, private, users, utils

backend/app/api/routes/items.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Item management API endpoints."""
2+
13
import uuid
24

35
# Removed unused Any import
@@ -10,7 +12,7 @@
1012
router = APIRouter(prefix="/items", tags=["items"])
1113

1214

13-
@router.get("/", response_model=ItemsPublic)
15+
@router.get("/")
1416
def read_items(
1517
session: SessionDep,
1618
current_user: CurrentUser,
@@ -41,20 +43,20 @@ def read_items(
4143
return ItemsPublic(data=items, count=count)
4244

4345

44-
@router.get("/{id}", response_model=ItemPublic)
46+
@router.get("/{item_id}")
4547
def read_item(
46-
session: SessionDep, current_user: CurrentUser, id: uuid.UUID,
48+
session: SessionDep, current_user: CurrentUser, item_id: uuid.UUID,
4749
) -> ItemPublic:
4850
"""Get item by ID."""
49-
item = session.get(Item, id)
51+
item = session.get(Item, item_id)
5052
if not item:
5153
raise HTTPException(status_code=404, detail="Item not found")
5254
if not current_user.is_superuser and (item.owner_id != current_user.id):
5355
raise HTTPException(status_code=400, detail="Not enough permissions")
5456
return ItemPublic.model_validate(item)
5557

5658

57-
@router.post("/", response_model=ItemPublic)
59+
@router.post("/")
5860
def create_item(
5961
*,
6062
session: SessionDep,
@@ -69,16 +71,16 @@ def create_item(
6971
return ItemPublic.model_validate(item)
7072

7173

72-
@router.put("/{id}", response_model=ItemPublic)
74+
@router.put("/{item_id}")
7375
def update_item(
7476
*,
7577
session: SessionDep,
7678
current_user: CurrentUser,
77-
id: uuid.UUID,
79+
item_id: uuid.UUID,
7880
item_in: ItemUpdate,
7981
) -> ItemPublic:
8082
"""Update an item."""
81-
item = session.get(Item, id)
83+
item = session.get(Item, item_id)
8284
if not item:
8385
raise HTTPException(status_code=404, detail="Item not found")
8486
if not current_user.is_superuser and (item.owner_id != current_user.id):
@@ -91,14 +93,14 @@ def update_item(
9193
return ItemPublic.model_validate(item)
9294

9395

94-
@router.delete("/{id}")
96+
@router.delete("/{item_id}")
9597
def delete_item(
9698
session: SessionDep,
9799
current_user: CurrentUser,
98-
id: uuid.UUID,
100+
item_id: uuid.UUID,
99101
) -> Message:
100102
"""Delete an item."""
101-
item = session.get(Item, id)
103+
item = session.get(Item, item_id)
102104
if not item:
103105
raise HTTPException(status_code=404, detail="Item not found")
104106
if not current_user.is_superuser and (item.owner_id != current_user.id):

0 commit comments

Comments
 (0)