Skip to content

Commit

Permalink
update ical on approve (#165)
Browse files Browse the repository at this point in the history
### Description

Please explain the changes you made here.

### Checklist

- [ ] Created tests which fail without the change (if possible)
- [ ] All tests passing
- [ ] Extended the documentation, if necessary

---------

Co-authored-by: Petitoto <[email protected]>
  • Loading branch information
Jake-Ransom and Petitoto authored Apr 2, 2023
1 parent 0b4e037 commit 656f862
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 16 additions & 11 deletions app/cruds/cruds_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ async def add_event(
db.add(event)
try:
await db.commit()
try:
await create_icalendar_file(db, calendar_file_path, settings)
return event
except Exception as error:
await db.rollback()
raise ValueError(error)
return event

except IntegrityError as error:
await db.rollback()
Expand Down Expand Up @@ -109,17 +104,25 @@ async def delete_event(db: AsyncSession, event_id: str, settings: Settings) -> N
raise ValueError()


async def confirm_event(db: AsyncSession, decision: Decision, event_id: str):
async def confirm_event(
db: AsyncSession, decision: Decision, event_id: str, settings: Settings
):
await db.execute(
update(models_calendar.Event)
.where(models_calendar.Event.id == event_id)
.values(decision=decision)
)
try:
await db.commit()
except IntegrityError:
if decision == Decision.approved:
try:
await create_icalendar_file(db, calendar_file_path, settings)
except Exception as error:
await db.rollback()
raise ValueError(error)
except IntegrityError as error:
await db.rollback()
raise ValueError()
raise ValueError(error)


async def create_icalendar_file(
Expand All @@ -138,8 +141,10 @@ async def create_icalendar_file(
ical_event.add("uid", f"{event.id}@myecl.fr")
ical_event.add("summary", event.name)
ical_event.add("description", event.description)
ical_event.add("dtstart", event.start.astimezone(timezone("UTC")))
ical_event.add("dtend", event.end.astimezone(timezone("UTC")))
ical_event.add(
"dtstart", event.start.astimezone(timezone(settings.TIMEZONE))
)
ical_event.add("dtend", event.end.astimezone(timezone(settings.TIMEZONE)))
ical_event.add("dtstamp", datetime.now(timezone(settings.TIMEZONE)))
ical_event.add("class", "public")
ical_event.add("organizer", event.organizer)
Expand Down
5 changes: 4 additions & 1 deletion app/endpoints/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,16 @@ async def confirm_booking(
decision: Decision,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.BDE)),
settings: Settings = Depends(get_settings),
):
"""
Give a decision to an event.
**Only usable by admins**
"""
await cruds_calendar.confirm_event(event_id=event_id, decision=decision, db=db)
await cruds_calendar.confirm_event(
event_id=event_id, decision=decision, db=db, settings=settings
)


@router.delete("/calendar/events/{event_id}", status_code=204, tags=[Tags.calendar])
Expand Down

0 comments on commit 656f862

Please sign in to comment.