Skip to content

Commit

Permalink
Merge pull request #6355 from smithellis/2021-update-500-error
Browse files Browse the repository at this point in the history
Move validation to the clean_avatar method
  • Loading branch information
akatsoulas authored Nov 14, 2024
2 parents f6a3e99 + 692f387 commit 345ec81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions kitsune/groups/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GroupAvatarForm(forms.ModelForm):
avatar = LimitedImageField(required=True, widget=ImageWidget)

def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super(GroupAvatarForm, self).__init__(*args, **kwargs)
self.fields["avatar"].help_text = _("Your avatar will be resized to {size}x{size}").format(
size=settings.AVATAR_SIZE
Expand All @@ -34,19 +35,19 @@ class Meta:
fields = ["avatar"]

def clean_avatar(self):
avatar = self.cleaned_data.get("avatar")

"""Validate the avatar file."""
# Ensure an avatar file is attached
if not avatar:
raise forms.ValidationError(_("An avatar image is required."))

# Validate file size
try:
check_file_size(avatar, settings.MAX_AVATAR_FILE_SIZE)
except FileTooLargeError as e:
raise forms.ValidationError(e.args[0])

return avatar
if self.request.method == "POST":
avatar = self.request.FILES.get("avatar")
if not avatar:
raise forms.ValidationError(_("You have not selected an image to upload."))
# Validate file size
try:
check_file_size(avatar, settings.MAX_AVATAR_FILE_SIZE)
except FileTooLargeError as e:
raise forms.ValidationError(e.args[0])

return self.cleaned_data["avatar"]


USERS_PLACEHOLDER = _lazy("username")
Expand Down
2 changes: 1 addition & 1 deletion kitsune/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def edit_avatar(request, group_slug):
if not _user_can_edit(request.user, prof):
raise PermissionDenied

form = GroupAvatarForm(request.POST or None, request.FILES or None, instance=prof)
form = GroupAvatarForm(request.POST or None, request.FILES, instance=prof, request=request)

old_avatar_path = None

Expand Down

0 comments on commit 345ec81

Please sign in to comment.