Skip to content

Commit

Permalink
Fix issues with HttpUrl (Related: pydantic/pydantic#6395) similar to C…
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavberi committed Nov 28, 2024
1 parent c66aadd commit a3a85da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Event_Location,
Event_Mode,
Event_Status,
HttpUrlString,
PyObjectId,
event_desc_type,
event_name_type,
Expand All @@ -39,7 +40,7 @@ class Event(BaseModel):
datetimeperiod: Tuple[datetime, datetime]
poster: str | None = None
audience: List[Audience] = []
link: HttpUrl | None = None
link: HttpUrlString | None = None

mode: Event_Mode = Event_Mode.hybrid
location: List[Event_Location] = []
Expand Down
17 changes: 16 additions & 1 deletion mtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import pytz
import strawberry
from bson import ObjectId
from pydantic import Field, StringConstraints, field_validator
from pydantic import (
BeforeValidator,
Field,
HttpUrl,
StringConstraints,
TypeAdapter,
field_validator,
)
from pydantic_core import core_schema
from typing_extensions import Annotated, Any

Expand Down Expand Up @@ -287,4 +294,12 @@ def __get_pydantic_json_schema__(cls, field_schema):
field_schema.update(type="string")


http_url_adapter = TypeAdapter(HttpUrl)
HttpUrlString = Annotated[
str,
BeforeValidator(
lambda value: str(http_url_adapter.validate_python(value))
),
]

timezone = pytz.timezone("Asia/Kolkata")
5 changes: 1 addition & 4 deletions mutations/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import strawberry
from fastapi.encoders import jsonable_encoder
from prettytable import PrettyTable
from pydantic import HttpUrl, TypeAdapter

from db import eventsdb
from mailing import triggerMail
Expand Down Expand Up @@ -97,9 +96,7 @@ 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 = TypeAdapter(HttpUrl).validate_python(
details.link
)
event_instance.link = details.link
if details.equipment is not None:
event_instance.equipment = details.equipment
if details.additional is not None:
Expand Down

0 comments on commit a3a85da

Please sign in to comment.