Forms
ProfilePhotoForm
Form that performs the validation of the upload of the user’s profile photo. Validates the size and extension of the file.
class ProfilePhotoForm(forms.Form):
photo = forms.FileField(
validators=[
FileSizeValidator(max_size=int(app_settings.MAX_SIZE_FILE) * 1024 * 1024),
],
)
def clean_photo(self):
valid_content_types = ["image/png", "image/jpg", "image/jpeg"]
if self.cleaned_data["photo"]:
photo = self.cleaned_data["photo"]
content_type = photo.content_type
if content_type not in valid_content_types:
raise forms.ValidationError(
"Invalid file type, please choose another one."
)
else:
raise forms.ValidationError("This field is required.")
return self.cleaned_data["photo"]