Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
|
1f77842b1f |
51
src/main.py
51
src/main.py
|
@ -209,25 +209,51 @@ def get_slot_by_id(
|
|||
return event
|
||||
|
||||
|
||||
@app.post('/calendar_events/{slot_id}/mark_busy', response_model=CalendarEvent, tags=['Calendar'])
|
||||
class MarkBusyByIdResponse(SQLModel):
|
||||
target_slot: CalendarEvent
|
||||
target_slot_is_busy: bool
|
||||
closest_free_slot: CalendarEvent | None
|
||||
closest_free_slot_same_time: bool
|
||||
closest_free_slot_same_day: bool
|
||||
|
||||
|
||||
@app.post('/calendar_events/{slot_id}/mark_busy', response_model=MarkBusyByIdResponse, tags=['Calendar'])
|
||||
def mark_busy_calendar_slot_by_id(
|
||||
slot_id: str,
|
||||
description: str | None = None,
|
||||
service: Resource = Depends(get_calendar_service)
|
||||
):
|
||||
event = get_slot_by_id(slot_id=slot_id, service=service)
|
||||
target_event_start_time = datetime.fromisoformat(event.start.dateTime)
|
||||
|
||||
if event.summary != TITLE_FREE:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Slot is busy")
|
||||
free_slots = get_free_calendar_slots(lower_bound=target_event_start_time, service=service)
|
||||
free_slots = list(filter(lambda slot: slot.id != event.id, free_slots))
|
||||
closest_free_slot = free_slots[0] if free_slots else None
|
||||
closest_free_slot_same_time = False
|
||||
closest_free_slot_same_day = False
|
||||
if closest_free_slot:
|
||||
closest_free_slot_start_time = datetime.fromisoformat(closest_free_slot.start.dateTime)
|
||||
closest_free_slot_same_time = closest_free_slot_start_time == target_event_start_time
|
||||
closest_free_slot_same_day = closest_free_slot_start_time.date() == target_event_start_time.date()
|
||||
|
||||
event.summary = TITLE_BUSY
|
||||
event.colorId = COLOR_BUSY
|
||||
if description:
|
||||
event.description = description
|
||||
target_slot_is_busy = False
|
||||
if event.summary == TITLE_FREE:
|
||||
event.summary = TITLE_BUSY
|
||||
event.colorId = COLOR_BUSY
|
||||
if description:
|
||||
event.description = description
|
||||
|
||||
service.events().update(calendarId=CALENDAR_ID, eventId=event.id, body=event.dict(exclude_unset=True)).execute()
|
||||
service.events().update(calendarId=CALENDAR_ID, eventId=event.id, body=event.dict(exclude_unset=True)).execute()
|
||||
else:
|
||||
target_slot_is_busy = True
|
||||
|
||||
return event
|
||||
return MarkBusyByIdResponse(
|
||||
target_slot=event,
|
||||
target_slot_is_busy=target_slot_is_busy,
|
||||
closest_free_slot=closest_free_slot,
|
||||
closest_free_slot_same_time=closest_free_slot_same_time,
|
||||
closest_free_slot_same_day=closest_free_slot_same_day
|
||||
)
|
||||
|
||||
|
||||
@app.post('/calendar_events/{slot_id}/mark_free', response_model=CalendarEvent, tags=['Calendar'])
|
||||
|
@ -313,16 +339,15 @@ class FormatDateRequest(BaseModel):
|
|||
{
|
||||
'date': "10.07.2023 15:33:08"
|
||||
}
|
||||
# '2023-07-10T15:33:08+07:00',
|
||||
# '2023-07-10 15:33:08',
|
||||
# '10.07.2023 15:33:08'
|
||||
]
|
||||
}
|
||||
|
||||
@validator('date', pre=True)
|
||||
def validate_date(cls, val):
|
||||
try:
|
||||
if re.match(r'^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$', val):
|
||||
if type(val) == datetime:
|
||||
return val
|
||||
elif re.match(r'^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$', val):
|
||||
return datetime.strptime(val, "%Y-%m-%d %H:%M:%S")
|
||||
elif re.match(r'^(\d{2}.\d{2}.\d{4}) (\d{2}:\d{2}:\d{2})$', val):
|
||||
return datetime.strptime(val, "%d.%m.%Y %H:%M:%S")
|
||||
|
|
Loading…
Reference in New Issue