Skip to content

Commit e2ff75e

Browse files
authored
Pydantic v2 update (#98)
* Pydantic v2 update. * Pydantic v2 update.
1 parent bccb1c4 commit e2ff75e

19 files changed

Lines changed: 95 additions & 92 deletions

File tree

calendar_backend/routes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -
103103

104104
app.add_middleware(
105105
DBSessionMiddleware,
106-
db_url=settings.DB_DSN,
106+
db_url=str(settings.DB_DSN),
107107
engine_args={"pool_pre_ping": True, "isolation_level": "AUTOCOMMIT"},
108108
)
109109
app.add_middleware(

calendar_backend/routes/event/comment.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
async def comment_event(event_id: int, comment: EventCommentPost) -> CommentEventGet:
1818
approve_status = ApproveStatuses.APPROVED if not settings.REQUIRE_REVIEW_EVENT_COMMENT else ApproveStatuses.PENDING
1919
comment_event = DbCommentEvent.create(
20-
event_id=event_id, session=db.session, **comment.dict(), approve_status=approve_status
20+
event_id=event_id, session=db.session, **comment.model_dump(), approve_status=approve_status
2121
)
2222
db.session.commit()
23-
return CommentEventGet.from_orm(comment_event)
23+
return CommentEventGet.model_validate(comment_event)
2424

2525

2626
@router.patch("/{id}", response_model=CommentEventGet)
@@ -30,17 +30,17 @@ async def update_comment(id: int, event_id: int, comment_inp: EventCommentPatch)
3030
raise ObjectNotFound(DbCommentEvent, id)
3131
if comment.approve_status is not ApproveStatuses.PENDING:
3232
raise ForbiddenAction(DbCommentEvent, id)
33-
comment_event = DbCommentEvent.update(id, session=db.session, **comment_inp.dict(exclude_unset=True))
33+
comment_event = DbCommentEvent.update(id, session=db.session, **comment_inp.model_dump(exclude_unset=True))
3434
db.session.commit()
35-
return CommentEventGet.from_orm(comment_event)
35+
return CommentEventGet.model_validate(comment_event)
3636

3737

3838
@router.get("/{id}", response_model=CommentEventGet)
3939
async def get_comment(id: int, event_id: int) -> CommentEventGet:
4040
comment = DbCommentEvent.get(id, session=db.session)
4141
if not comment.event_id == event_id or comment.approve_status != ApproveStatuses.APPROVED:
4242
raise ObjectNotFound(DbCommentEvent, id)
43-
return CommentEventGet.from_orm(comment)
43+
return CommentEventGet.model_validate(comment)
4444

4545

4646
@router.delete("/{id}", response_model=None)

calendar_backend/routes/event/comment_review.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from auth_lib.fastapi import UnionAuth
44
from fastapi import APIRouter, Depends
55
from fastapi_sqlalchemy import db
6-
from pydantic import parse_obj_as
6+
from pydantic import TypeAdapter
77

88
from calendar_backend.exceptions import ObjectNotFound
99
from calendar_backend.models import ApproveStatuses
@@ -25,7 +25,8 @@ async def get_unreviewed_comments(
2525
.filter(DbCommentEvent.event_id == event_id, DbCommentEvent.approve_status == ApproveStatuses.PENDING)
2626
.all()
2727
)
28-
return parse_obj_as(list[CommentEventGet], comments)
28+
adapter = TypeAdapter(list[CommentEventGet])
29+
return adapter.validate_python(comments)
2930

3031

3132
@router.post("/{id}/review/", response_model=CommentEventGet)
@@ -42,4 +43,4 @@ async def review_comment(
4243
if action == ApproveStatuses.DECLINED:
4344
DbCommentEvent.delete(comment.id, session=db.session)
4445
db.session.commit()
45-
return CommentEventGet.from_orm(comment)
46+
return CommentEventGet.model_validate(comment)

calendar_backend/routes/event/event.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fastapi import APIRouter, Depends, Query
77
from fastapi.responses import FileResponse
88
from fastapi_sqlalchemy import db
9-
from pydantic import parse_obj_as
9+
from pydantic import TypeAdapter
1010

1111
from calendar_backend.exceptions import NotEnoughCriteria
1212
from calendar_backend.methods import list_calendar
@@ -23,7 +23,7 @@
2323

2424
@router.get("/{id}", response_model=EventGet)
2525
async def get_event_by_id(id: int) -> EventGet:
26-
return EventGet.from_orm(Event.get(id, session=db.session))
26+
return EventGet.model_validate(Event.get(id, session=db.session))
2727

2828

2929
async def _get_timetable(start: date, end: date, group_id, lecturer_id, room_id, detail, limit, offset):
@@ -60,7 +60,7 @@ async def _get_timetable(start: date, end: date, group_id, lecturer_id, room_id,
6060
}
6161
]
6262

63-
return GetListEvent(items=events, limit=limit, offset=offset, total=cnt).dict(exclude=fmt)
63+
return GetListEvent(items=events, limit=limit, offset=offset, total=cnt).model_dump(exclude=fmt)
6464

6565

6666
@router.get("/", response_model=GetListEvent | None)
@@ -86,7 +86,7 @@ async def get_events(
8686

8787
@router.post("/", response_model=EventGet)
8888
async def create_event(event: EventPost, _=Depends(UnionAuth(scopes=["timetable.event.create"]))) -> EventGet:
89-
event_dict = event.dict()
89+
event_dict = event.model_dump()
9090
rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])]
9191
lecturers = [Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", [])]
9292
groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])]
@@ -98,7 +98,7 @@ async def create_event(event: EventPost, _=Depends(UnionAuth(scopes=["timetable.
9898
session=db.session,
9999
)
100100
db.session.commit()
101-
return EventGet.from_orm(event_get)
101+
return EventGet.model_validate(event_get)
102102

103103

104104
@router.post("/bulk", response_model=list[EventGet])
@@ -107,7 +107,7 @@ async def create_events(
107107
) -> list[EventGet]:
108108
result = []
109109
for event in events:
110-
event_dict = event.dict()
110+
event_dict = event.model_dump()
111111
rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])]
112112
lecturers = [Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", [])]
113113
groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])]
@@ -121,16 +121,17 @@ async def create_events(
121121
)
122122
)
123123
db.session.commit()
124-
return parse_obj_as(list[EventGet], result)
124+
adapter = TypeAdapter(list[EventGet])
125+
return adapter.validate_python(result)
125126

