Skip to content

Commit

Permalink
Merge pull request #6001 from akatsoulas/default-user-msg
Browse files Browse the repository at this point in the history
Populate username by default
  • Loading branch information
akatsoulas committed May 14, 2024
2 parents 861b4cc + fbd04f5 commit 8872b90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion kitsune/messages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def outbox(request):

@login_required
def new_message(request):
form = MessageForm(request.POST or None, user=request.user)
data = request.POST or None
form_kwargs = {"user": request.user}
if not data:
form_kwargs["initial"] = request.GET.dict()
form = MessageForm(data, **form_kwargs)
if form.is_valid() and not is_ratelimited(request, "private-message-day", "50/d"):
send_message(
form.cleaned_data["to"],
Expand Down
12 changes: 7 additions & 5 deletions kitsune/sumo/form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,24 @@ def to_python(self, value):
if not value:
if self.required:
raise ValidationError(_("To field is required."))
else:
return []
return []

# This generator expression splits the input string `value` by commas to extract
# parts, strips whitespace from each part, and then further splits each non-empty
# part by the colon.
# Each resulting pair of values (before and after the colon) is stripped of
# any extra whitespace and returned as a tuple. This process is done lazily,
# generating each tuple only when iterated over.
key_value_pairs = (
tuple(map(str.strip, part.split(":"))) for part in value.split(",") if part.strip()
tuple(map(str.strip, (part if ":" in part else "user: " + part).split(":")[:2]))
for part in value.split(",")
if part.strip()
)

# Create data structure to hold values grouped by keys
to_objects = {}
for key, value in key_value_pairs:
# check if the value is a valid username in the database
if not User.objects.filter(username=value).exists():
raise ValidationError(_(f"{value} is not a valid username."))
to_objects.setdefault(f"{key.lower()}s", []).append(value)

return to_objects
Expand Down

0 comments on commit 8872b90

Please sign in to comment.