Skip to content

Commit

Permalink
Add undefined checks and don't depend on having vaalilakana.json havi…
Browse files Browse the repository at this point in the history
…ng the correct structure
  • Loading branch information
ApsiV11 committed Oct 3, 2024
1 parent 7e76892 commit 73bc834
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion data/example_vaalilakana.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Puheenjohtaja": [], "Varapuheenjohtaja": [], "Rahastonhoitaja": [], "Fuksikapteeni": [], "IE": [], "Hupimestari": [], "Viestint\u00e4vastaava": [], "Kv-vastaava": [], "Yrityssuhdevastaava": [], "Opintovastaava": [], "ISOvastaava": [], "Jatkuvuustoimikunnan puheenjohtaja": [], "Excumestari": [], "Lukkarimestari": [], "Ulkosuhdevastaava": [], "Kvantin p\u00e4\u00e4toimittaja": []}
{}
34 changes: 22 additions & 12 deletions vaalilakanabot2024.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def _vaalilakana_to_string(lakana):
# Hardcoded to maintain order instead using dict keys
for position in BOARD:
output += f"<b>{position}:</b>\n"
for applicant in lakana[position]:
applicants = lakana[position] if position in lakana else []
for applicant in applicants:
link = applicant["fiirumi"]
selected = applicant["valittu"]
if selected:
Expand All @@ -101,7 +102,8 @@ def _vaalilakana_to_string(lakana):
output += "<b>----------Toimihenkilöt----------</b>\n"
for position in OFFICIALS:
output += f"<b>{position}:</b>\n"
for applicant in lakana[position]:
applicants = lakana[position] if position in lakana else []
for applicant in applicants:
link = applicant["fiirumi"]
selected = applicant["valittu"]
if selected:
Expand Down Expand Up @@ -199,14 +201,15 @@ def remove_applicant(update, context):
)
raise ValueError("Invalid parameters") from e

if position not in vaalilakana:
if position not in BOARD and position not in OFFICIALS:
updater.bot.send_message(
chat_id, f"Tunnistamaton virka: {position}", parse_mode="HTML"
)
raise ValueError(f"Unknown position {position}")

found = None
for applicant in vaalilakana[position]:
applicants = vaalilakana[position] if position in vaalilakana else []
for applicant in applicants:
if name == applicant["name"]:
found = applicant
break
Expand Down Expand Up @@ -249,7 +252,7 @@ def add_fiirumi_to_applicant(update, context):
)
raise ValueError("Invalid parameters") from e

if position not in vaalilakana:
if position not in BOARD and position not in OFFICIALS:
updater.bot.send_message(
chat_id, f"Tunnistamaton virka: {position}", parse_mode="HTML"
)
Expand All @@ -264,7 +267,9 @@ def add_fiirumi_to_applicant(update, context):
raise ValueError(f"Unknown thread {thread_id}")

found = None
for applicant in vaalilakana[position]:

applicants = vaalilakana[position] if position in vaalilakana else []
for applicant in applicants:
if name == applicant["name"]:
found = applicant
fiirumi = f'{BASE_URL}/t/{fiirumi_posts[thread_id]["slug"]}/{fiirumi_posts[thread_id]["id"]}'
Expand Down Expand Up @@ -393,6 +398,9 @@ def enter_applicant_name(update: Update, context: CallbackContext) -> int:
"valittu": False,
}

if position not in vaalilakana:
vaalilakana[position] = []

vaalilakana[position].append(new_applicant)
_save_data("data/vaalilakana.json", vaalilakana)
global last_applicant
Expand Down Expand Up @@ -433,16 +441,17 @@ def unassociate_fiirumi(update, context):
)
return

if role not in vaalilakana:
if role not in BOARD and role not in OFFICIALS:
updater.bot.send_message(
chat_id, "Virheelliset parametrit, roolia ei löytynyt"
)
return

# Try finding the dict with matching applicant name from vaalilakana
for app_info in vaalilakana[role]:
if app_info["name"] == applicant:
app_info["fiirumi"] = ""
applicants = vaalilakana[role] if role in vaalilakana else []
for applicant in applicants:
if applicant["name"] == applicant:
applicant["fiirumi"] = ""
break
else:
# If the loop didn't break
Expand Down Expand Up @@ -480,14 +489,15 @@ def add_selected_tag(update, context):
)
raise ValueError from e

if position not in vaalilakana:
if position not in BOARD and position not in OFFICIALS:
updater.bot.send_message(
chat_id, f"Tunnistamaton virka: {position}", parse_mode="HTML"
)
raise ValueError(f"Unknown position {position}")

found = None
for applicant in vaalilakana[position]:
applicants = vaalilakana[position] if position in vaalilakana else []
for applicant in applicants:
if name == applicant["name"]:
found = applicant
applicant["valittu"] = True
Expand Down

0 comments on commit 73bc834

Please sign in to comment.