126127

127128
@router.patch("/{id}", response_model=EventGet)
128129
async def patch_event(
129130
id: int, event_inp: EventPatch, _=Depends(UnionAuth(scopes=["timetable.event.update"]))
130131
) -> EventGet:
131-
patched = Event.update(id, session=db.session, **event_inp.dict(exclude_unset=True))
132+
patched = Event.update(id, session=db.session, **event_inp.model_dump(exclude_unset=True))
132133
db.session.commit()
133-
return EventGet.from_orm(patched)
134+
return EventGet.model_validate(patched)
134135

135136

136137
@router.delete("/bulk", response_model=None)

calendar_backend/routes/group/group.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
@router.get("/{id}", response_model=GroupGet)
1818
async def get_group_by_id(id: int) -> GroupGet:
19-
return GroupGet.from_orm(Group.get(id, session=db.session))
19+
return GroupGet.model_validate(Group.get(id, session=db.session))
2020

2121

2222
@router.get("/", response_model=GetListGroup)
@@ -40,9 +40,9 @@ async def get_groups(query: str = "", limit: int = 10, offset: int = 0) -> GetLi
4040
async def create_group(group: GroupPost, _=Depends(UnionAuth(scopes=["timetable.group.create"]))) -> GroupGet:
4141
if db.session.query(Group).filter(Group.number == group.number).one_or_none():
4242
raise HTTPException(status_code=423, detail="Already exists")
43-
group = Group.create(**group.dict(), session=db.session)
43+
group = Group.create(**group.model_dump(), session=db.session)
4444
db.session.commit()
45-
return GroupGet.from_orm(group)
45+
return GroupGet.model_validate(group)
4646

