1 from django import forms
3 from django.utils.translation import ugettext as _
4 from django.utils.safestring import mark_safe
5 from django.conf import settings
6 from django.http import str_to_unicode
7 from forum.models import User
10 DEFAULT_NEXT = '/' + getattr(settings, 'FORUM_SCRIPT_ALIAS')
14 next = str_to_unicode(urllib.unquote(next), 'utf-8')
16 if next.startswith('/'):
20 def get_next_url(request):
21 return clean_next(request.REQUEST.get('next'))
23 class StrippedNonEmptyCharField(forms.CharField):
24 def clean(self,value):
26 if self.required and value == '':
27 raise forms.ValidationError(_('this field is required'))
30 class NextUrlField(forms.CharField):
32 super(NextUrlField,self).__init__(max_length = 255,widget = forms.HiddenInput(),required = False)
33 def clean(self,value):
34 return clean_next(value)
36 login_form_widget_attrs = { 'class': 'required login' }
37 username_re = re.compile(r'^[\w ]+$')
39 class UserNameField(StrippedNonEmptyCharField):
40 RESERVED_NAMES = (u'fuck', u'shit', u'ass', u'sex', u'add',
41 u'edit', u'save', u'delete', u'manage', u'update', 'remove', 'new')
42 def __init__(self,db_model=User, db_field='username', must_exist=False,skip_clean=False,label=_('choose a username'),**kw):
43 self.must_exist = must_exist
44 self.skip_clean = skip_clean
45 self.db_model = db_model
46 self.db_field = db_field
47 error_messages={'required':_('user name is required'),
48 'taken':_('sorry, this name is taken, please choose another'),
49 'forbidden':_('sorry, this name is not allowed, please choose another'),
50 'missing':_('sorry, there is no user with this name'),
51 'multiple-taken':_('sorry, we have a serious error - user name is taken by several users'),
52 'invalid':_('user name can only consist of letters, empty space and underscore'),
54 if 'error_messages' in kw:
55 error_messages.update(kw['error_messages'])
56 del kw['error_messages']
57 super(UserNameField,self).__init__(max_length=30,
58 widget=forms.TextInput(attrs=login_form_widget_attrs),
60 error_messages=error_messages,
64 def clean(self,username):
65 """ validate username """
66 if self.skip_clean == True:
68 if hasattr(self, 'user_instance') and isinstance(self.user_instance, User):
69 if username == self.user_instance.username:
72 username = super(UserNameField, self).clean(username)
73 except forms.ValidationError:
74 raise forms.ValidationError(self.error_messages['required'])
75 if self.required and not username_re.search(username):
76 raise forms.ValidationError(self.error_messages['invalid'])
77 if username in self.RESERVED_NAMES:
78 raise forms.ValidationError(self.error_messages['forbidden'])
80 user = self.db_model.objects.get(
81 **{'%s' % self.db_field : username}
87 raise forms.ValidationError(self.error_messages['taken'])
88 except self.db_model.DoesNotExist:
90 raise forms.ValidationError(self.error_messages['missing'])
93 except self.db_model.MultipleObjectsReturned:
94 raise forms.ValidationError(self.error_messages['multiple-taken'])
96 class UserEmailField(forms.EmailField):
97 def __init__(self,skip_clean=False,**kw):
98 self.skip_clean = skip_clean
99 super(UserEmailField,self).__init__(widget=forms.TextInput(attrs=dict(login_form_widget_attrs,
100 maxlength=200)), label=mark_safe(_('your email address')),
101 error_messages={'required':_('email address is required'),
102 'invalid':_('please enter a valid email address'),
103 'taken':_('this email is already used by someone else, please choose another'),
108 def clean(self,email):
109 """ validate if email exist in database
111 return: raise error if it exist """
112 email = super(UserEmailField,self).clean(email.strip())
115 if settings.EMAIL_UNIQUE == True:
117 user = User.objects.get(email = email)
118 raise forms.ValidationError(self.error_messsages['taken'])
119 except User.DoesNotExist:
121 except User.MultipleObjectsReturned:
122 raise forms.ValidationError(self.error_messages['taken'])
126 class SetPasswordForm(forms.Form):
127 password1 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
128 label=_('choose password'),
129 error_messages={'required':_('password is required')},
131 password2 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
132 label=mark_safe(_('retype password')),
133 error_messages={'required':_('please, retype your password'),
134 'nomatch':_('sorry, entered passwords did not match, please try again')},
137 def __init__(self, data=None, user=None, *args, **kwargs):
138 super(SetPasswordForm, self).__init__(data, *args, **kwargs)
140 def clean_password2(self):
142 Validates that the two password inputs match.
145 if 'password1' in self.cleaned_data:
146 if self.cleaned_data['password1'] == self.cleaned_data['password2']:
147 self.password = self.cleaned_data['password2']
148 self.cleaned_data['password'] = self.cleaned_data['password2']
149 return self.cleaned_data['password2']
151 del self.cleaned_data['password2']
152 raise forms.ValidationError(self.fields['password2'].error_messages['nomatch'])
154 return self.cleaned_data['password2']