Django AbstractUser error when login in -
i have created abstractuser model add fields standard usermodel.
i think set correctly, when log in, error:
unbound method save() must called userprofile instance first argument (got nothing instead)
here accounts/models.py
from django.db import models django.conf import settings django.contrib.auth.models import usermanager, abstractuser class userprofile(abstractuser): mobile = models.charfield(max_length=20, blank=true) homephone = models.charfield(max_length=20, blank=true) updated = models.datetimefield(blank=true, null=true) objects = usermanager()
in settings have:
auth_user_model = 'accounts.userprofile'
my authentication middleware:
from django.conf import settings django.contrib.auth import authenticate, login django.contrib.auth.models import user accounts.models import userprofile #authentication middleware using external cookie named authentication class cookiemiddleware(object): def process_request(self, request): #if not hasattr(request, 'userprofile'): # raise improperlyconfigured() if "authentication" not in request.cookies: #cookie not found - nothing return #token found - first check if user allready logged in if request.user.is_authenticated(): return #not logged in, send remoteuserbackend.py token = request.cookies["authentication"] #return if cookie length 0 if len(token) == 0: return userprofile = authenticate(token=token) request.userprofile = userprofile if request.userprofile: login(request, request.userprofile)
my remoteuserbackend.py login in external users:
from django.conf import settings django.contrib.auth import authenticate, login django.contrib.auth.models import user base64 import b64decode hashlib import sha1 urllib import unquote sitetasks import tasks accounts.models import userprofile class backend( object ): def authenticate(self, username=none, password=none, token=none): #unescape token unescaped_token = unquote(token) #decode token decoded_token = unescaped_token.decode('base64') #split token tree variable secret, hashstring, userid = decoded_token.split('-', 2) #secret needs bee in lower match shared secret secret_lower = secret.lower() #make string of shared_secret, hashstring, userid check_string = "%s%s%s" % (settings.shared_secret, hashstring, userid) #sha1 string sha1_check_string = sha1(check_string) #check if shared_secret matching cookie secret cookie_valid = sha1_check_string.hexdigest() == secret_lower if cookie_valid: try: userprofile = userprofile.objects.get(username=userid) #the user exist, update user #make celery worker update user asynchronous tasks.user_update.delay(user_id=userid) except userprofile.doesnotexist: # create new user userprofile = userprofile(username=userid) userprofile.is_staff = false userprofile.is_superuser = false userprofile.save() #save user #make celery worker update user asynchronous tasks.user_update.delay(user_id=userid) return userprofile return none def get_user(self, user_id): try: return userprofile.objects.get(pk=user_id) except userprofile.doesnotexist: return none
the traceback error this:
request method: request url: http://mydomain.com/ django version: 1.6.dev20130302084542 python version: 2.7.3 installed applications: ('django.contrib.admin', 'django.contrib.admindocs', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django.contrib.flatpages', 'south', 'djcelery', 'gunicorn', 'sorl.thumbnail', 'template_utils', 'compressor', 'tagging', 'ckeditor', 'debug_toolbar', 'mptt', 'accounts', ) installed middleware: ('django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.locale.localemiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.contrib.flatpages.middleware.flatpagefallbackmiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 'myproj.cookiemiddleware.cookiemiddleware') traceback: file "/home/user/.virtualenvs/site/downloads/django-trunk/django/core/handlers/base.py" in get_response 82. response = middleware_method(request) file "/home/user/.virtualenvs/site/myproj/myproj/cookiemiddleware.py" in process_request 35. login(request, request.userprofile) file "/home/user/.virtualenvs/site/downloads/django-trunk/django/contrib/auth/__init__.py" in login 86. user_logged_in.send(sender=user.__class__, request=request, user=user) file "/home/user/.virtualenvs/site/downloads/django-trunk/django/dispatch/dispatcher.py" in send 182. response = receiver(signal=self, sender=sender, **named) file "/home/user/.virtualenvs/site/downloads/django-trunk/django/contrib/auth/models.py" in update_last_login 31. user.save(update_fields=['last_login']) exception type: typeerror @ / exception value: unbound method save() must called userprofile instance first argument (got nothing instead)
anyone see im doing wrong here?
your backend authenticate()
method returns userprofile
(the class) instead of userprofile
(the instance of class belonging user).
although there's nothing wrong in middleware, clearer if kept same naming convention - normal python 1 - , referred request.userprofile
instead of request.userprofile
.
Comments
Post a Comment