From c6d2267bca462db969f82f6e7b1b1d4b3cfbef90 Mon Sep 17 00:00:00 2001 From: Eli Chadwick Date: Fri, 10 Nov 2023 10:53:59 +0000 Subject: [PATCH] get eventbrite id from url --- amy/extrequests/templatetags/eventbrite.py | 16 +++++++++ amy/extrequests/tests/test_template_tags.py | 34 +++++++++++++++++++ .../requests/all_trainingrequests.html | 9 +++++ .../management/commands/fake_database.py | 7 +++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 amy/extrequests/templatetags/eventbrite.py 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 @@ Submitter Reg. Code + Eventbrite ID Affiliation {{ req.member_code|default:"—" }} + + {% if req.eventbrite_url %} + {% eventbrite_id_from_url req.eventbrite_url %} + {% else %} + — + {% endif %} + {{ req.affiliation|default:"—" }} {% if req.person %} diff --git a/amy/workshops/management/commands/fake_database.py b/amy/workshops/management/commands/fake_database.py index 5341f8bbc..b7f3c1f9c 100644 --- a/amy/workshops/management/commands/fake_database.py +++ b/amy/workshops/management/commands/fake_database.py @@ -322,7 +322,12 @@ def fake_training_request(self, person_or_None): underrepresented_choices = TrainingRequest._meta.get_field( "underrepresented" ).choices - eventbrite_url = self.faker.url() if registration_code and randbool(0.5) else "" + eventbrite_url = "" + if registration_code and randbool(0.5): + eventbrite_url = ( + "https://eventbrite.com/fake-" + f"{self.faker.random_number(digits=12, fix_len=True)}" + ) req = TrainingRequest.objects.create( state=state, person=person_or_None,