PageRenderTime 63ms CodeModel.GetById 45ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/users/forms.py

https://github.com/chengwang/kitsune
Python | 195 lines | 170 code | 15 blank | 10 comment | 4 complexity | 47d775738aa86b3a03c118d874d516e5 MD5 | raw file
  1import re
  2
  3from django import forms
  4from django.conf import settings
  5from django.contrib.auth import authenticate, forms as auth_forms
  6from django.contrib.auth.models import User
  7
  8from tower import ugettext as _, ugettext_lazy as _lazy
  9
 10from sumo.widgets import ImageWidget
 11from upload.forms import clean_image_extension
 12from upload.utils import check_file_size, FileTooLargeError
 13from users.models import Profile
 14from users.widgets import FacebookURLWidget, TwitterURLWidget
 15
 16
 17USERNAME_INVALID = _lazy(u'Username may contain only letters, '
 18                         'numbers and @/./+/-/_ characters.')
 19USERNAME_REQUIRED = _lazy(u'Username is required.')
 20USERNAME_SHORT = _lazy(u'Username is too short (%(show_value)s characters). '
 21                       'It must be at least %(limit_value)s characters.')
 22USERNAME_LONG = _lazy(u'Username is too long (%(show_value)s characters). '
 23                      'It must be %(limit_value)s characters or less.')
 24EMAIL_REQUIRED = _lazy(u'Email address is required.')
 25EMAIL_SHORT = _lazy(u'Email address is too short (%(show_value)s characters). '
 26                    'It must be at least %(limit_value)s characters.')
 27EMAIL_LONG = _lazy(u'Email address is too long (%(show_value)s characters). '
 28                   'It must be %(limit_value)s characters or less.')
 29PASSWD_REQUIRED = _lazy(u'Password is required.')
 30PASSWD2_REQUIRED = _lazy(u'Please enter your password twice.')
 31
 32
 33class RegisterForm(forms.ModelForm):
 34    """A user registration form that requires unique email addresses.
 35
 36    The default Django user creation form does not require an email address,
 37    let alone that it be unique. This form does, and sets a minimum length
 38    for usernames.
 39
 40    """
 41    username = forms.RegexField(
 42        label=_lazy(u'Username:'), max_length=30, min_length=4,
 43        regex=r'^[\w.@+-]+$',
 44        help_text=_lazy(u'Required. 30 characters or fewer. Letters, digits '
 45                         'and @/./+/-/_ only.'),
 46        error_messages={'invalid': USERNAME_INVALID,
 47                        'required': USERNAME_REQUIRED,
 48                        'min_length': USERNAME_SHORT,
 49                        'max_length': USERNAME_LONG})
 50    email = forms.EmailField(label=_lazy(u'Email address:'),
 51                             error_messages={'required': EMAIL_REQUIRED,
 52                                             'min_length': EMAIL_SHORT,
 53                                             'max_length': EMAIL_LONG})
 54    password = forms.CharField(label=_lazy(u'Password:'),
 55                               widget=forms.PasswordInput(
 56                                   render_value=False),
 57                               error_messages={'required': PASSWD_REQUIRED})
 58    password2 = forms.CharField(label=_lazy(u'Repeat password:'),
 59                                widget=forms.PasswordInput(
 60                                    render_value=False),
 61                                error_messages={'required': PASSWD2_REQUIRED},
 62                                help_text=_lazy(u'Enter the same password as '
 63                                                 'above, for verification.'))
 64
 65    class Meta(object):
 66        model = User
 67        fields = ('username', 'password', 'password2', 'email')
 68
 69    def clean(self):
 70        super(RegisterForm, self).clean()
 71        password = self.cleaned_data.get('password')
 72        password2 = self.cleaned_data.get('password2')
 73
 74        if not password == password2:
 75            raise forms.ValidationError(_('Passwords must match.'))
 76
 77        return self.cleaned_data
 78
 79    def clean_email(self):
 80        email = self.cleaned_data['email']
 81        if User.objects.filter(email=email).exists():
 82            raise forms.ValidationError(_('A user with that email address '
 83                                          'already exists.'))
 84        return email
 85
 86    def __init__(self,  request=None, *args, **kwargs):
 87        super(RegisterForm, self).__init__(request, auto_id='id_for_%s',
 88                                           *args, **kwargs)
 89
 90
 91class AuthenticationForm(auth_forms.AuthenticationForm):
 92    """Overrides the default django form.
 93
 94    * Doesn't prefill password on validation error.
 95    * Allows logging in inactive users (initialize with `only_active=False`).
 96    """
 97    password = forms.CharField(label=_lazy(u"Password"),
 98                               widget=forms.PasswordInput(render_value=False))
 99
