forms.py 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. from PIL import Image
  2. from django import forms
  3. from django.conf import settings
  4. from django.core.exceptions import ValidationError
  5. from django.utils.translation import ugettext_lazy as _
  6. from misago.forms import Form
  7. class UploadAvatarForm(Form):
  8. avatar_upload = forms.ImageField(error_messages={'invalid_image': _("Uploaded file is not correct image.")})
  9. error_source = 'avatar_upload'
  10. layout = [
  11. [
  12. None,
  13. [
  14. ('avatar_upload', {'label': _("Upload Image File"), '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.")}),
  15. ],
  16. ],
  17. ]
  18. def clean_avatar_upload(self):
  19. image = self.cleaned_data.get('avatar_upload',False)
  20. if image:
  21. if image._size > self.request.settings.upload_limit * 1024:
  22. if self.request.settings.upload_limit > 1024:
  23. limit = '%s Mb' % "{:10.2f}".format(float(self.request.settings.upload_limit / 1024.0))
  24. else:
  25. limit = '%s Kb' % self.request.settings.upload_limit
  26. raise ValidationError(_("Avatar image cannot be larger than %(limit)s.") % {'limit': limit})
  27. return image
  28. else:
  29. raise ValidationError(_("Couldn't read uploaded image"))