]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/localauth/forms.py
Fixes OSQA 307, AttributeError: 'ChangePasswordForm' object has no attribute 'cleaned...
[osqa.git] / forum_modules / localauth / forms.py
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
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         spam_fields = call_all_handlers('create_anti_spam_field')
21         if spam_fields:
22             spam_fields = dict(spam_fields)
23             for name,field in spam_fields.items():
24                 self.fields[name] = field
25
26             self._anti_spam_fields = spam_fields.keys()
27         else:
28             self._anti_spam_fields = []
29
30     def anti_spam_fields(self):
31         return [self[name] for name in self._anti_spam_fields]
32
33 class ClassicLoginForm(forms.Form):
34     """ legacy account signin form """
35     next = NextUrlField()
36     username = UserNameField(required=False,skip_clean=True)
37     password = forms.CharField(max_length=128,
38             widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
39             required=False)
40
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,
44                 prefix, initial)
45         self.user_cache = None
46
47     def _clean_nonempty_field(self,field):
48         value = None
49         if field in self.cleaned_data:
50             value = self.cleaned_data[field].strip()
51             if value == '':
52                 value = None
53         self.cleaned_data[field] = value
54         return value
55
56     def clean_username(self):
57         return self._clean_nonempty_field('username')
58
59     def clean_password(self):
60         return self._clean_nonempty_field('password')
61
62     def clean(self):
63         error_list = []
64         username = self.cleaned_data['username']
65         password = self.cleaned_data['password']
66
67         self.user_cache = None
68         if username and password:
69             user_ = authenticate(username=username, password=password)
70
71             if user_ is None:
72                 del self.cleaned_data['username']
73                 del self.cleaned_data['password']
74
75                 error_list.insert(0,(_("Please enter valid username and password "
76                                     "(both are case-sensitive).")))
77
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.'))
82             try:
83                 self.user_cache = user_.user
84             except:
85                 error_list.append(_('This user is not a valid user'))
86
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)
95
96         return self.cleaned_data
97
98     def get_user(self):
99         """ get authenticated user """
100         return self.user_cache