]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/localauth/forms.py
check whether the recaptcha public and private keys have been provided before populat...
[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 from forum.settings import RECAPTCHA_PUB_KEY, RECAPTCHA_PRIV_KEY
11
12 class ClassicRegisterForm(SetPasswordForm):
13     """ legacy registration form """
14
15     next = NextUrlField()
16     username = UserNameField()
17     email = UserEmailField()
18
19     def __init__(self, *args, **kwargs):
20         super(ClassicRegisterForm, self).__init__(*args, **kwargs)
21
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')
24             if spam_fields:
25                 spam_fields = dict(spam_fields)
26                 for name, field in spam_fields.items():
27                     self.fields[name] = field
28
29                 self._anti_spam_fields = spam_fields.keys()
30             else:
31                 self._anti_spam_fields = []
32
33     def anti_spam_fields(self):
34         return [self[name] for name in self._anti_spam_fields]
35
36 class ClassicLoginForm(forms.Form):
37     """ legacy account signin form """
38     next = NextUrlField()
39     username = UserNameField(required=False, skip_clean=True)
40     password = forms.CharField(max_length=128,
41                                widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
42                                required=False)
43
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,
47                                                prefix, initial)
48         self.user_cache = None
49
50     def _clean_nonempty_field(self, field):
51         value = None
52         if field in self.cleaned_data:
53             value = self.cleaned_data[field].strip()
54             if value == '':
55                 value = None
56         self.cleaned_data[field] = value
57         return value
58
59     def clean_username(self):
60         return self._clean_nonempty_field('username')
61
62     def clean_password(self):
63         return self._clean_nonempty_field('password')
64
65     def clean(self):
66         error_list = []
67         username = self.cleaned_data['username']
68         password = self.cleaned_data['password']
69
70         self.user_cache = None
71         if username and password:
72             user_ = authenticate(username=username, password=password)
73
74             if user_ is None:
75                 del self.cleaned_data['username']
76                 del self.cleaned_data['password']
77
78                 error_list.insert(0, (_("Please enter valid username and password "
79                 "(both are case-sensitive).")))
80
81             if len(error_list) > 0:
82                 error_list.insert(0, _('Login failed.'))
83             try:
84                 self.user_cache = user_.user
85             except:
86                 error_list.append(_('This user is not a valid user'))
87
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)
96
97         return self.cleaned_data
98
99     def get_user(self):
100         """ get authenticated user """
101         return self.user_cache