From ea38b72b5baef5017eebe08fbb69f29fd91a0bbb Mon Sep 17 00:00:00 2001 From: smith Date: Wed, 20 Nov 2024 10:40:00 -0500 Subject: [PATCH] Improve user lookup form field query (#6361) * Remove from loop * Get all responses, not just first --- kitsune/sumo/form_fields.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/kitsune/sumo/form_fields.py b/kitsune/sumo/form_fields.py index 4290c075898..736be66f9d3 100644 --- a/kitsune/sumo/form_fields.py +++ b/kitsune/sumo/form_fields.py @@ -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: @@ -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