4747

4848
@router.patch("/{id}", response_model=GroupGet)
@@ -56,9 +56,9 @@ async def patch_group(
5656
and query.id != id
5757
):
5858
raise HTTPException(status_code=423, detail="Already exists")
59-
patched = Group.update(id, **group_inp.dict(exclude_unset=True), session=db.session)
59+
patched = Group.update(id, **group_inp.model_dump(exclude_unset=True), session=db.session)
6060
db.session.commit()
61-
return GroupGet.from_orm(patched)
61+
return GroupGet.model_validate(patched)
6262

6363

6464
@router.delete("/{id}", response_model=None)

calendar_backend/routes/lecturer/comment.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ async def comment_lecturer(lecturer_id: int, comment: LecturerCommentPost) -> Co
2121
db_comment_lecturer = DbCommentLecturer.create(
2222
lecturer_id=lecturer_id,
2323
session=db.session,
24-
**comment.dict(),
24+
**comment.model_dump(),
2525
approve_status=approve_status,
2626
)
2727
db.session.commit()
28-
return CommentLecturer.from_orm(db_comment_lecturer)
28+
return CommentLecturer.model_validate(db_comment_lecturer)
2929

3030

3131
@router.patch("/comment/{id}", response_model=CommentLecturer)
@@ -35,9 +35,9 @@ async def update_comment_lecturer(id: int, lecturer_id: int, comment_inp: Lectur
3535
raise ObjectNotFound(DbCommentLecturer, id)
3636
if comment.approve_status is not ApproveStatuses.PENDING:
3737
raise ForbiddenAction(DbCommentLecturer, id)
38-
patched = DbCommentLecturer.update(id, session=db.session, **comment_inp.dict(exclude_unset=True))
38+
patched = DbCommentLecturer.update(id, session=db.session, **comment_inp.model_dump(exclude_unset=True))
3939
db.session.commit()
40-
return CommentLecturer.from_orm(patched)
40+
return CommentLecturer.model_validate(patched)
4141

4242

4343
@router.delete("/comment/{id}", response_model=None)
@@ -58,7 +58,7 @@ async def get_comment(id: int, lecturer_id: int) -> CommentLecturer:
5858
raise ObjectNotFound(DbCommentLecturer, id)
5959
if comment.approve_status is not None:
6060
raise ForbiddenAction(DbCommentLecturer, id)
61-
return CommentLecturer.from_orm(comment)
61+
return CommentLecturer.model_validate(comment)
6262

6363

6464
@router.get("/comment/", response_model=LecturerComments)

calendar_backend/routes/lecturer/comment_review.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from auth_lib.fastapi import UnionAuth
44
from fastapi import APIRouter, Depends
55
from fastapi_sqlalchemy import db
6-
from pydantic import parse_obj_as
6+
from pydantic import TypeAdapter
77

88
from calendar_backend.exceptions import ObjectNotFound
99
from calendar_backend.models.db import ApproveStatuses
@@ -25,7 +25,8 @@ async def get_unreviewed_comments(
2525
)
2626
.all()
2727
)
28-
return parse_obj_as(list[CommentLecturer], comments)
28+
adapter = TypeAdapter(list[CommentLecturer])
29+
return adapter.validate_python(comments)
2930

3031

