Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Forgot to add one file to the last commit.

  • Loading branch information...
commit 39e232eac1c13c9c84704ae187f1fa2c0b61730f 1 parent eb5d19b
@zuzelvp authored
Showing with 72 additions and 0 deletions.
  1. +72 −0 forum_modules/p2puauth/forms.py
View
72 forum_modules/p2puauth/forms.py
@@ -0,0 +1,72 @@
+from forum.forms import NextUrlField, UserNameField
+from django.utils.translation import ugettext as _
+from django.contrib.auth import authenticate
+from django import forms
+
+
+class ClassicLoginForm(forms.Form):
+ """ legacy account signin form """
+ next = NextUrlField()
+ username = UserNameField(required=False, skip_clean=True)
+ password = forms.CharField(max_length=128,
+ widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
+ required=False)
+
+ def __init__(self, data=None, files=None, auto_id='id_%s',
+ prefix=None, initial=None):
+ super(ClassicLoginForm, self).__init__(data, files, auto_id,
+ prefix, initial)
+ self.user_cache = None
+
+ def _clean_nonempty_field(self, field):
+ value = None
+ if field in self.cleaned_data:
+ value = self.cleaned_data[field].strip()
+ if value == '':
+ value = None
+ self.cleaned_data[field] = value
+ return value
+
+ def clean_username(self):
+ return self._clean_nonempty_field('username')
+
+ def clean_password(self):
+ return self._clean_nonempty_field('password')
+
+ def clean(self):
+ error_list = []
+ username = self.cleaned_data['username']
+ password = self.cleaned_data['password']
+
+ self.user_cache = None
+ if username and password:
+ user_ = authenticate(username=username, password=password)
+
+ if user_ is None:
+ del self.cleaned_data['username']
+ del self.cleaned_data['password']
+
+ error_list.insert(0, (_("Please enter valid username and password "
+ "(both are case-sensitive).")))
+
+ if len(error_list) > 0:
+ error_list.insert(0, _('Login failed.'))
+ try:
+ self.user_cache = user_.user
+ except:
+ error_list.append(_('This user is not a valid user'))
+
+ elif password == None and username == None:
+ error_list.append(_('Please enter username and password'))
+ elif password == None:
+ error_list.append(_('Please enter your password'))
+ elif username == None:
+ error_list.append(_('Please enter user name'))
+ if len(error_list) > 0:
+ self._errors['__all__'] = forms.util.ErrorList(error_list)
+
+ return self.cleaned_data
+
+ def get_user(self):
+ """ get authenticated user """
+ return self.user_cache
Please sign in to comment.
Something went wrong with that request. Please try again.