]> git.parisson.com Git - django-social-auth.git/commitdiff
Add context processor to access social auth information by backend name, useful to...
authorDaniel G. Taylor <dan@programmer-art.org>
Mon, 10 Oct 2011 13:41:57 +0000 (09:41 -0400)
committerDaniel G. Taylor <dan@programmer-art.org>
Mon, 10 Oct 2011 13:41:57 +0000 (09:41 -0400)
social_auth/context_processors.py

index 96f7c19bfbd4b2b1242dc95a31b55bc0a8862704..db9b051a584951740a9a00daf74b8f8435f24b96 100644 (file)
@@ -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.