Skip to content

Commit

Permalink
2019 - Update 'MultiUsernameField' to handle display names (#6351)
Browse files Browse the repository at this point in the history
Use Q for improved query
Fix issue where an invalid username forces full revision list
  • Loading branch information
smithellis authored Nov 18, 2024
1 parent c477deb commit c760798
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
23 changes: 10 additions & 13 deletions kitsune/sumo/form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth.models import User
from django.core import validators
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.translation import gettext as _


Expand Down Expand Up @@ -54,8 +55,9 @@ def validate(self, value):


class MultiUsernameField(forms.Field):
"""Form field that takes a comma-separated list of usernames as input,
validates that users exist for each one, and returns the list of users."""
"""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."""

def to_python(self, value):
if not value:
Expand All @@ -67,18 +69,13 @@ def to_python(self, value):
users = []
for username in value.split(","):
username = username.strip()
msg = ""
if username:
try:
user = User.objects.get(username=username)
users.append(user)
except User.DoesNotExist:
msg = _("{username} is not a valid username.")
else:
if not user.is_active:
msg = _("{username} is not an active user.")
if msg:
raise forms.ValidationError(msg.format(username=username))
user = User.objects.filter(
Q(username=username) | Q(profile__name=username)
).first()
if user:
if user.is_active:
users.append(user)

return users

Expand Down
10 changes: 4 additions & 6 deletions kitsune/wiki/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1650,24 +1650,22 @@ def _show_revision_warning(document, revision):


def recent_revisions(request):
# Make writable
request.GET = request.GET.copy()

fragment = request.GET.pop("fragment", None)
form = RevisionFilterForm(request.GET)

# We are going to ignore validation errors for the most part, but
# this is needed to call the functions that generate `cleaned_data`
# This helps in particular when bad user names are typed in.
# Validate the form to populate cleaned_data, even with invalid usernames.
form.is_valid()

filters = {}
# If something has gone very wrong, `cleaned_data` won't be there.
if hasattr(form, "cleaned_data"):
if form.cleaned_data.get("locale"):
filters.update(document__locale=form.cleaned_data["locale"])

# Only apply user filter if there are valid users
if form.cleaned_data.get("users"):
filters.update(creator__in=form.cleaned_data["users"])

start = form.cleaned_data.get("start")
end = form.cleaned_data.get("end")
if start or end:
Expand Down

0 comments on commit c760798

Please sign in to comment.