text: #!/usr/bin/env python
  # encoding: utf-8
  
  
  """
  @version: ??
  @author: liangliangyy
  @license: MIT Licence 
  @contact: liangliangyy@gmail.com
  @site: https://www.lylinux.org/
  @software: PyCharm
  @file: user_login_backend.py
  @time: 2017/2/17 下午8:45
  """
  from django.conf import settings
  from django.contrib.auth import get_user_model
  from django.contrib.auth.backends import ModelBackend
  
  
  class EmailOrUsernameModelBackend(ModelBackend):
      """
      允许使用用户名或邮箱登录
      """
  
      def authenticate(self, request, username=None, password=None, **kwargs):
          if '@' in username:
              kwargs = {'email': username}
          else:
              kwargs = {'username': username}
          try:
              user = get_user_model().objects.get(**kwargs)
              if user.check_password(password):
                  return user
          except get_user_model().DoesNotExist:
              return None
  
      def get_user(self, username):
          try:
              return get_user_model().objects.get(pk=username)
          except get_user_model().DoesNotExist:
              return None
  