From: Daniel G. Taylor Date: Mon, 10 Oct 2011 13:41:57 +0000 (-0400) Subject: Add context processor to access social auth information by backend name, useful to... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=d4099b0459d185293019228c68ce420e6c1e0e5a;p=django-social-auth.git 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. --- 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.