From 1f77842b1f1c24325a91a4237844272adb7b2685 Mon Sep 17 00:00:00 2001 From: quaduzi Date: Tue, 11 Jul 2023 02:01:35 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D1=81=D0=B2=D0=BE=D0=B1=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BB=D0=BE=D1=82=D0=B0?= =?UTF-8?q?,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B2=D1=8B=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B9=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.py | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main.py b/src/main.py index f85701c..4f1bded 100644 --- a/src/main.py +++ b/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")