1 from django import forms
3 from django.utils.translation import ugettext as _
4 from django.utils.safestring import mark_safe
5 from forum import settings
6 from forum.models import User
7 from forum.modules import call_all_handlers
11 DEFAULT_NEXT = getattr(settings, 'APP_BASE_URL')
15 next = urllib.unquote(next)
16 if not isinstance(next, unicode):
17 next = unicode(next, 'utf-8')
19 if next.startswith('/'):
23 def get_next_url(request):
24 return clean_next(request.REQUEST.get('next'))
26 class StrippedNonEmptyCharField(forms.CharField):
27 def clean(self,value):
29 if self.required and value == '':
30 raise forms.ValidationError(_('this field is required'))
33 class NextUrlField(forms.CharField):
35 super(NextUrlField,self).__init__(max_length = 255,widget = forms.HiddenInput(),required = False)
36 def clean(self,value):
37 return clean_next(value)
39 login_form_widget_attrs = { 'class': 'required login' }
40 username_re = re.compile(r'^[\-\w\s ]+$', re.UNICODE)
42 class UserNameField(StrippedNonEmptyCharField):
43 def __init__(self,db_model=User, db_field='username', must_exist=False,skip_clean=False,label=_('choose a username'),**kw):
44 self.must_exist = must_exist
45 self.skip_clean = skip_clean
46 self.db_model = db_model
47 self.db_field = db_field
48 error_messages={'required':_('user name is required'),
49 'taken':_('sorry, this name is taken, please choose another'),
50 'forbidden':_('sorry, this name is not allowed, please choose another'),
51 'missing':_('sorry, there is no user with this name'),
52 'multiple-taken':_('sorry, we have a serious error - user name is taken by several users'),
53 'invalid':_('user name can only consist of letters, empty space, hyphens and underscore'),
54 'toshort':_('user name is to short, please use at least %d characters') % settings.MIN_USERNAME_LENGTH
56 if 'error_messages' in kw:
57 error_messages.update(kw['error_messages'])
58 del kw['error_messages']
59 super(UserNameField,self).__init__(max_length=30,
60 widget=forms.TextInput(attrs=login_form_widget_attrs),
62 error_messages=error_messages,
66 def clean(self,username):
67 """ validate username """
68 if self.skip_clean == True:
70 if hasattr(self, 'user_instance') and isinstance(self.user_instance, User):
71 if username == self.user_instance.username:
74 username = super(UserNameField, self).clean(username)
75 except forms.ValidationError:
76 raise forms.ValidationError(self.error_messages['required'])
77 if len(username) < settings.MIN_USERNAME_LENGTH:
78 raise forms.ValidationError(self.error_messages['toshort'])
79 if self.required and not username_re.match(username):
80 raise forms.ValidationError(self.error_messages['invalid'])
81 if username in settings.RESERVED_USERNAMES:
82 raise forms.ValidationError(self.error_messages['forbidden'])
84 user = self.db_model.objects.get(
85 **{'%s' % self.db_field : username}
91 raise forms.ValidationError(self.error_messages['taken'])
92 except self.db_model.DoesNotExist:
94 raise forms.ValidationError(self.error_messages['missing'])
97 except self.db_model.MultipleObjectsReturned:
98 raise forms.ValidationError(self.error_messages['multiple-taken'])
100 class UserEmailField(forms.EmailField):
101 def __init__(self,skip_clean=False,**kw):
102 self.skip_clean = skip_clean
103 super(UserEmailField,self).__init__(widget=forms.TextInput(attrs=dict(login_form_widget_attrs,
104 maxlength=200)), label=mark_safe(_('your email address')),
105 error_messages={'required':_('email address is required'),
106 'invalid':_('please enter a valid email address'),
107 'taken':_('this email is already used by someone else, please choose another'),
112 def clean(self,email):
113 """ validate if email exist in database
115 return: raise error if it exist """
116 email = super(UserEmailField,self).clean(email.strip())
119 if settings.EMAIL_UNIQUE == True:
121 user = User.objects.get(email = email)
122 raise forms.ValidationError(self.error_messages['taken'])
123 except User.DoesNotExist:
125 except User.MultipleObjectsReturned:
126 raise forms.ValidationError(self.error_messages['taken'])
130 class UserRealNameField(StrippedNonEmptyCharField):
131 def __init__(self, db_model=User, db_field='real_name', must_exist=True, skip_clean=False, label=_('Your real name'),**kw):
132 self.must_exist = must_exist
133 self.skip_clean = skip_clean
134 self.db_model = db_model
135 self.db_field = db_field
136 error_messages={'required':_('Real name is required')
138 if 'error_messages' in kw:
139 error_messages.update(kw['error_messages'])
140 del kw['error_messages']
141 super(UserRealNameField,self).__init__(max_length=100,
142 widget=forms.TextInput(attrs=login_form_widget_attrs),
144 error_messages=error_messages,
148 def clean(self, real_name):
149 if self.skip_clean == True:
152 return super(UserRealNameField, self).clean(real_name)
153 except forms.ValidationError:
154 raise forms.ValidationError(self.error_messages['required'])
156 class SetPasswordForm(forms.Form):
157 password1 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
158 label=_('choose password'),
159 error_messages={'required':_('password is required')},
161 password2 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
162 label=mark_safe(_('retype password')),
163 error_messages={'required':_('please, retype your password'),
164 'nomatch':_('sorry, entered passwords did not match, please try again')},
167 def __init__(self, data=None, user=None, *args, **kwargs):
168 super(SetPasswordForm, self).__init__(data, *args, **kwargs)
170 def clean_password2(self):
172 Validates that the two password inputs match.
175 if 'password1' in self.cleaned_data:
176 if self.cleaned_data['password1'] == self.cleaned_data['password2']:
177 self.password = self.cleaned_data['password2']
178 self.cleaned_data['password'] = self.cleaned_data['password2']
179 return self.cleaned_data['password2']
181 del self.cleaned_data['password2']
182 raise forms.ValidationError(self.fields['password2'].error_messages['nomatch'])
184 return self.cleaned_data['password2']
186 class SimpleCaptchaForm(forms.Form):
189 def __init__(self, *args, **kwargs):
190 super(SimpleCaptchaForm, self).__init__(*args, **kwargs)
192 spam_fields = call_all_handlers('create_anti_spam_field')
194 spam_fields = dict(spam_fields)
195 for name, field in spam_fields.items():
196 self.fields[name] = field
198 self._anti_spam_fields = spam_fields.keys()
200 self._anti_spam_fields = []