Skip to content

Commit bec6ede

Browse files
committed
Update old schemas and routes to use new ORM layout
1 parent 7905143 commit bec6ede

File tree

16 files changed

+161
-156
lines changed

16 files changed

+161
-156
lines changed

api/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from starlette.middleware.authentication import AuthenticationMiddleware
33

44
from api.middleware import TokenAuthentication, on_auth_error
5-
from api.routers.old import old_routes_router
5+
from api.routers.v1 import v1_routes_router
66
from api.settings import Server
77

88
app = FastAPI(redoc_url="/", docs_url="/swagger")
@@ -13,4 +13,4 @@
1313
on_error=on_auth_error,
1414
)
1515

16-
app.include_router(old_routes_router)
16+
app.include_router(v1_routes_router)

api/models/orm/infraction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Infraction(Base):
1717
user_id: Mapped[int] = mapped_column(ForeignKey("users.user_id"))
1818
issued_in_jam_id: Mapped[int] = mapped_column(ForeignKey("jams.jam_id"))
1919
infraction_type: Mapped[InfractionType] = mapped_column(
20-
Enum(*InfractionType.__args__, name="infraction_type_enum"),
20+
Enum(*InfractionType.__args__, name="infraction_type"),
2121
nullable=False,
2222
)
2323
reason: Mapped[str] = mapped_column(String(), nullable=False)

api/models/schemas/old/infraction.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

api/models/schemas/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def discord_ids_must_be_snowflake(field_to_check: int) -> int:
2+
"""Ensure the ids are valid Discord snowflakes."""
3+
if field_to_check and field_to_check.bit_length() > 64:
4+
raise ValueError("Field must fit within a 64 bit int.")
5+
return field_to_check

api/models/schemas/v1/infraction.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pydantic import BaseModel, validator
2+
3+
from api.models.orm.infraction import InfractionType
4+
from api.models.schemas.utils import discord_ids_must_be_snowflake
5+
6+
7+
class InfractionBase(BaseModel):
8+
"""Base model for all infraction types."""
9+
10+
user_id: int
11+
jam_id: int
12+
reason: str
13+
infraction_type: InfractionType
14+
15+
# validators
16+
_ensure_valid_discord_id = validator("user_id", allow_reuse=True)(discord_ids_must_be_snowflake)
17+
18+
19+
class InfractionCreate(InfractionBase):
20+
"""The expected fields to create a new infraction."""
21+
22+
23+
class Infraction(InfractionBase):
24+
"""A model representing an infraction."""
25+
26+
id: int
27+
28+
class Config:
29+
"""Sets ORM mode to true so that pydantic will validate the objects returned by SQLAlchemy."""
30+
31+
orm_mode = True

api/models/schemas/old/jam.py renamed to api/models/schemas/v1/jam.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
from pydantic import BaseModel
22

3-
from api.models.schemas.old import infraction, team, winner
3+
from api.models.schemas.v1 import infraction, team, winner
44

55

6-
class CodeJam(BaseModel):
7-
"""A model representing a codejam."""
6+
class CodeJamBase(BaseModel):
7+
"""A Base model representing a codejam."""
88

99
name: str
1010
teams: list[team.Team]
1111
ongoing: bool = False
1212

1313

14-
class CodeJamResponse(CodeJam):
14+
class CodeJamCreate(CodeJamBase):
15+
"""The expected fields to create a new Code Jam."""
16+
17+
18+
class CodeJam(CodeJamBase):
1519
"""Response model representing a code jam."""
1620

1721
id: int
18-
teams: list[team.TeamResponse]
19-
infractions: list[infraction.InfractionResponse]
22+
infractions: list[infraction.Infraction]
2023
winners: list[winner.Winner]
2124

2225
class Config:

api/models/schemas/old/team.py renamed to api/models/schemas/v1/team.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
from pydantic import BaseModel
44

5-
from api.models.schemas.old import user
5+
from api.models.schemas.v1 import user
66

77

8-
class Team(BaseModel):
9-
"""A model representing a team for a codejam."""
8+
class TeamBase(BaseModel):
9+
"""A Base model representing a team for a codejam."""
1010

1111
name: str
1212
users: list[user.User]
1313
discord_role_id: Optional[int] = None
1414
discord_channel_id: Optional[int] = None
1515

1616

17-
class TeamResponse(Team):
17+
class Team(TeamBase):
1818
"""Response model representing a team."""
1919

2020
id: int
@@ -26,11 +26,11 @@ class Config:
2626
orm_mode = True
2727

2828

29-
class UserTeamResponse(BaseModel):
30-
"""Response model representing user and team relationship."""
29+
class UserTeam(BaseModel):
30+
"""A model representing user and team relationship."""
3131

3232
user_id: int
33-
team: TeamResponse
33+
team: Team
3434
is_leader: bool
3535

3636
class Config:

api/models/schemas/old/user.py renamed to api/models/schemas/v1/user.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
from pydantic import BaseModel
22

3-
from api.models.schemas.old import infraction
4-
5-
6-
class User(BaseModel):
7-
"""A model representing a user for a codejam."""
8-
9-
user_id: int
10-
is_leader: bool
11-
12-
class Config:
13-
"""Sets ORM mode to true so that pydantic will validate the objects returned by SQLAlchemy."""
14-
15-
orm_mode = True
3+
from api.models.schemas.v1 import infraction
164

175

186
class ParticipationHistory(BaseModel):
@@ -23,18 +11,23 @@ class ParticipationHistory(BaseModel):
2311
first_place: bool
2412
team_id: int
2513
is_leader: bool
26-
infractions: list[infraction.InfractionResponse]
14+
infractions: list[infraction.Infraction]
2715

2816
class Config:
2917
"""Sets ORM mode to true so that pydantic will validate the objects returned by SQLAlchemy."""
3018

3119
orm_mode = True
3220

3321

34-
class UserResponse(BaseModel):
35-
"""Response model representing a user."""
22+
class UserBase(BaseModel):
23+
"""A Base model representing core data about a user."""
3624

3725
id: int
26+
27+
28+
class User(UserBase):
29+
"""Response model representing everything about a user."""
30+
3831
participation_history: list[ParticipationHistory]
3932

4033
class Config:
File renamed without changes.

api/routers/old/__init__.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)