From: Matías Aguirre Date: Thu, 9 Feb 2012 04:52:14 +0000 (-0200) Subject: Context processor improvements on code and doc. Closes #245 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=bec39d7af1a1fd9526cbdd3a64a47461aee768dc;p=django-social-auth.git Context processor improvements on code and doc. Closes #245 --- diff --git a/README.rst b/README.rst index 82a5c28..0b823cb 100644 --- a/README.rst +++ b/README.rst @@ -211,10 +211,31 @@ Configuration TEMPLATE_CONTEXT_PROCESSORS = ( ... + 'social_auth.context_processors.social_auth_by_name_backends', + 'social_auth.context_processors.social_auth_backends', 'social_auth.context_processors.social_auth_by_type_backends', ) - check `social_auth.context_processors`. + * `social_auth_by_name_backends`: + Adds a `social_auth` dict where each key is a provider name and its value + is a UserSocialAuth instance if user has associated an account with that + provider, otherwise `None`. + + * `social_auth_backends`: + Adds a `social_auth` dict with keys are `associated, `not_associated` and + `backends`. `associated` key is a list of `UserSocialAuth` instances + associated with current user. `not_associated` is a list of providers names + that the current user doesn't have any association yet. `backends` holds + the list of backend names supported. + + * `social_auth_by_type_backends`: + Simiar to `social_auth_backends` but each value is grouped by backend type + `openid`, `oauth2` and `oauth`. + + Check `social_auth.context_processors` for details. + + **Note**: `social_auth_backends` and `social_auth_by_type_backends` don't + play nice together. - Sync database to create needed models:: diff --git a/doc/configuration.rst b/doc/configuration.rst index 9a9c598..64463e6 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -108,10 +108,31 @@ Configuration TEMPLATE_CONTEXT_PROCESSORS = ( ... + 'social_auth.context_processors.social_auth_by_name_backends', + 'social_auth.context_processors.social_auth_backends', 'social_auth.context_processors.social_auth_by_type_backends', ) - check `social_auth.context_processors`. + * `social_auth_by_name_backends`: + Adds a `social_auth` dict where each key is a provider name and its value + is a UserSocialAuth instance if user has associated an account with that + provider, otherwise `None`. + + * `social_auth_backends`: + Adds a `social_auth` dict with keys are `associated, `not_associated` and + `backends`. `associated` key is a list of `UserSocialAuth` instances + associated with current user. `not_associated` is a list of providers names + that the current user doesn't have any association yet. `backends` holds + the list of backend names supported. + + * `social_auth_by_type_backends`: + Simiar to `social_auth_backends` but each value is grouped by backend type + `openid`, `oauth2` and `oauth`. + + Check `social_auth.context_processors` for details. + + **Note**: `social_auth_backends` and `social_auth_by_type_backends` don't + play nice together. - Sync database to create needed models:: diff --git a/social_auth/context_processors.py b/social_auth/context_processors.py index 47bf4c6..1355be1 100644 --- a/social_auth/context_processors.py +++ b/social_auth/context_processors.py @@ -36,10 +36,11 @@ def social_auth_by_name_backends(request): """ keys = get_backends().keys() accounts = dict(zip(keys, [None] * len(keys))) + user = request.user - if isinstance(request.user, User) and request.user.is_authenticated(): - for associated in request.user.social_auth.all(): - accounts[associated.provider.replace('-', '_')] = associated + if isinstance(user, User) and user.is_authenticated(): + accounts.update((assoc.provider.replace('-', '_'), assoc) + for assoc in user.social_auth.all()) return {'social_auth': accounts} diff --git a/social_auth/utils.py b/social_auth/utils.py index 4033867..b5de003 100644 --- a/social_auth/utils.py +++ b/social_auth/utils.py @@ -69,9 +69,10 @@ def group_backend_by_type(items, key=lambda x: x): get_backends, OpenIdAuth, BaseOAuth, BaseOAuth2 result = defaultdict(list) + backends = get_backends() for item in items: - backend = get_backends()[key(item)] + backend = backends[key(item)] if issubclass(backend, OpenIdAuth): result['openid'].append(item) elif issubclass(backend, BaseOAuth2):