2016-10-28 71 views
0

這個問題很奇怪。這裏有一個堆棧跟蹤:Django:unbound方法is_authenticated()必須用AbstractBaseUser實例調用

Environment: 


Request Method: GET 
Request URL: http://localhost/en/dashboard 

Django Version: 1.9.6 
Python Version: 2.7.12 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.admin', 
'django_mongoengine', 
'django_mongoengine.mongo_auth', 
'django_mongoengine.mongo_admin.sites', 
'django_mongoengine.mongo_admin', 
'django_adyen', 
'spiral_api', 
'rest_framework', 
'rest_framework_mongoengine', 
'spiral_admin', 
'rest_framework_docs') 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.locale.LocaleMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'spiral_api.middlewares.BehalfUserMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'spiral_api.middlewares.AuthHeaderMiddleware') 



Traceback: 

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    235.     response = middleware_method(request, response) 

File "/code/django_app/spiral_api/middlewares.py" in process_response 
    11.  def process_response(self, request, response): 

File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py" in inner 
    204.    self._setup() 

File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py" in _setup 
    351.   self._wrapped = self._setupfunc() 

File "/code/django_app/spiral_api/middlewares.py" in get_user 
    18.   user = get_user_middleware(request) 

Exception Type: TypeError at /en/dashboard 
Exception Value: unbound method is_authenticated() must be called with AbstractBaseUser instance as first argument (got nothing instead) 

這裏有一箇中間件:

from functools import partial 

from django.contrib.auth.middleware import get_user as get_user_middleware 
from django.contrib.auth.models import AbstractBaseUser 
from django.utils.functional import SimpleLazyObject 

from spiral_api.models import SpiralUserProfile 


class AuthHeaderMiddleware(object): 
    def process_response(self, request, response): 
     response['is_login'] = int(request.user.is_active) if hasattr(request, 'user') else 0 
     return response 


class BehalfUserMiddleware(object): 
    def get_user(self, request): 
     user = get_user_middleware(request) 
     if user.is_authenticated(): 
      profile = SpiralUserProfile.objects.get(user=user) 
      return profile.behalf or user 
     else: 
      return user 

    def process_request(self, request): 
     assert hasattr(request, 'session'), (
      "The Django authentication middleware requires session middleware " 
      "to be installed. Edit your MIDDLEWARE_CLASSES setting to insert " 
      "'django.contrib.sessions.middleware.SessionMiddleware' before " 
      "'django.contrib.auth.middleware.AuthenticationMiddleware'." 
     ) 
     request.user = SimpleLazyObject(partial(self.get_user, request)) 
     request.behalf = SimpleLazyObject(lambda: get_user_middleware(request)) 

可悲的是項目是不是我的。從異常你看它指向get_user_middleware()函數。我試圖在那裏添加print,也作爲sys.exit(),沒有任何作品。它似乎是它甚至沒有達到這個功能。這裏的中間件設置的外觀在設置:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    # 'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'spiral_api.middlewares.BehalfUserMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'spiral_api.middlewares.AuthHeaderMiddleware' 
) 

而且SpiralUserProfile有一個字段behalf這是參考User模型的模型。我完全不明白爲什麼異常在翻譯甚至沒來的地方顯示錯誤?如何解決這個問題?

版本requirements.txt的:

nose==1.3.7 
pinocchio==0.4.2 
django_nose==1.4.4 
bjoern==1.4.3 
amqp==1.4.9 
anyjson==0.3.3 
apiclient==1.0.3 
billiard==3.3.0.23 
blinker==1.4 
build==1.0.2 
celery==3.1.23 
Django==1.9.6 
django-bootstrap3==7.0.1 
django-crispy-forms==1.6.0 
django-fanout==1.1.1 
django-filter==0.13.0 
git+https://github.com/MongoEngine/django-mongoengine 
django-redis==4.4.3 
django-rest-framework-mongoengine==3.3.0 
djangorestframework==3.3.3 
drfdocs==0.0.9 
EasyProcess==0.2.2 
fanout==1.2.0 
git+http://github.com/google/google-api-python-client/ 
gunicorn==19.3.0 
httplib2==0.9.2 
kombu==3.0.35 
mongodbforms==0.3 
mongoengine==0.10.6 
nltk==3.2.1 
oauth2==1.9.0.post1 
oauth2client==2.1.0 
oauthlib==1.1.2 
Pillow==3.2.0 
psycopg2==2.6 
pubcontrol==2.2.7 
pyasn1==0.1.9 
pyasn1-modules==0.0.8 
PyInvoice==0.1.7 
PyJWT==1.4.0 
pymongo==3.2.2 
python-openid==2.2.5 
pytz==2016.4 
PyVirtualDisplay==0.2 
qrcode==5.3 
qrplatba==0.3.4 
redis==2.10.5 
reportlab==3.3.0 
requests==2.10.0 
requests-oauthlib==0.6.1 
rsa==3.4.2 
selenium==2.53.2 
simplejson==3.8.2 
six==1.10.0 
uritemplate==0.6 
urllib3==1.15.1 
xvfbwrapper==0.2.8 
zope.dottedname==4.1.0 
pyPdf 
+0

這是完整的堆棧跟蹤? – e4c5

回答

1

我不知道這個曾經是如何工作的,但我可以明白你爲什麼會得到這個錯誤。在django-mongoengine/mongo_auth/models.py我看到這個:

class BaseUser(object): 

    is_anonymous = AbstractBaseUser.is_anonymous 
    is_authenticated = AbstractBaseUser.is_authenticated 

class User(BaseUser, document.Document): 
    ... 

這似乎是錯誤的來源。

你可以把它通過讓用戶直接類實現這些方法修改庫工作:

class User(document.Document): 
    ... 
    def is_anonymous(self): 
     """ 
     Always returns False. This is a way of comparing User objects to 
     anonymous users. 
     """ 
     return False 

    def is_authenticated(self): 
     """ 
     Always return True. This is a way to tell if the user has been 
     authenticated in templates. 
     """ 
     return True 
+1

似乎django-mongoengine軟件包剛剛接受了以不同方式解決問題的PR:https://github.com/MongoEngine/django-mongoengine/pull/77/files#diff-5a7b5fa78e5ae80551cc405fafe61e5bR45 –

相關問題