Skip to content

Commit

Permalink
allow event id or url in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
elichad committed Nov 20, 2023
1 parent c6d2267 commit a52b4ec
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 40 deletions.
19 changes: 17 additions & 2 deletions amy/extrequests/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions amy/extrequests/templatetags/eventbrite.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
24 changes: 24 additions & 0 deletions amy/extrequests/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
Expand Down Expand Up @@ -112,6 +113,7 @@ def test_fields(self):
"invalid_member_code",
"affiliation",
"location",
"eventbrite_id",
"order_by",
},
)
Expand Down Expand Up @@ -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"
Expand Down
33 changes: 0 additions & 33 deletions amy/extrequests/tests/test_template_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
35 changes: 35 additions & 0 deletions amy/extrequests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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")
14 changes: 14 additions & 0 deletions amy/extrequests/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit a52b4ec

Please sign in to comment.