3132
@router.post("/{id}/review/", response_model=CommentLecturer)
@@ -42,4 +43,4 @@ async def review_comment(
4243
if action == ApproveStatuses.DECLINED:
4344
DbCommentLecturer.delete(comment.id, session=db.session)
4445
db.session.commit()
45-
return CommentLecturer.from_orm(comment)
46+
return CommentLecturer.model_validate(comment)

calendar_backend/routes/lecturer/lecturer.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@router.get("/{id}", response_model=LecturerGet)
2323
async def get_lecturer_by_id(id: int) -> LecturerGet:
2424
lecturer = Lecturer.get(id, session=db.session)
25-
result = LecturerGet.from_orm(Lecturer.get(id, session=db.session))
25+
result = LecturerGet.model_validate(Lecturer.get(id, session=db.session))
2626
if lecturer.avatar_id:
2727
result.avatar_link = get_photo_webpath(lecturer.avatar.link)
2828
return result
@@ -49,7 +49,7 @@ async def get_lecturers(
4949

5050
result = []
5151
for row in query:
52-
row_get = LecturerGet.from_orm(row)
52+
row_get = LecturerGet.model_validate(row)
5353
if row.avatar:
5454
row_get.avatar_link = get_photo_webpath(row.avatar.link)
5555
result.append(row_get)
@@ -65,9 +65,9 @@ async def get_lecturers(
6565
async def create_lecturer(
6666
lecturer: LecturerPost, _=Depends(UnionAuth(scopes=["timetable.lecturer.create"]))
6767
) -> LecturerGet:
68-
dblecturer = Lecturer.create(session=db.session, **lecturer.dict())
68+
dblecturer = Lecturer.create(session=db.session, **lecturer.model_dump())
6969
db.session.commit()
70-
return LecturerGet.from_orm(dblecturer)
70+
return LecturerGet.model_validate(dblecturer)
7171

7272

7373
@router.patch("/{id}", response_model=LecturerGet)
@@ -79,12 +79,15 @@ async def patch_lecturer(
7979
if photo.lecturer_id != id or photo.approve_status != ApproveStatuses.APPROVED:
8080
raise ObjectNotFound(DbPhoto, lecturer_inp.avatar_id)
8181
lecturer_upd = Lecturer.update(
82-
id, session=db.session, **lecturer_inp.dict(exclude_unset=True), avatar_link=get_photo_webpath(photo.link)
82+
id,
83+
session=db.session,
84+
**lecturer_inp.model_dump(exclude_unset=True),
85+
avatar_link=get_photo_webpath(photo.link),
8386
)
8487
else:
85-
lecturer_upd = Lecturer.update(id, session=db.session, **lecturer_inp.dict(exclude_unset=True))
88+
lecturer_upd = Lecturer.update(id, session=db.session, **lecturer_inp.model_dump(exclude_unset=True))
8689
db.session.commit()
87-
return LecturerGet.from_orm(lecturer_upd)
90+
return LecturerGet.model_validate(lecturer_upd)
8891

8992

9093
@router.delete("/{id}", response_model=None)

calendar_backend/routes/lecturer/photo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def upload_photo(lecturer_id: int, photo: UploadFile = File(...)) -> Photo
2929
"""
3030
photo = await upload_lecturer_photo(lecturer_id, db.session, file=photo)
3131
db.session.commit()
32-
return Photo.from_orm(photo)
32+
return Photo.model_validate(photo)
3333

3434

3535
@router.get("/photo", response_model=LecturerPhotos)
@@ -68,4 +68,4 @@ async def get_photo(id: int, lecturer_id: int) -> Photo:
6868
photo = DbPhoto.get(id, session=db.session)
6969
if photo.lecturer_id != lecturer_id or photo.approve_status != ApproveStatuses.APPROVED:
7070
raise ObjectNotFound(DbPhoto, id)
71-
return Photo.from_orm(photo)
71+
return Photo.model_validate(photo)

calendar_backend/routes/lecturer/photo_review.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def get_unreviewed_photos(
5151

5252
result = []
5353
for row in query:
54-
get_row = Photo.from_orm(row)
54+
get_row = Photo.model_validate(row)
5555
get_row.link = get_photo_webpath(row.link)
5656
result.append(get_row)
5757

@@ -78,4 +78,4 @@ async def review_photo(
7878
if not photo.lecturer.avatar:
7979
photo.lecturer.avatar_id = photo.id
8080
db.session.flush()
81-
return Photo.from_orm(photo)
81+
return Photo.model_validate(photo)

0 commit comments

Comments
 (0)