diff --git a/amy/extrequests/filters.py b/amy/extrequests/filters.py index 3bb90a046..b11f3bb39 100644 --- a/amy/extrequests/filters.py +++ b/amy/extrequests/filters.py @@ -7,6 +7,7 @@ import django_filters from extrequests.models import SelfOrganisedSubmission, WorkshopInquiryRequest +from extrequests.utils import get_eventbrite_id_from_url from workshops.fields import Select2Widget from workshops.filters import ( AllCountriesFilter, @@ -43,8 +44,9 @@ def __init__(self, data=None, *args, **kwargs): field_name="member_code", lookup_expr="icontains", label="Member code" ) - eventbrite_url = django_filters.CharFilter( - field_name="eventbrite_url", lookup_expr="icontains", label="Eventbrite URL" + eventbrite_id = django_filters.CharFilter( + label="Eventbrite ID or URL", + method="filter_eventbrite_id", ) state = django_filters.ChoiceFilter( @@ -171,6 +173,19 @@ def filter_member_code_override( return queryset.filter(member_code_override=True) return queryset + 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 + try: + int(value) + except ValueError: + value = get_eventbrite_id_from_url(value) + + return queryset.filter(eventbrite_url__icontains=value) + # ------------------------------------------------------------ # WorkshopRequest related filter and filter methods diff --git a/amy/extrequests/templatetags/eventbrite.py b/amy/extrequests/templatetags/eventbrite.py index 1d11bb910..f8ce6409f 100644 --- a/amy/extrequests/templatetags/eventbrite.py +++ b/amy/extrequests/templatetags/eventbrite.py @@ -1,6 +1,8 @@ from django import template import regex +from extrequests.utils import get_eventbrite_id_from_url + register = template.Library() # Eventbrite IDs are long strings of digits (~12 characters) @@ -9,8 +11,4 @@ @register.simple_tag def eventbrite_id_from_url(url: str) -> str: - if not isinstance(url, str): - return url - - re = regex.search(EVENTBRITE_ID_PATTERN, url) - return re.group() if re else url + return get_eventbrite_id_from_url(url) diff --git a/amy/extrequests/tests/test_filters.py b/amy/extrequests/tests/test_filters.py index 3930540ac..cc1c6e52c 100644 --- a/amy/extrequests/tests/test_filters.py +++ b/amy/extrequests/tests/test_filters.py @@ -63,6 +63,7 @@ def setUp(self) -> None: person=self.ironman, review_process="preapproved", member_code=self.membership.registration_code, + eventbrite_url="https://www.eventbrite.com/e/711575811407", personal="Tony", family="Stark", email="me@stark.com", @@ -112,6 +113,7 @@ def test_fields(self): "invalid_member_code", "affiliation", "location", + "eventbrite_id", "order_by", }, ) @@ -318,6 +320,28 @@ def test_filter_location(self): # Assert self.assertQuerysetEqual(result, [self.request_ironman]) + def test_filter_eventbrite_id__digits(self): + # Arrange + name = "eventbrite_id" + value = "1407" + + # Act + result = self.filterset.filters[name].filter(self.qs, value) + + # Assert + self.assertQuerysetEqual(result, [self.request_ironman]) + + def test_filter_eventbrite_id__url(self): + # Arrange + name = "eventbrite_id" + value = "https://www.eventbrite.com/myevent?eid=711575811407" + + # Act + result = self.filterset.filters[name].filter(self.qs, value) + + # Assert + self.assertQuerysetEqual(result, [self.request_ironman]) + def test_filter_order_by(self): # Arrange filter_name = "order_by" diff --git a/amy/extrequests/tests/test_template_tags.py b/amy/extrequests/tests/test_template_tags.py index 94d5e1e49..e5b447beb 100644 --- a/amy/extrequests/tests/test_template_tags.py +++ b/amy/extrequests/tests/test_template_tags.py @@ -2,7 +2,6 @@ from django.test import TestCase -from extrequests.templatetags.eventbrite import eventbrite_id_from_url from amy.extrequests.templatetags.request_membership import ( membership_active, membership_alert_type, @@ -100,35 +99,3 @@ def test_inactive(self): # Assert self.assertEqual(expected, result) - - -class TestEventbriteIdFromUrl(TestCase): - def test_long_url(self): - # Arrange - url = "https://www.eventbrite.com/e/online-instructor-training-7-8-november-2023-tickets-711575811407?aff=oddtdtcreator" # noqa: line too long - - # Act - result = eventbrite_id_from_url(url) - - # Assert - self.assertEqual(result, "711575811407") - - def test_short_url(self): - # Arrange - url = "https://www.eventbrite.com/e/711575811407" - - # Act - result = eventbrite_id_from_url(url) - - # Assert - self.assertEqual(result, "711575811407") - - def test_admin_url(self): - # Arrange - url = "https://www.eventbrite.com/myevent?eid=711575811407" - - # Act - result = eventbrite_id_from_url(url) - - # Assert - self.assertEqual(result, "711575811407") diff --git a/amy/extrequests/tests/test_utils.py b/amy/extrequests/tests/test_utils.py index d84c86464..848aca7b7 100644 --- a/amy/extrequests/tests/test_utils.py +++ b/amy/extrequests/tests/test_utils.py @@ -1,7 +1,10 @@ from datetime import date, timedelta +from django.test import TestCase + from extrequests.utils import ( MemberCodeValidationError, + get_eventbrite_id_from_url, get_membership_or_none_from_code, member_code_valid, member_code_valid_training, @@ -283,3 +286,35 @@ def test_returns_matching_membership(self): # Assert self.assertEqual(result, self.membership) + + +class TestGetEventbriteIdFromUrl(TestCase): + def test_long_url(self): + # Arrange + 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) + + # Assert + self.assertEqual(result, "711575811407") + + def test_short_url(self): + # Arrange + url = "https://www.eventbrite.com/e/711575811407" + + # Act + result = get_eventbrite_id_from_url(url) + + # Assert + self.assertEqual(result, "711575811407") + + def test_admin_url(self): + # Arrange + url = "https://www.eventbrite.com/myevent?eid=711575811407" + + # Act + result = get_eventbrite_id_from_url(url) + + # Assert + self.assertEqual(result, "711575811407") diff --git a/amy/extrequests/utils.py b/amy/extrequests/utils.py index 64a8dfc00..39d201adf 100644 --- a/amy/extrequests/utils.py +++ b/amy/extrequests/utils.py @@ -1,9 +1,13 @@ from datetime import date from django.core.exceptions import ValidationError +import regex from workshops.models import Membership +# Eventbrite IDs are long strings of digits (~12 characters) +EVENTBRITE_ID_PATTERN = regex.compile(r"\d{10,}") + class MemberCodeValidationError(ValidationError): pass @@ -76,3 +80,13 @@ def get_membership_or_none_from_code(code: str) -> Membership | None: return Membership.objects.get(registration_code=code) except Membership.DoesNotExist: return None + + +def get_eventbrite_id_from_url(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 + + re = regex.search(EVENTBRITE_ID_PATTERN, url) + return re.group() if re else url