Refactor activity names to use an enum instead of a string to avoid typos #147

This commit is contained in:
Ian Renton
2026-09-18 18:09:41 +01:00
parent ab79e7e01c
commit e5caf7353d
61 changed files with 735 additions and 638 deletions
+20 -12
View File
@@ -1,23 +1,31 @@
from core.constants import ACTIVITIES
from data.activities import ACTIVITIES
def get_activity_by_name(name):
"""Utility function to resolve an arbitrary, case-insensitive activity name string (e.g. from a spot comment, a
provider, or an API request) to the matching known Activity. Returns None if no match is found."""
if not name:
return None
for activity_name, activity in ACTIVITIES.items():
if activity_name.upper() == name.upper():
return activity
return None
def get_ref_regex_for_activity(activity):
"""Utility function to get the regex string for an activity reference for a named activity. If no match is
found, None will be returned."""
for a in ACTIVITIES:
if a.name.upper() == activity.upper():
return a.ref_regex
return None
found = get_activity_by_name(activity)
return found.ref_regex if found else None
def get_icon_for_activity(activity):
"""Utility function to get the icon for a named activity. If no match is found, None will be returned."""
for a in ACTIVITIES:
if a.name.upper() == activity.upper():
return a.icon
return None
found = get_activity_by_name(activity)
return found.icon if found else None
def get_activity_name_from_comment_name(activity):
@@ -25,11 +33,11 @@ def get_activity_name_from_comment_name(activity):
but there are some cases (e.g. is "TOTA" Towers, Tiles or Toilets?) where we need to transform one to the
other."""
for a in ACTIVITIES:
for activity_name, a in ACTIVITIES.items():
if any(n.upper() == activity.upper() for n in a.comment_names):
return a.name
return activity_name
return None
# Regex matching any activity's "comment name", i.e. how it may be referred to in spot comments
ANY_ACTIVITY_REGEX = rf"({'|'.join(n for a in ACTIVITIES for n in a.comment_names)})"
ANY_ACTIVITY_REGEX = rf"({'|'.join(n for a in ACTIVITIES.values() for n in a.comment_names)})"