Commit 3d05b2c109e28ce6a41a8066d06eb21d5257d4f5

Authored by Zambom
1 parent de5f9a51

Adding image validation

Showing 2 changed files with 23 additions and 6 deletions   Show diff stats
users/forms.py
... ... @@ -5,6 +5,18 @@ from rolepermissions.shortcuts import assign_role
5 5 from .models import User
6 6  
7 7 class Validation(forms.ModelForm):
  8 + MIN_PASS_LENGTH = 8
  9 + MAX_UPLOAD_SIZE = 2*1024*1024
  10 +
  11 + def clean_image(self):
  12 + image = self.cleaned_data.get('image', False)
  13 +
  14 + if image:
  15 + if image._size > self.MAX_UPLOAD_SIZE:
  16 + raise forms.ValidationError(_("The image is too large. It should have less than 2MB."))
  17 +
  18 + return image
  19 +
8 20 def clean_password(self):
9 21 password = self.cleaned_data.get('password')
10 22  
... ... @@ -12,8 +24,8 @@ class Validation(forms.ModelForm):
12 24 return password
13 25  
14 26 # At least MIN_LENGTH long
15   - if len(password) < self.MIN_LENGTH:
16   - raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_LENGTH))
  27 + if len(password) < self.MIN_PASS_LENGTH:
  28 + raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_PASS_LENGTH))
17 29  
18 30 # At least one letter and one non-letter
19 31 first_isalpha = password[0].isalpha()
... ... @@ -38,7 +50,6 @@ class RegisterUserForm(Validation):
38 50 password = forms.CharField(label=_('Password'), widget = forms.PasswordInput)
39 51 password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput)
40 52  
41   - MIN_LENGTH = 8
42 53 is_edit = False
43 54  
44 55 def save(self, commit=True):
... ... @@ -58,7 +69,6 @@ class ProfileForm(Validation):
58 69 password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False)
59 70 password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False)
60 71  
61   - MIN_LENGTH = 8
62 72 is_edit = True
63 73  
64 74 def save(self, commit=True):
... ...
users/models.py
... ... @@ -2,16 +2,23 @@ import re
2 2  
3 3 from django.db import models
4 4 from django.core import validators
  5 +from django.core.exceptions import ValidationError
5 6 from django.utils.translation import ugettext_lazy as _
6 7 from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin
7 8 from django.contrib.staticfiles.templatetags.staticfiles import static
8 9  
  10 +def validate_img_extension(value):
  11 + valid_formats = ['image/jpeg','image/x-citrix-jpeg','image/png','image/x-citrix-png','image/x-png']
  12 +
  13 + if not value.file.content_type in valid_formats:
  14 + raise ValidationError(_('File not supported.'))
  15 +
9 16 class User(AbstractBaseUser, PermissionsMixin):
10 17  
11 18 email = models.EmailField(_('Mail'), unique = True, validators = [
12 19 validators.RegexValidator(
13 20 re.compile('^[\w.@+-]+$'),
14   - _('Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .')
  21 + _('Type a valid email. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .')
15 22 , 'invalid'
16 23 )
17 24 ], help_text = _('Your email address that will be used to access the platform'))
... ... @@ -19,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin):
19 26 last_name = models.CharField(_('Last Name'), max_length = 100)
20 27 social_name = models.CharField(_('Social Name'), max_length = 100, blank = True, null = True)
21 28 description = models.TextField(_('Description'), blank = True)
22   - image = models.ImageField(verbose_name = _('Photo'), null=True, blank = True, upload_to = 'users/')
  29 + image = models.ImageField(verbose_name = _('Photo'), null=True, blank = True, upload_to = 'users/', validators = [validate_img_extension])
23 30 date_created = models.DateTimeField(_('Create Date'), auto_now_add = True)
24 31 last_update = models.DateTimeField(_('Last Update'), auto_now = True)
25 32 show_email = models.IntegerField(_('Show email?'), null = True, blank = True, choices = ((1, _('Allow everyone to see my address')), (2, _('Only classmates can see my address')), (3, _('Nobody can see my address'))))
... ...