diff --git a/amy/extrequests/templatetags/eventbrite.py b/amy/extrequests/templatetags/eventbrite.py new file mode 100644 index 000000000..1d11bb910 --- /dev/null +++ b/amy/extrequests/templatetags/eventbrite.py @@ -0,0 +1,16 @@ +from django import template +import regex + +register = template.Library() + +# Eventbrite IDs are long strings of digits (~12 characters) +EVENTBRITE_ID_PATTERN = regex.compile(r"\d{10,}") + + +@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 diff --git a/amy/extrequests/tests/test_template_tags.py b/amy/extrequests/tests/test_template_tags.py index e752ca540..94d5e1e49 100644 --- a/amy/extrequests/tests/test_template_tags.py +++ b/amy/extrequests/tests/test_template_tags.py @@ -2,10 +2,12 @@ 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, ) + from workshops.models import Membership @@ -98,3 +100,35 @@ 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/templates/requests/all_trainingrequests.html b/amy/templates/requests/all_trainingrequests.html index 0690f0ff6..b8d184eab 100644 --- a/amy/templates/requests/all_trainingrequests.html +++ b/amy/templates/requests/all_trainingrequests.html @@ -5,6 +5,7 @@ {% load tags %} {% load state %} {% load static %} +{% load eventbrite %} {% block content %} {% if requests %} @@ -20,6 +21,7 @@