diff --git a/users/forms.py b/users/forms.py index d837fe9..3bc1720 100644 --- a/users/forms.py +++ b/users/forms.py @@ -5,6 +5,18 @@ from rolepermissions.shortcuts import assign_role from .models import User class Validation(forms.ModelForm): + MIN_PASS_LENGTH = 8 + MAX_UPLOAD_SIZE = 2*1024*1024 + + def clean_image(self): + image = self.cleaned_data.get('image', False) + + if image: + if image._size > self.MAX_UPLOAD_SIZE: + raise forms.ValidationError(_("The image is too large. It should have less than 2MB.")) + + return image + def clean_password(self): password = self.cleaned_data.get('password') @@ -12,8 +24,8 @@ class Validation(forms.ModelForm): return password # At least MIN_LENGTH long - if len(password) < self.MIN_LENGTH: - raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_LENGTH)) + if len(password) < self.MIN_PASS_LENGTH: + raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_PASS_LENGTH)) # At least one letter and one non-letter first_isalpha = password[0].isalpha() @@ -38,7 +50,6 @@ class RegisterUserForm(Validation): password = forms.CharField(label=_('Password'), widget = forms.PasswordInput) password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) - MIN_LENGTH = 8 is_edit = False def save(self, commit=True): @@ -58,7 +69,6 @@ class ProfileForm(Validation): password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) - MIN_LENGTH = 8 is_edit = True def save(self, commit=True): diff --git a/users/models.py b/users/models.py index edc44d6..320f91c 100644 --- a/users/models.py +++ b/users/models.py @@ -2,16 +2,23 @@ import re from django.db import models from django.core import validators +from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin from django.contrib.staticfiles.templatetags.staticfiles import static +def validate_img_extension(value): + valid_formats = ['image/jpeg','image/x-citrix-jpeg','image/png','image/x-citrix-png','image/x-png'] + + if not value.file.content_type in valid_formats: + raise ValidationError(_('File not supported.')) + class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_('Mail'), unique = True, validators = [ validators.RegexValidator( re.compile('^[\w.@+-]+$'), - _('Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .') + _('Type a valid email. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .') , 'invalid' ) ], help_text = _('Your email address that will be used to access the platform')) @@ -19,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin): last_name = models.CharField(_('Last Name'), max_length = 100) social_name = models.CharField(_('Social Name'), max_length = 100, blank = True, null = True) description = models.TextField(_('Description'), blank = True) - image = models.ImageField(verbose_name = _('Photo'), null=True, blank = True, upload_to = 'users/') + image = models.ImageField(verbose_name = _('Photo'), null=True, blank = True, upload_to = 'users/', validators = [validate_img_extension]) date_created = models.DateTimeField(_('Create Date'), auto_now_add = True) last_update = models.DateTimeField(_('Last Update'), auto_now = True) 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')))) -- libgit2 0.21.2