Skip to content

Commit

Permalink
Bug fixes (hopefully it works)
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegg11 committed Dec 21, 2024
1 parent c45a890 commit 10bc5a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
10 changes: 2 additions & 8 deletions core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,8 @@ class DailyAnnoucementAdminForm(forms.ModelForm):

def clean(self):
cleaned_data = super().clean()
# Not sure if we need to enforce the maximum of 3 consecutive school days rule or not in the form
# If we do, is there an internal method for checking whether a certain is a school day?

# start_date = cleaned_data.get("start_date")
# end_date = cleaned_data.get("end_date")

# if (start_date - end_date).days > 4:
# raise forms.ValidationError({"end_date": "Annoucement can only be read up to a maximum of 3 consecutive school days"})

# TODO: Validate start and end date using function DailyAnnoucements.validate_annoucement_date()

class AnnouncementAdminForm(forms.ModelForm):
status = forms.ChoiceField(
Expand Down
8 changes: 3 additions & 5 deletions core/management/commands/auth_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ def handle(self, *args, **options):

SECRETS_PATH = settings.SECRETS_PATH

CLIENT_PATH = SECRETS_PATH + "\\client_secret.json"
AUTHORIZED_PATH = SECRETS_PATH + "\\authorized_user.json"
CLIENT_PATH = SECRETS_PATH + "/client_secret.json"
AUTHORIZED_PATH = SECRETS_PATH + "/authorized_user.json"

scopes = [
'https://www.googleapis.com/auth/spreadsheets.readonly'
]
scopes = gspread.auth.READONLY_SCOPES

if not Path(settings.SECRETS_PATH).is_dir():
raise CommandError(f"{SECRETS_PATH} directory does not exist")
Expand Down
44 changes: 28 additions & 16 deletions core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def setup_periodic_tasks(sender, **kwargs):
crontab(hour=1, minute=0), clear_expired
) # Delete expired oauth2 tokens from db everyday at 1am

sender.add_periodic_task(crontab(hour=8, minute=9, day_of_week="mon-fri"), fetch_annoucements)
sender.add_periodic_task(crontab(hour=8, minute=0, day_of_week="mon-fri"), fetch_annoucements)


@app.task
Expand Down Expand Up @@ -248,8 +248,8 @@ def fetch_annoucements():
if settings.GOOGLE_SHEET_KEY == "" or settings.GOOGLE_SHEET_KEY == None:
return

CLIENT_PATH = settings.SECRETS_PATH + "\\client_secret.json"
AUTHORIZED_PATH = settings.SECRETS_PATH + "\\authorized_user.json"
CLIENT_PATH = settings.SECRETS_PATH + "/client_secret.json"
AUTHORIZED_PATH = settings.SECRETS_PATH + "/authorized_user.json"

if not Path(settings.SECRETS_PATH).is_dir():
logger.warning(f"Fetch Annoucements: {settings.SECRETS_PATH} directory does not exist")
Expand All @@ -260,28 +260,40 @@ def fetch_annoucements():
return

client = None
scopes = [
'https://www.googleapis.com/auth/spreadsheets.readonly'
]
scopes = gspread.auth.READONLY_SCOPES

if Path(AUTHORIZED_PATH).is_file():
creds = Credentials.from_authorized_user_file(AUTHORIZED_PATH, scopes)

if not creds.valid and creds.expired and creds.refresh_token:

try:
creds.refresh(Request())
client = gspread.authorize(creds)

with open(AUTHORIZED_PATH, "w") as f:
f.write(creds.to_json())
except Exception as e:
logger.warning("Fetch Annoucements: Failed to refresh or authorize new credentials")
return
logger.warning("Fetch Annoucements: Failed to refresh credentials")
return

with open(AUTHORIZED_PATH, "w") as f:
f.write(creds.to_json())

try:
client = gspread.authorize(creds)
except Exception as e:
logger.warning("Fetch Annoucements: Failed to authorize credentials")
return

else:
logger.warning("Fetch Annoucements: Please run auth_google command to authenticate google account")
return
return

worksheet = None

try:
worksheet = client.open_by_key(settings.GOOGLE_SHEET_KEY).sheet1
except Exception as e:
logger.warning("Fetch Annoucements: Failed to open google sheet")
return

worksheet = client.open(settings.GOOGLE_SHEET_KEY).sheet1
row_counter = 2

while True:
Expand All @@ -302,8 +314,8 @@ def fetch_annoucements():

# TODO: Validate start and end date using function DailyAnnoucements.validate_annoucement_date()

DailyAnnoucement.objects.create(**parsed_data)
DailyAnnoucement.objects.get_or_create(**parsed_data)
except:
logger.warning(f"Fetch Annoucements: Failed to read, parse or create object for row {row_counter}")

row_counter += 1
row_counter += 1

0 comments on commit 10bc5a5

Please sign in to comment.