From 9636d4a6767f49384d5c386bc3f1142c88b90613 Mon Sep 17 00:00:00 2001 From: Alvin Li Date: Tue, 13 Aug 2013 14:13:24 +0800 Subject: cloned from 'bitbucket', 2013/08/13 --- 97suifangqa/apps/profile/backends.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 97suifangqa/apps/profile/backends.py (limited to '97suifangqa/apps/profile/backends.py') diff --git a/97suifangqa/apps/profile/backends.py b/97suifangqa/apps/profile/backends.py new file mode 100644 index 0000000..8903805 --- /dev/null +++ b/97suifangqa/apps/profile/backends.py @@ -0,0 +1,25 @@ +#-*- coding: utf-8 -*- + +from django.contrib.auth.models import User + +class EmailOrUsernameModelBackend(object): + + def authenticate(self, username=None, password=None): + if '@' in username: + kwargs = {'email': username} + else: + kwargs = {'username': username} + try: + user = User.objects.get(**kwargs) + if user.check_password(password): + return user + except User.DoesNotExist: + pass + return None + + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except User.DoesNotExist: + pass + return None \ No newline at end of file -- cgit v1.2.2