forms.py 1.2 KB

12345678910111213141516171819202122232425
  1. from PIL import Image
  2. from django.core.exceptions import ValidationError
  3. from django.utils.translation import ugettext_lazy as _
  4. import floppyforms as forms
  5. from misago.conf import settings
  6. from misago.forms import Form
  7. class UploadAvatarForm(Form):
  8. avatar_upload = forms.ImageField(label= _("Upload Image File"),
  9. help_text= _("Select image file on your computer you wish to use as forum avatar. You will be able to crop image after upload. Animations will be stripped."),
  10. error_messages={'invalid_image': _("Uploaded file is not correct image.")})
  11. error_source = 'avatar_upload'
  12. def clean_avatar_upload(self):
  13. image = self.cleaned_data.get('avatar_upload', False)
  14. if image:
  15. if image._size > settings.upload_limit * 1024:
  16. if settings.upload_limit > 1024:
  17. limit = '%s Mb' % "{:10.2f}".format(float(settings.upload_limit / 1024.0))
  18. else:
  19. limit = '%s Kb' % settings.upload_limit
  20. raise ValidationError(_("Avatar image cannot be larger than %(limit)s.") % {'limit': limit})
  21. else:
  22. raise ValidationError(_("Couldn't read uploaded image"))
  23. return image