1 from forum.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 from forum.settings import RECAPTCHA_PUB_KEY, RECAPTCHA_PRIV_KEY
12 class ClassicRegisterForm(SetPasswordForm):
13 """ legacy registration form """
16 username = UserNameField()
17 email = UserEmailField()
19 def __init__(self, *args, **kwargs):
20 super(ClassicRegisterForm, self).__init__(*args, **kwargs)
22 if len(str(RECAPTCHA_PUB_KEY.value)) > 0 and len(str(RECAPTCHA_PRIV_KEY.value)) > 0:
23 spam_fields = call_all_handlers('create_anti_spam_field')
25 spam_fields = dict(spam_fields)
26 for name, field in spam_fields.items():
27 self.fields[name] = field
29 self._anti_spam_fields = spam_fields.keys()
31 self._anti_spam_fields = []
33 def anti_spam_fields(self):
34 return [self[name] for name in self._anti_spam_fields]
36 class ClassicLoginForm(forms.Form):
37 """ legacy account signin form """
39 username = UserNameField(required=False, skip_clean=True)
40 password = forms.CharField(max_length=128,
41 widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
44 def __init__(self, data=None, files=None, auto_id='id_%s',
45 prefix=None, initial=None):
46 super(ClassicLoginForm, self).__init__(data, files, auto_id,
48 self.user_cache = None
50 def _clean_nonempty_field(self, field):
52 if field in self.cleaned_data:
53 value = self.cleaned_data[field].strip()
56 self.cleaned_data[field] = value
59 def clean_username(self):
60 return self._clean_nonempty_field('username')
62 def clean_password(self):
63 return self._clean_nonempty_field('password')
67 username = self.cleaned_data['username']
68 password = self.cleaned_data['password']
70 self.user_cache = None
71 if username and password:
72 user_ = authenticate(username=username, password=password)
75 del self.cleaned_data['username']
76 del self.cleaned_data['password']
78 error_list.insert(0, (_("Please enter valid username and password "
79 "(both are case-sensitive).")))
81 if len(error_list) > 0:
82 error_list.insert(0, _('Login failed.'))
84 self.user_cache = user_.user
86 error_list.append(_('This user is not a valid user'))
88 elif password == None and username == None:
89 error_list.append(_('Please enter username and password'))
90 elif password == None:
91 error_list.append(_('Please enter your password'))
92 elif username == None:
93 error_list.append(_('Please enter user name'))
94 if len(error_list) > 0:
95 self._errors['__all__'] = forms.util.ErrorList(error_list)
97 return self.cleaned_data
100 """ get authenticated user """
101 return self.user_cache