Skip to content

Commit

Permalink
improve naming and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elichad committed Nov 20, 2023
1 parent c1c5cd8 commit 49958dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
16 changes: 11 additions & 5 deletions amy/extrequests/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions amy/extrequests/templatetags/eventbrite.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions amy/extrequests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
7 changes: 2 additions & 5 deletions amy/extrequests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 49958dd

Please sign in to comment.