示例#1
0
    try:
        UserCls = import_string(model_path)
    except ImportError:
        raise ImproperlyConfigured('Error importing user model class: "%s"' % model_path)
    except AttributeError:
        raise ImproperlyConfigured('Module does not define a class "%s"' % model_path)

    try:
        AnonUserCls = import_string(anon_model_path)
    except ImportError:
        raise ImproperlyConfigured('Error importing Anonymous user model class: "%s"' % anon_model_path)
    except AttributeError:
        raise ImproperlyConfigured('Module does not define a class "%s"' % anon_model_path)
    return UserCls, AnonUserCls
_get_user_models = memoize(_get_user_models, _user_model_cache, 1)

def get_user_model(model_name=None):
    """
    Used to get access to defined user models.

    from newauth.models import User, get_user_model

    OtherUser = get_user_model('other')

    class MyProfile(models.Model):
        user = models.ForeignKey(User)
        other_user_type = models.ForeignKey(OtherUser)
    """
    return _get_user_models(model_name)[0]
示例#2
0
    """
    backend_data = _get_backend_data().get(backend_name)
    if not backend_data:
        raise ImproperlyConfigured('The specified backend "%s" does not exist. Is NEWAUTH_BACKENDS a correctly defined dict?' % backend_name)

    backends = []
    for path in backend_data['backend']:
        try:
            cls = import_string(path)
        except (ImportError, AttributeError), e:
            raise ImproperlyConfigured('Error importing authentication backend %s: "%s"' % (path, e))
        except ValueError, e:
            raise ImproperlyConfigured('Error importing authentication backends. Is NEWAUTH_BACKENDS a correctly defined dict?')
        backends.append(cls(backend_name))
    return backends
load_backends = memoize(load_backends, _auth_backend_cache, 1)

def get_backends(backend_names=None):
    """
    Load all auth backends and return them as a (name, backend) two tuple.
    """
    all_backend_names = _get_backend_data().keys()
    if backend_names is None:
        backend_names = all_backend_names
    else:
        backend_names = filter(lambda b: b in backend_names, all_backend_names)

    backends = []
    for backend_name in backend_names:
        backends.append((backend_name, load_backends(backend_name)))
    return backends