Skip to content

Commit

Permalink
Make sure to lowercase all matching emails
Browse files Browse the repository at this point in the history
Sometimes people opt to give email addresses as capitalized, make sure
we handle that when mapping to and from geardb.
  • Loading branch information
DavidCain committed Apr 22, 2024
1 parent 43a68b5 commit 9611fb5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ws/tests/views/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,8 @@ def test_trips_data_included(self):
{
"id": 37,
"affiliation": "MIT undergrad",
"alternate_emails": ["tim@mit.edu"],
"email": "tim@example.com",
"alternate_emails": ["tim@MIT.EDU"], # Case-insensitive!
"email": "tim@EXAMPLE.COM",
"num_rentals": 3,
},
{
Expand Down
7 changes: 6 additions & 1 deletion ws/utils/geardb.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def membership_information() -> Iterator[MembershipInformation]:
info_by_user_id = _get_trips_information()

# Bridge from a lowercase email address to a Trips user ID
# Yes, lowercasing an email could technically cause collisions (Turkish dotless i)...
# This is just for statistics, though, so hopefully it's fine.
email_to_user_id: dict[str, int] = dict(
EmailAddress.objects.filter(verified=True)
.annotate(lower_email=Lower("email"))
Expand All @@ -345,7 +347,10 @@ def membership_information() -> Iterator[MembershipInformation]:

def trips_info_for(all_known_emails: Collection[str]) -> TripsInformation | None:
try:
email = next(e for e in all_known_emails if e.lower() in email_to_user_id)
# Maintain ordering, to prefer first email!
email = next(
e.lower() for e in all_known_emails if e.lower() in email_to_user_id
)
except StopIteration:
return None

Expand Down

0 comments on commit 9611fb5

Please sign in to comment.