]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/localauth/forms.py
Remove footer link to squatted domain
[osqa.git] / forum_modules / localauth / forms.py
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
8 import logging
9
10 class ClassicRegisterForm(SetPasswordForm):
11     """ legacy registration form """
12
13     next = NextUrlField()
14     username = UserNameField()
15     email = UserEmailField()
16
17     def __init__(self, *args, **kwargs):
18         super(ClassicRegisterForm, self).__init__(*args, **kwargs)
19
20         # Try importing the ReCapthca public and private keys, Import Error will be raised in case it has been disabled
21         try:
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
24         except ImportError:
25             recaptcha_enabled = False
26
27         if recaptcha_enabled:
28             spam_fields = call_all_handlers('create_anti_spam_field')
29             if spam_fields:
30                 spam_fields = dict(spam_fields)
31                 for name, field in spam_fields.items():
32                     self.fields[name] = field
33
34                 self._anti_spam_fields = spam_fields.keys()
35             else:
36                 self._anti_spam_fields = []
37
38     def anti_spam_fields(self):
39         return [self[name] for name in self._anti_spam_fields]
40
41 class ClassicLoginForm(forms.Form):
42     """ legacy account signin form """
43     next = NextUrlField()
44     username = UserNameField(required=False, skip_clean=True)
45     password = forms.CharField(max_length=128,
46                                widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
47                                required=False)
48
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,
52                                                prefix, initial)
53         self.user_cache = None
54
55     def _clean_nonempty_field(self, field):
56         value = None
57         if field in self.cleaned_data:
58             value = self.cleaned_data[field].strip()
59             if value == '':
60                 value = None
61         self.cleaned_data[field] = value
62         return value
63
64     def clean_username(self):
65         return self._clean_nonempty_field('username')
66
67     def clean_password(self):
68         return self._clean_nonempty_field('password')
69
70     def clean(self):
71         error_list = []
72         username = self.cleaned_data['username']
73         password = self.cleaned_data['password']
74
75         self.user_cache = None
76         if username and password:
77             user_ = authenticate(username=username, password=password)
78
79             if user_ is None:
80                 del self.cleaned_data['username']
81                 del self.cleaned_data['password']
82
83                 error_list.insert(0, (_("Please enter valid username and password "
84                 "(both are case-sensitive).")))
85
86             if len(error_list) > 0:
87                 error_list.insert(0, _('Login failed.'))
88             try:
89                 self.user_cache = user_.user
90             except:
91                 error_list.append(_('This user is not a valid user'))
92
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)
101
102         return self.cleaned_data
103
104     def get_user(self):
105         """ get authenticated user """
106         return self.user_cache