开发者

Custom user model breaks auth tests

Hecko out there,

I’m using a custom user model that I’ve opted to call Member, which extends the default user model (pretty much as described here).

The rub is that this breaks several tests of the auth app, because the test fixture only creates standard users, not members. When I add the missing member definitions/rows to django/contrib/auth/fixtures/authtestdata.json they run again, but that’s of course no solution.

Is this is a resul开发者_StackOverflow中文版t of me doing something wrong, and how should I best go about fixing it?

As a last recourse, I would just add a patch for authtestdata.json in Buildout, but perhaps there is a more elegant solution.

Thanks a lot,

Telofy


You may get some joy from setting the AUTH_USER_PROFILE parameter in settings.py, as per http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users. This is a great way of extending the standard User model in a way that Django can work with.


Update: Please disregard my post below. Use the original version of the custom auth backend and add django.contrib.auth.backends.ModelBackend as a second (fallback) backend under the custom one in AUTHENTICATION_BACKENDS in your settings.py. It has the same effect and is much simpler.


I now got rid of the patch and instead extended the custom auth backend that I linked to in my question to fall back on the methods of the original auth backend if the user can’t be found in the member table (my custom user). If this happens, a warning is issued (which can be a bit annoying when running the tests). Also, the messages tests cause a lot of exceptions, but they don’t seem to have any adverse effect on the tests. I hope they are intentional. ^^

# -*- encoding: utf-8 -*-
import logging
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model

logger = logging.getLogger(__name__)

# http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
class CustomUserModelBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = self.user_class.objects.get(username=username)
            if user.check_password(password):
                return user
        except self.user_class.DoesNotExist:
            user = super(CustomUserModelBackend, self).authenticate(username, password)
            if user:
                logger.warn("Using User, not Member: %s" % user)
            return user

    def get_user(self, user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except self.user_class.DoesNotExist:
            user = super(CustomUserModelBackend, self).get_user(user_id)
            if user:
                logger.warn("Using User, not Member: %s" % user)
            return user

    @property
    def user_class(self):
        if not hasattr(self, '_user_class'):
            self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
            if not self._user_class:
                raise ImproperlyConfigured('Could not get custom user model')
        return self._user_class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