1. iiamSemaj
  2. django-publicauth


django-publicauth / openauth / backends / email.py

from __future__ import absolute_import

from django import forms
from openauth.backends import BaseBackend
from django.utils.translation import ugettext as _

from annoying.exceptions import Redirect

from openauth import settings
from openauth.fields import PasswordField

class EmailBackend(BaseBackend):

    email = forms.EmailField()
    password = PasswordField()

    def clean(self):
        Validate cridentials and login the user in case of success.
        If user is not activated and ACTIVATION_REQUIRED set to True
        form will not validate.
        super(EmailBackend, self).clean()
        if self.is_valid():
            email = self.cleaned_data['email'].lower()
            password = self.cleaned_data['password']
            user = get_object_or_None(User, email=email)
            if user and user.check_password(password):
                if 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_user(self.request, user)
                raise Redirect(settings.LOGIN_REDIRECT_URL)
                raise forms.ValidationError(_(u'Incorrect authentication data'))