100    def __init__(self, request=None, only_active=True, *args, **kwargs):
101        self.only_active = only_active
102        super(AuthenticationForm, self).__init__(request, *args, **kwargs)
103
104    def clean(self):
105        username = self.cleaned_data.get('username')
106        password = self.cleaned_data.get('password')
107
108        if username and password:
109            self.user_cache = authenticate(username=username,
110                                           password=password)
111            if self.user_cache is None:
112                raise forms.ValidationError(
113                    _('Please enter a correct username and password. Note '
114                      'that both fields are case-sensitive.'))
115            elif self.only_active and not self.user_cache.is_active:
116                raise forms.ValidationError(_('This account is inactive.'))
117
118        if self.request:
119            if not self.request.session.test_cookie_worked():
120                raise forms.ValidationError(
121                    _("Your Web browser doesn't appear to have cookies "
122                      "enabled. Cookies are required for logging in."))
123
124        return self.cleaned_data
125
126
127class ProfileForm(forms.ModelForm):
128    """The form for editing the user's profile."""
129
130    class Meta(object):
131        model = Profile
132        exclude = ('user', 'livechat_id', 'avatar')
133        widgets = {
134            'twitter': TwitterURLWidget,
135            'facebook': FacebookURLWidget,
136        }
137
138    def clean_twitter(self):
139        twitter = self.cleaned_data['twitter']
140        if twitter and not re.match(TwitterURLWidget.pattern, twitter):
141            raise forms.ValidationError(_(u'Please enter a twitter.com URL.'))
142        return twitter
143
144    def clean_facebook(self):
145        facebook = self.cleaned_data['facebook']
146        if facebook and not re.match(FacebookURLWidget.pattern, facebook):
147            raise forms.ValidationError(_(u'Please enter a facebook.com URL.'))
148        return facebook
149
150
151class AvatarForm(forms.ModelForm):
152    """The form for editing the user's avatar."""
153    avatar = forms.ImageField(required=True, widget=ImageWidget)
154
155    def __init__(self, *args, **kwargs):
156        super(AvatarForm, self).__init__(*args, **kwargs)
157        self.fields['avatar'].help_text = (
158            u'Your avatar will be resized to {size}x{size}'.format(
159                size=settings.AVATAR_SIZE))
160
161    class Meta(object):
162        model = Profile
163        fields = ('avatar',)
164
165    def clean_avatar(self):
166        if not ('avatar' in self.cleaned_data and self.cleaned_data['avatar']):
167            return self.cleaned_data['avatar']
168        try:
169            check_file_size(self.cleaned_data['avatar'],
170                            settings.MAX_AVATAR_FILE_SIZE)
171        except FileTooLargeError as e:
172            raise forms.ValidationError(e.args[0])
173        clean_image_extension(self.cleaned_data.get('avatar'))
174        return self.cleaned_data['avatar']
175
176
177class EmailConfirmationForm(forms.Form):
178    """A simple form that requires an email address."""
179    email = forms.EmailField(label=_lazy(u'Email address:'))
180
181
182class EmailChangeForm(forms.Form):
183    """A simple form that requires an email address and validates that it is
184    not the current user's email."""
185    email = forms.EmailField(label=_lazy(u'Email address:'))
186
187    def __init__(self, user, *args, **kwargs):
188        super(EmailChangeForm, self).__init__(*args, **kwargs)
189        self.user = user
190
191    def clean_email(self):
192        if self.user.email == self.cleaned_data['email']:
193            raise forms.ValidationError(_('This is your current email.'))
194
195        return self.cleaned_data['email']