diff --git a/amy/extrequests/filters.py b/amy/extrequests/filters.py index 2ebc636df..e8d38a1c2 100644 --- a/amy/extrequests/filters.py +++ b/amy/extrequests/filters.py @@ -7,7 +7,7 @@ import django_filters from extrequests.models import SelfOrganisedSubmission, WorkshopInquiryRequest -from extrequests.utils import get_eventbrite_id_from_url +from extrequests.utils import get_eventbrite_id_from_url_or_return_input from workshops.fields import Select2Widget from workshops.filters import ( AllCountriesFilter, @@ -167,13 +167,19 @@ def filter_non_null_manual_score(self, queryset, name, manual_score): def filter_eventbrite_id( self, queryset: QuerySet, name: str, value: str ) -> QuerySet: - # user may input the event's ID or its full URL - # events have multiple possible URLs which all contain the ID, - # so filter by the ID if possible + """ + Returns the queryset filtered by an Eventbrite ID or URL. + Events have multiple possible URLs which all contain the ID, so + if a URL is used, the filter will try to extract and filter by the ID. + If no ID can be found, the filter will use the original input. + """ + try: + # if input is an integer, assume it to be a partial or full Eventbrite ID int(value) except ValueError: - value = get_eventbrite_id_from_url(value) + # otherwise, try to extract an ID from the input + value = get_eventbrite_id_from_url_or_return_input(value) return queryset.filter(eventbrite_url__icontains=value) diff --git a/amy/extrequests/templatetags/eventbrite.py b/amy/extrequests/templatetags/eventbrite.py index dd5f42d97..e27c0beff 100644 --- a/amy/extrequests/templatetags/eventbrite.py +++ b/amy/extrequests/templatetags/eventbrite.py @@ -1,10 +1,10 @@ from django import template -from extrequests.utils import get_eventbrite_id_from_url +from extrequests.utils import get_eventbrite_id_from_url_or_return_input register = template.Library() @register.simple_tag def eventbrite_id_from_url(url: str) -> str: - return get_eventbrite_id_from_url(url) + return get_eventbrite_id_from_url_or_return_input(url) diff --git a/amy/extrequests/tests/test_utils.py b/amy/extrequests/tests/test_utils.py index 848aca7b7..b0289ac6f 100644 --- a/amy/extrequests/tests/test_utils.py +++ b/amy/extrequests/tests/test_utils.py @@ -4,7 +4,7 @@ from extrequests.utils import ( MemberCodeValidationError, - get_eventbrite_id_from_url, + get_eventbrite_id_from_url_or_return_input, get_membership_or_none_from_code, member_code_valid, member_code_valid_training, @@ -294,7 +294,7 @@ def test_long_url(self): url = "https://www.eventbrite.com/e/online-instructor-training-7-8-november-2023-tickets-711575811407?aff=oddtdtcreator" # noqa: line too long # Act - result = get_eventbrite_id_from_url(url) + result = get_eventbrite_id_from_url_or_return_input(url) # Assert self.assertEqual(result, "711575811407") @@ -304,7 +304,7 @@ def test_short_url(self): url = "https://www.eventbrite.com/e/711575811407" # Act - result = get_eventbrite_id_from_url(url) + result = get_eventbrite_id_from_url_or_return_input(url) # Assert self.assertEqual(result, "711575811407") @@ -314,7 +314,7 @@ def test_admin_url(self): url = "https://www.eventbrite.com/myevent?eid=711575811407" # Act - result = get_eventbrite_id_from_url(url) + result = get_eventbrite_id_from_url_or_return_input(url) # Assert self.assertEqual(result, "711575811407") diff --git a/amy/extrequests/utils.py b/amy/extrequests/utils.py index 45badbbfb..2b4033172 100644 --- a/amy/extrequests/utils.py +++ b/amy/extrequests/utils.py @@ -82,11 +82,8 @@ def get_membership_or_none_from_code(code: str) -> Membership | None: return None -def get_eventbrite_id_from_url(url: str) -> str: +def get_eventbrite_id_from_url_or_return_input(url: str) -> str: """Given the URL for an Eventbrite event, returns that event's ID. - If the ID can't be found, returns the original URL.""" - if not isinstance(url, str): - return url - + If the ID can't be found, returns the input URL.""" match = re.search(EVENTBRITE_ID_PATTERN, url) return match.group() if match else url