From d4099b0459d185293019228c68ce420e6c1e0e5a Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Mon, 10 Oct 2011 09:41:57 -0400 Subject: [PATCH] Add context processor to access social auth information by backend name, useful to easily provide backend-specific functionality in your app by checking e.g. social_auth.facebook and allowing the user to publish to Facebook if it exists. --- social_auth/context_processors.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/social_auth/context_processors.py b/social_auth/context_processors.py index 96f7c19..db9b051 100644 --- a/social_auth/context_processors.py +++ b/social_auth/context_processors.py @@ -26,6 +26,24 @@ def social_auth_by_type_backends(request): return {'social_auth': data} +def social_auth_by_name_backends(request): + """Load Social Auth current user data to context. + Will add a social_auth object whose attribute names are the names of each + provider, e.g. social_auth.facebook would be the facebook association or + None, depending on the logged in user's current associations. Providers + with a hyphen have the hyphen replaced with an underscore, e.g. + google-oauth2 becomes google_oauth2 when referenced in templates. + """ + keys = BACKENDS.keys() + accounts = dict(zip(keys, [None] * len(keys))) + + if isinstance(request.user, User) and request.user.is_authenticated(): + for associated in request.user.social_auth.all(): + accounts[associated.provider.replace('-', '_')] = associated + + return {'social_auth': accounts} + + def backends_data(user): """Return backends data for given user. -- 2.39.5