Skip to content

Commit

Permalink
Fix Azamat's code
Browse files Browse the repository at this point in the history
  • Loading branch information
depocoder committed Sep 25, 2024
1 parent 10141c5 commit aaea3f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
31 changes: 19 additions & 12 deletions backend/app/controllers/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import uuid
from typing import Any, Optional
from typing import Optional

from pydantic import BaseModel, Field, computed_field

Expand All @@ -19,21 +19,15 @@ class ModeusCreds(BaseModel):
password: str


"""
{"size":500,"timeMin":"2024-09-23T00:00:00+03:00","timeMax":"2024-09-29T23:59:59+03:00","attendeePersonId":["d69c87c8-aece-4f39-b6a2-7b467b968211"]}
_"""


class ModeusSearchEvents(BaseModel):
"""Modeus search events body."""

size : int = 50
time_min: datetime.datetime = Field(alias="timeMin", default=datetime.datetime.now())
time_max: datetime.datetime = Field(alias="timeMax", default=datetime.datetime.now() - datetime.timedelta(days=7))
size : int = Field(examples=[50], default=50)
time_min: datetime.datetime = Field(alias="timeMin", examples=[datetime.datetime.now()])
time_max: datetime.datetime = Field(alias="timeMax", examples=[datetime.datetime.now() - datetime.timedelta(days=7)])
attendee_person_id: list[str] = Field(alias="attendeePersonId", default="d69c87c8-aece-4f39-b6a2-7b467b968211")

class Location(BaseModel):
event_id: str = Field(alias="eventId")
event_id: uuid.UUID = Field(alias="eventId")
custom_location: str = Field(alias="customLocation")

@computed_field
Expand All @@ -50,11 +44,24 @@ class Event(BaseModel):
id: uuid.UUID

class Embedded(BaseModel):
event: list[Event] = Field(alias="events")
events: list[Event] = Field(alias="events")
locations: list[Location] = Field(alias="event-locations")

class FullEvent(Event, Location):
pass

class ModeusCalendar(BaseModel):
"""Modeus calendar response."""

embedded: Embedded = Field(alias="_embedded")

def parse_modeus_response(self) -> list[FullEvent]:
locations = {location.event_id: location for location in self.embedded.locations}
full_events = []
for event in self.embedded.events:
location = locations[event.id]
full_events.append(FullEvent(**{
**event.model_dump(by_alias=True), **location.model_dump(by_alias=True)
}))
return full_events

7 changes: 4 additions & 3 deletions backend/integration/modeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from bs4 import BeautifulSoup, Tag
from httpx import URL, AsyncClient

from app.controllers.models import ModeusSearchEvents
from app.controllers.models import ModeusSearchEvents, ModeusCalendar, FullEvent
from integration.exceptions import CannotAuthenticateError, LoginFailedError


Expand Down Expand Up @@ -120,7 +120,7 @@ async def get_events(
__jwt: str,
body: ModeusSearchEvents,
timeout: int = 15,
) -> list[str] | None:
) -> list[FullEvent]:
"""Get events for student in modeus"""
session = AsyncClient(
http2=True,
Expand All @@ -133,4 +133,5 @@ async def get_events(
"/schedule-calendar-v2/api/calendar/events/search",
content=body.model_dump_json(by_alias=True),
)
return response.json()
modeus_calendar = ModeusCalendar.model_validate_json(response.text)
return modeus_calendar.parse_modeus_response()

0 comments on commit aaea3f9

Please sign in to comment.