Commit 3d05b2c109e28ce6a41a8066d06eb21d5257d4f5
1 parent
de5f9a51
Exists in
master
and in
3 other branches
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,6 +5,18 @@ from rolepermissions.shortcuts import assign_role | ||
5 | from .models import User | 5 | from .models import User |
6 | 6 | ||
7 | class Validation(forms.ModelForm): | 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 | def clean_password(self): | 20 | def clean_password(self): |
9 | password = self.cleaned_data.get('password') | 21 | password = self.cleaned_data.get('password') |
10 | 22 | ||
@@ -12,8 +24,8 @@ class Validation(forms.ModelForm): | @@ -12,8 +24,8 @@ class Validation(forms.ModelForm): | ||
12 | return password | 24 | return password |
13 | 25 | ||
14 | # At least MIN_LENGTH long | 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 | # At least one letter and one non-letter | 30 | # At least one letter and one non-letter |
19 | first_isalpha = password[0].isalpha() | 31 | first_isalpha = password[0].isalpha() |
@@ -38,7 +50,6 @@ class RegisterUserForm(Validation): | @@ -38,7 +50,6 @@ class RegisterUserForm(Validation): | ||
38 | password = forms.CharField(label=_('Password'), widget = forms.PasswordInput) | 50 | password = forms.CharField(label=_('Password'), widget = forms.PasswordInput) |
39 | password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) | 51 | password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) |
40 | 52 | ||
41 | - MIN_LENGTH = 8 | ||
42 | is_edit = False | 53 | is_edit = False |
43 | 54 | ||
44 | def save(self, commit=True): | 55 | def save(self, commit=True): |
@@ -58,7 +69,6 @@ class ProfileForm(Validation): | @@ -58,7 +69,6 @@ class ProfileForm(Validation): | ||
58 | password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) | 69 | password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) |
59 | password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) | 70 | password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) |
60 | 71 | ||
61 | - MIN_LENGTH = 8 | ||
62 | is_edit = True | 72 | is_edit = True |
63 | 73 | ||
64 | def save(self, commit=True): | 74 | def save(self, commit=True): |
users/models.py
@@ -2,16 +2,23 @@ import re | @@ -2,16 +2,23 @@ import re | ||
2 | 2 | ||
3 | from django.db import models | 3 | from django.db import models |
4 | from django.core import validators | 4 | from django.core import validators |
5 | +from django.core.exceptions import ValidationError | ||
5 | from django.utils.translation import ugettext_lazy as _ | 6 | from django.utils.translation import ugettext_lazy as _ |
6 | from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin | 7 | from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin |
7 | from django.contrib.staticfiles.templatetags.staticfiles import static | 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 | class User(AbstractBaseUser, PermissionsMixin): | 16 | class User(AbstractBaseUser, PermissionsMixin): |
10 | 17 | ||
11 | email = models.EmailField(_('Mail'), unique = True, validators = [ | 18 | email = models.EmailField(_('Mail'), unique = True, validators = [ |
12 | validators.RegexValidator( | 19 | validators.RegexValidator( |
13 | re.compile('^[\w.@+-]+$'), | 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 | , 'invalid' | 22 | , 'invalid' |
16 | ) | 23 | ) |
17 | ], help_text = _('Your email address that will be used to access the platform')) | 24 | ], help_text = _('Your email address that will be used to access the platform')) |
@@ -19,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin): | @@ -19,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin): | ||
19 | last_name = models.CharField(_('Last Name'), max_length = 100) | 26 | last_name = models.CharField(_('Last Name'), max_length = 100) |
20 | social_name = models.CharField(_('Social Name'), max_length = 100, blank = True, null = True) | 27 | social_name = models.CharField(_('Social Name'), max_length = 100, blank = True, null = True) |
21 | description = models.TextField(_('Description'), blank = True) | 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 | date_created = models.DateTimeField(_('Create Date'), auto_now_add = True) | 30 | date_created = models.DateTimeField(_('Create Date'), auto_now_add = True) |
24 | last_update = models.DateTimeField(_('Last Update'), auto_now = True) | 31 | last_update = models.DateTimeField(_('Last Update'), auto_now = True) |
25 | 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')))) | 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')))) |