Skip to content

Commit 9391c1b

Browse files
fix: handle missing json file
1 parent b4e82ac commit 9391c1b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

app/main.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import arrow
4+
import os
45
import pandas as pd
56
import sys
67
import time
@@ -18,9 +19,9 @@
1819
from meetup_query import *
1920
from passlib.context import CryptContext
2021
from pathlib import Path
21-
from pony.orm import Database, Required, Optional, PrimaryKey, Set, db_session
22+
from pony.orm import Database, Optional, PrimaryKey, Required, Set, db_session
2223
from pydantic import BaseModel
23-
from schedule import get_schedule, get_current_schedule_time, snooze_schedule, check_and_revert_snooze
24+
from schedule import check_and_revert_snooze, get_current_schedule_time, get_schedule, snooze_schedule
2425
from sign_jwt import main as gen_token
2526
from slackbot import *
2627
from typing import List, Union
@@ -74,8 +75,8 @@
7475

7576

7677
class IPConfig(BaseModel):
77-
whitelist: List[str] = ["localhost", "127.0.0.1"]
78-
public_ips: List[str] = [] # TODO: add whitelisted public IPs here
78+
whitelist: list[str] = ["localhost", "127.0.0.1"]
79+
public_ips: list[str] = [] # TODO: add whitelisted public IPs here
7980

8081

8182
ip_config = IPConfig()
@@ -254,7 +255,7 @@ async def ip_whitelist_or_auth(request: Request, current_user: User = Depends(ge
254255
return current_user
255256

256257

257-
def check_auth(auth: Union[dict, User]) -> None:
258+
def check_auth(auth: dict | User) -> None:
258259
"""
259260
Shared function to check authentication result.
260261
Raises an HTTPException if authentication fails.
@@ -276,9 +277,7 @@ async def login_for_oauth_token(form_data: OAuth2PasswordRequestForm = Depends()
276277
headers={"WWW-Authenticate": "Bearer"},
277278
)
278279
oauth_token_expires = timedelta(minutes=TOKEN_EXPIRE)
279-
oauth_token = create_access_token(
280-
data={"sub": user.username}, expires_delta=oauth_token_expires
281-
)
280+
oauth_token = create_access_token(data={"sub": user.username}, expires_delta=oauth_token_expires)
282281

283282
return {"access_token": oauth_token, "token_type": "bearer"}
284283

@@ -416,6 +415,10 @@ def get_events(auth: dict = Depends(ip_whitelist_or_auth),
416415
# cleanup output file
417416
sort_json(json_fn)
418417

418+
# check if file exists after sorting
419+
if not os.path.exists(json_fn) or os.stat(json_fn).st_size == 0:
420+
return {"message": "No events found", "events": []}
421+
419422
return pd.read_json(json_fn)
420423

421424

@@ -524,7 +527,7 @@ def post_slack(
524527
def snooze_slack_post(
525528
duration: str,
526529
auth: dict = Depends(ip_whitelist_or_auth),
527-
):
530+
):
528531
"""
529532
Snooze the Slack post for the specified duration
530533
@@ -544,7 +547,7 @@ def snooze_slack_post(
544547

545548
# TODO: test IP whitelisting
546549
@api_router.get("/schedule")
547-
def get_current_schedule(auth: Union[dict, User] = Depends(ip_whitelist_or_auth)):
550+
def get_current_schedule(auth: dict | User = Depends(ip_whitelist_or_auth)):
548551
"""
549552
Get the current schedule including any active snoozes
550553
"""

0 commit comments

Comments
 (0)