]> git.parisson.com Git - django-social-auth.git/commitdiff
Context processor improvements on code and doc. Closes #245
authorMatías Aguirre <matiasaguirre@gmail.com>
Thu, 9 Feb 2012 04:52:14 +0000 (02:52 -0200)
committerMatías Aguirre <matiasaguirre@gmail.com>
Thu, 9 Feb 2012 04:52:14 +0000 (02:52 -0200)
README.rst
doc/configuration.rst
social_auth/context_processors.py
social_auth/utils.py

index 82a5c28824d3e91500ee1ef54b9296fd5a102761..0b823cbdbe3bed204a8411b1de87f0b3d71c9191 100644 (file)
@@ -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::
 
index 9a9c59836f70470bf1f87afd20706ac7d1fafa9f..64463e6f14e72ff3fa978a500f3c31d06282d2cc 100644 (file)
@@ -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::
 
index 47bf4c65e7dc70e73b4f4ca96df0163ae5d27246..1355be1d202214f2a8cf7cfde3fd7bcb7f84791e 100644 (file)
@@ -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}
 
index 403386720e8ed801aabb36cce689455b64e262dc..b5de003079cc8451d1ead326fae838add887e1ae 100644 (file)
@@ -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):