1 from forum.utils.forms import NextUrlField, UserNameField, UserEmailField, SetPasswordForm
2 from forum.models import Question
3 from forum.modules import call_all_handlers
4 from django.contrib.contenttypes.models import ContentType
5 from django.utils.translation import ugettext as _
6 from django.contrib.auth import authenticate
7 from django import forms
10 class ClassicRegisterForm(SetPasswordForm):
11 """ legacy registration form """
14 username = UserNameField()
15 email = UserEmailField()
17 def __init__(self, *args, **kwargs):
18 super(ClassicRegisterForm, self).__init__(*args, **kwargs)
20 spam_fields = call_all_handlers('create_anti_spam_field')
22 spam_fields = dict(spam_fields)
23 for name,field in spam_fields.items():
24 self.fields[name] = field
26 self._anti_spam_fields = spam_fields.keys()
28 self._anti_spam_fields = []
30 def anti_spam_fields(self):
31 return [self[name] for name in self._anti_spam_fields]
33 class ClassicLoginForm(forms.Form):
34 """ legacy account signin form """
36 username = UserNameField(required=False,skip_clean=True)
37 password = forms.CharField(max_length=128,
38 widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
41 def __init__(self, data=None, files=None, auto_id='id_%s',
42 prefix=None, initial=None):
43 super(ClassicLoginForm, self).__init__(data, files, auto_id,
45 self.user_cache = None
47 def _clean_nonempty_field(self,field):
49 if field in self.cleaned_data:
50 value = self.cleaned_data[field].strip()
53 self.cleaned_data[field] = value
56 def clean_username(self):
57 return self._clean_nonempty_field('username')
59 def clean_password(self):
60 return self._clean_nonempty_field('password')
64 username = self.cleaned_data['username']
65 password = self.cleaned_data['password']
67 self.user_cache = None
68 if username and password:
69 user_ = authenticate(username=username, password=password)
72 del self.cleaned_data['username']
73 del self.cleaned_data['password']
75 error_list.insert(0,(_("Please enter valid username and password "
76 "(both are case-sensitive).")))
78 elif not user_.is_active:
79 error_list.append(_("This account is inactive."))
80 if len(error_list) > 0:
81 error_list.insert(0,_('Login failed.'))
83 self.user_cache = user_.user
85 error_list.append(_('This user is not a valid user'))
87 elif password == None and username == None:
88 error_list.append(_('Please enter username and password'))
89 elif password == None:
90 error_list.append(_('Please enter your password'))
91 elif username == None:
92 error_list.append(_('Please enter user name'))
93 if len(error_list) > 0:
94 self._errors['__all__'] = forms.util.ErrorList(error_list)
96 return self.cleaned_data
99 """ get authenticated user """
100 return self.user_cache