Skip to content

Commit

Permalink
Improve user lookup form field query (#6361)
Browse files Browse the repository at this point in the history
* Remove from loop
* Get all responses, not just first
  • Loading branch information
smithellis authored Nov 20, 2024
1 parent cd7e2f2 commit ea38b72
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions kitsune/sumo/form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def validate(self, value):


class MultiUsernameField(forms.Field):
"""Form field that takes a comma-separated list of usernames OR profile
"""
Form field that takes a comma-separated list of usernames OR profile
names (display names) as input, validates that users exist for each one,
and returns the list of users."""
and returns the list of users.
"""

def to_python(self, value):
if not value:
Expand All @@ -67,15 +69,14 @@ def to_python(self, value):
return []

users = []
for username in value.split(","):
username = username.strip()
if username:
user = User.objects.filter(
Q(username=username) | Q(profile__name=username)
).first()
if user:
if user.is_active:
users.append(user)
usernames = [name.strip() for name in value.split(",") if name]
if usernames:
all_users = User.objects.filter(
Q(username__in=usernames) | Q(profile__name__in=usernames)
)
for user in all_users:
if user and user.is_active:
users.append(user)

return users

Expand Down

0 comments on commit ea38b72

Please sign in to comment.