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 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 # Try importing the ReCapthca public and private keys, Import Error will be raised in case it has been disabled
22 from forum.settings import RECAPTCHA_PUB_KEY, RECAPTCHA_PRIV_KEY
23 recaptcha_enabled = len(str(RECAPTCHA_PUB_KEY.value)) > 0 and len(str(RECAPTCHA_PRIV_KEY.value)) > 0
25 recaptcha_enabled = False
28 spam_fields = call_all_handlers('create_anti_spam_field')
30 spam_fields = dict(spam_fields)
31 for name, field in spam_fields.items():
32 self.fields[name] = field
34 self._anti_spam_fields = spam_fields.keys()
36 self._anti_spam_fields = []
38 def anti_spam_fields(self):
39 return [self[name] for name in self._anti_spam_fields]
41 class ClassicLoginForm(forms.Form):
42 """ legacy account signin form """
44 username = UserNameField(required=False, skip_clean=True)
45 password = forms.CharField(max_length=128,
46 widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
49 def __init__(self, data=None, files=None, auto_id='id_%s',
50 prefix=None, initial=None):
51 super(ClassicLoginForm, self).__init__(data, files, auto_id,
53 self.user_cache = None
55 def _clean_nonempty_field(self, field):
57 if field in self.cleaned_data:
58 value = self.cleaned_data[field].strip()
61 self.cleaned_data[field] = value
64 def clean_username(self):
65 return self._clean_nonempty_field('username')
67 def clean_password(self):
68 return self._clean_nonempty_field('password')
72 username = self.cleaned_data['username']
73 password = self.cleaned_data['password']
75 self.user_cache = None
76 if username and password:
77 user_ = authenticate(username=username, password=password)
80 del self.cleaned_data['username']
81 del self.cleaned_data['password']
83 error_list.insert(0, (_("Please enter valid username and password "
84 "(both are case-sensitive).")))
86 if len(error_list) > 0:
87 error_list.insert(0, _('Login failed.'))
89 self.user_cache = user_.user
91 error_list.append(_('This user is not a valid user'))
93 elif password == None and username == None:
94 error_list.append(_('Please enter username and password'))
95 elif password == None:
96 error_list.append(_('Please enter your password'))
97 elif username == None:
98 error_list.append(_('Please enter user name'))
99 if len(error_list) > 0:
100 self._errors['__all__'] = forms.util.ErrorList(error_list)
102 return self.cleaned_data
105 """ get authenticated user """
106 return self.user_cache