Commits

offline committed e280191

fixes, fixes, fixes

Comments (0)

Files changed (4)

openauth/forms.py

     Form field for username handling.
     """
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, login=True, *args, **kwargs):
         super(UsernameField, self).__init__(*args, **kwargs)
-        self.label = _(u'Login')
+        self.label = openauth.settings.USERNAME_LABEL
 
 
     def clean(self, value):
         if not openauth.settings.USERNAME_REGEX.match(value):
             raise forms.ValidationError(_(u'Login contains restricted symbols'))
 
-        if openauth.settings.UNIQUE_USERNAME and get_object_or_None(User, username=value):
+        if openauth.settings.UNIQUE_USERNAME and not login and get_object_or_None(User, username=value):
             raise forms.ValidationError(_(u'This login already registered'))
         return value
 
     def clean(self, value):
         super(PasswordField, self).clean(value)
         if len(value) < openauth.settings.PASSWORD_MIN_LENGTH:
-            raise forms.ValidationError(_(u'Password length is less than %(min)d') % {'min': PASSWORD_MIN_LENGTH})
+            raise forms.ValidationError(_(u'Password length is less than %(min)d') % {'min': openauth.settings.PASSWORD_MIN_LENGTH})
         if len(value) > openauth.settings.PASSWORD_MAX_LENGTH:
-            raise forms.ValidationError(_(u'Password length is more than %(max)d') % {'max': PASSWORD_MAX_LENGTH})
+            raise forms.ValidationError(_(u'Password length is more than %(max)d') % {'max': openauth.settings.PASSWORD_MAX_LENGTH})
         return value
 
 
 class RegistrationForm(forms.Form):
-    username = UsernameField()
+
+    username = UsernameField(login=False)
     email = forms.EmailField(label=_('Email'))
     password = PasswordField(label=_('Password'))
     password_dup = PasswordField(label=_('Password (confirmation)'))
 
     
     def clean_email(self):
-        #return self.cleaned_data.get('email','')
-        try:
-            User.objects.get(email__exact=self.cleaned_data['email'].lower())
-        except User.DoesNotExist:
-            return self.cleaned_data['email']
-        except KeyError:
-            pass
-        else:
+        if get_object_or_None(User, email=self.cleaned_data['email'].lower()):
             raise forms.ValidationError(_(u'This email already registered'))
+        return self.cleaned_data['email']
 
     
     def clean(self):
 
 
 class LoginForm(forms.Form):
-    email = forms.EmailField(label=_('Email'))
-    password = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
+    username = UsernameField()
+    password = PasswordField()
 
-    def __init__(self, *args, **kwargs):
-        self.request = kwargs.pop('request')
-        self.base_fields['email'].help_text = ''
-        #self.base_fields['password'].widget = forms.PasswordInput()
-        self.base_fields['password'].help_text = ''
+    def __init__(self, request, *args, **kwargs):
+        self.request = request
         super(LoginForm, self).__init__(*args, **kwargs)
 
-
     def clean(self):
         super(LoginForm, self).clean()
         if self.is_valid():
-            email = self.cleaned_data['email'].lower()
+            username = self.cleaned_data['username'].lower()
             password = self.cleaned_data['password']
-            user = get_object_or_None(User, email=email)
+            if 'username' in openauth.settings.AUTHENTICATION_TYPE:
+                user = get_object_or_None(User, username=username)
+            elif 'email' in openauth.settings.AUTHENTICATION_TYPE:
+                user = get_object_or_None(User, email=username)
             if user and  user.check_password(password):
-                if user.is_active:
-                    user.backend = 'django.contrib.auth.backends.ModelBackend'
-                    login(self.request, user)
-                    return self.cleaned_data
-                else:
+                if openauth.settings.ACTIVATION_REQUIRED and not user.is_active:
                     raise forms.ValidationError(_(u'Your account is not active. Please activate it.'))
+                user.backend = 'django.contrib.auth.backends.ModelBackend'
+                login(self.request, user)
+                return self.cleaned_data
             else:
-                raise forms.ValidationError(_(u'Incorrect email or password'))
+                raise forms.ValidationError(_(u'Incorrect authentication data'))
 
 LoginForm = autostrip(LoginForm)
 

openauth/settings.py

 PASSWORD_RESET_CONFIRMATION_TYPE = get_config("PASSWORD_RESET_CONFIRMATION_TYPE", 2)
 
 LOGIN_REDIRECT_URL = get_config("LOGIN_REDIRECT_URL", "/auth/login/")
+AUTHENTICATION_TYPE = get_config("AUTHENTICATION_TYPE", ["username"])
 
 USERNAME_REGEX = get_config("USERNAME_REGEX", re.compile(r"[a-z0-9][_a-z0-9]*[a-z0-9]$", re.I))
 USERNAME_MIN_LENGTH = get_config("ACCOUNT_USERNAME_MIN_LENGTH", 3)
 USERNAME_MAX_LENGTH = get_config("ACCOUNT_USERNAME_MAX_LENGTH", 30)
+USERNAME_LABEL = get_config("USERNAME_LABEL", "Username")
 
 PASSWORD_MIN_LENGTH = get_config("ACCOUNT_PASSWORD_MIN_LENGTH", 1)
 PASSWORD_MAX_LENGTH = get_config("ACCOUNT_PASSWORD_MAX_LENGTH", 30)

openauth/templates/openauth/login.html

+{% extends 'base.html' %}
+{% block content %}
+<form method='post' action='.'>
+    {{ form.as_table }}
+    <input type='submit' value='submit' />
+</form>
+{% endblock content %}

openauth/views.py

         return message(request, _('You are already authenticated'), 'notice', '/')
 
     if request.POST:
-        form = LoginForm(request.POST, request=request)
+        form = LoginForm(request, request.POST)
         request.session['login_redirect_url'] = request.GET.get('next')
         if form.is_valid():
             redirect_url = build_redirect_url(request, openauth.settings.LOGIN_REDIRECT_URL)