Skip to content

Commit

Permalink
Fix Pydantic deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavberi committed Sep 14, 2024
1 parent 608ca7e commit ca904a2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
6 changes: 0 additions & 6 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ def check_end_year(cls, value, info: ValidationInfo):
raise ValueError("Start date cannot be same/after end date")
return value

# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information. # noqa: E501
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
json_encoders={ObjectId: str},
)


Expand All @@ -76,12 +73,9 @@ class Holiday(BaseModel):
default_factory=lambda: datetime.now(timezone), frozen=True
)

# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information. # noqa: E501
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
json_encoders={ObjectId: str},
str_max_length=5000,
extra="forbid",
str_strip_whitespace=True,
Expand Down
18 changes: 10 additions & 8 deletions mutations/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import strawberry
from fastapi.encoders import jsonable_encoder
from prettytable import PrettyTable
from pydantic import HttpUrl, parse_obj_as
from pydantic import HttpUrl, TypeAdapter

from db import eventsdb
from mailing import triggerMail
Expand Down Expand Up @@ -86,7 +86,9 @@ def createEvent(details: InputEventDetails, info: Info) -> EventType:
if details.audience is not None:
event_instance.audience = [Audience(aud) for aud in details.audience]
if details.link is not None:
event_instance.link = parse_obj_as(HttpUrl, details.link)
event_instance.link = TypeAdapter(HttpUrl).validate_python(
details.link
)
if details.equipment is not None:
event_instance.equipment = details.equipment
if details.additional is not None:
Expand Down Expand Up @@ -136,7 +138,7 @@ def createEvent(details: InputEventDetails, info: Info) -> EventType:
created_id = eventsdb.insert_one(
jsonable_encoder(event_instance)
).inserted_id
created_event = Event.parse_obj(eventsdb.find_one({"_id": created_id}))
created_event = Event.model_validate(eventsdb.find_one({"_id": created_id}))

return EventType.from_pydantic(created_event)

Expand Down Expand Up @@ -253,7 +255,7 @@ def editEvent(details: InputEditEventDetails, info: Info) -> EventType:
raise Exception("You do not have permission to access this resource.")

event_ref = eventsdb.find_one({"_id": details.eventid})
return EventType.from_pydantic(Event.parse_obj(event_ref))
return EventType.from_pydantic(Event.model_validate(event_ref))


@strawberry.mutation
Expand Down Expand Up @@ -287,7 +289,7 @@ def progressEvent(
event_ref = eventsdb.find_one({"_id": eventid})
if event_ref is None or user is None:
raise noaccess_error
event_instance = Event.parse_obj(event_ref)
event_instance = Event.model_validate(event_ref)

# get current time
current_time = datetime.now(timezone)
Expand Down Expand Up @@ -425,7 +427,7 @@ def progressEvent(
raise noaccess_error

event_ref = eventsdb.find_one({"_id": eventid})
updated_event_instance = Event.parse_obj(event_ref)
updated_event_instance = Event.model_validate(event_ref)

# trigger mail notification

Expand Down Expand Up @@ -654,7 +656,7 @@ def deleteEvent(eventid: str, info: Info) -> EventType:
raise Exception(
"Can not access event. Either it does not exist or user does not have perms." # noqa: E501
)
event_instance = Event.parse_obj(event_ref)
event_instance = Event.model_validate(event_ref)

updation = event_ref["status"]
updation["state"] = Event_State_Status.deleted.value
Expand Down Expand Up @@ -753,7 +755,7 @@ def deleteEvent(eventid: str, info: Info) -> EventType:
)

event_ref = eventsdb.find_one({"_id": eventid})
return EventType.from_pydantic(Event.parse_obj(event_ref))
return EventType.from_pydantic(Event.model_validate(event_ref))


@strawberry.mutation
Expand Down
4 changes: 2 additions & 2 deletions mutations/holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def createHoliday(details: InputHolidayDetails, info: Info) -> HolidayType:
created_id = holidaysdb.insert_one(jsonable_encoder(holiday)).inserted_id
created_holiday = holidaysdb.find_one({"_id": created_id})

return HolidayType.from_pydantic(Holiday.parse_obj(created_holiday))
return HolidayType.from_pydantic(Holiday.model_validate(created_holiday))


@strawberry.mutation
Expand Down Expand Up @@ -63,7 +63,7 @@ def editHoliday(
)
updated_holiday = holidaysdb.find_one({"_id": id})

return HolidayType.from_pydantic(Holiday.parse_obj(updated_holiday))
return HolidayType.from_pydantic(Holiday.model_validate(updated_holiday))


@strawberry.mutation
Expand Down
12 changes: 6 additions & 6 deletions queries/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def event(eventid: str, info: Info) -> EventType:
):
trim_public_events(event)

return EventType.from_pydantic(Event.parse_obj(event))
return EventType.from_pydantic(Event.model_validate(event))


@strawberry.field
Expand Down Expand Up @@ -175,7 +175,7 @@ def events(
trim_public_events(event)

return [
EventType.from_pydantic(Event.parse_obj(event)) for event in events
EventType.from_pydantic(Event.model_validate(event)) for event in events
]


Expand Down Expand Up @@ -205,7 +205,7 @@ def incompleteEvents(clubid: str, info: Info) -> List[EventType]:
)

return [
EventType.from_pydantic(Event.parse_obj(event)) for event in events
EventType.from_pydantic(Event.model_validate(event)) for event in events
]


Expand Down Expand Up @@ -248,7 +248,7 @@ def incompleteEvents(clubid: str, info: Info) -> List[EventType]:
# TODO: Add trimming of events as public events

# return [
# EventType.from_pydantic(Event.parse_obj(event)) for event in events
# EventType.from_pydantic(Event.model_validate(event)) for event in events
# ]


Expand Down Expand Up @@ -303,7 +303,7 @@ def pendingEvents(clubid: str | None, info: Info) -> List[EventType]:
)

return [
EventType.from_pydantic(Event.parse_obj(event)) for event in events
EventType.from_pydantic(Event.model_validate(event)) for event in events
]


Expand Down Expand Up @@ -345,7 +345,7 @@ def availableRooms(
free_rooms.update(event["location"])

return RoomListType.from_pydantic(
RoomList.parse_obj({"locations": free_rooms})
RoomList.model_validate({"locations": free_rooms})
)


Expand Down
4 changes: 2 additions & 2 deletions queries/holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def holidays(
holidays = holidaysdb.find(query)

return [
HolidayType.from_pydantic(Holiday.parse_obj(holiday))
HolidayType.from_pydantic(Holiday.model_validate(holiday))
for holiday in holidays
]

Expand All @@ -44,7 +44,7 @@ def holiday(id: str) -> HolidayType:

holiday = holidaysdb.find_one({"_id": id})

return HolidayType.from_pydantic(Holiday.parse_obj(holiday))
return HolidayType.from_pydantic(Holiday.model_validate(holiday))


# register all queries of holidays
Expand Down

0 comments on commit ca904a2

Please sign in to comment.