From e7d59178152276165d8c1b4d0a63681445998487 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Sun, 14 Aug 2011 06:02:37 -0300 Subject: [PATCH] SocialAuth context processors. Closes gh-113 --- social_auth/context_processors.py | 52 +++++++++++++++++++++++++++++++ social_auth/utils.py | 18 +++++++++++ 2 files changed, 70 insertions(+) create mode 100644 social_auth/context_processors.py diff --git a/social_auth/context_processors.py b/social_auth/context_processors.py new file mode 100644 index 0000000..d0b8d66 --- /dev/null +++ b/social_auth/context_processors.py @@ -0,0 +1,52 @@ +from social_auth.backends import BACKENDS +from social_auth.utils import group_backend_by_type + + +# Note: social_auth_backends and social_auth_by_type_backends don't play nice +# together + +def social_auth_backends(request): + """Load Social Auth current user data to context. + Will add a output from backends_data to context under social_auth key. + """ + return {'social_auth': backends_data(request.user)} + + +def social_auth_by_type_backends(request): + """Load Social Auth current user data to context. + Will add a output from backends_data to context under social_auth key where + each entry will be grouped by backend type (openid, oauth, oauth2). + """ + data = backends_data(request.user) + data['backends'] = group_backend_by_type(data['backends']) + data['not_associated'] = group_backend_by_type(data['not_associated']) + data['associated'] = group_backend_by_type(data['associated'], + key=lambda assoc: assoc.provider) + print data + return {'social_auth': data} + + +def backends_data(user): + """Return backends data for given user. + + Will return a dict with values: + associated: UserSocialAuth model instances for currently + associated accounts + not_associated: Not associated (yet) backend names. + backends: All backend names. + + If user is not authenticated, then first list is empty, and there's no + difference between the second and third lists. + """ + available = BACKENDS.keys() + values = {'associated': [], + 'not_associated': available, + 'backends': available} + + if user.is_authenticated(): + associated = user.social_auth.all() + not_associated = list(set(available) - + set(assoc.provider for assoc in associated)) + values['associated'] = associated + values['not_associated'] = not_associated + return values diff --git a/social_auth/utils.py b/social_auth/utils.py index 146d450..9d54848 100644 --- a/social_auth/utils.py +++ b/social_auth/utils.py @@ -1,4 +1,7 @@ import urlparse +from collections import defaultdict + +from social_auth.backends import BACKENDS, OpenIdAuth, BaseOAuth, BaseOAuth2 def sanitize_redirect(host, redirect_to): @@ -38,6 +41,21 @@ def sanitize_redirect(host, redirect_to): return redirect_to +def group_backend_by_type(items, key=lambda x: x): + """Group items by backend type.""" + result = defaultdict(list) + + for item in items: + backend = BACKENDS[key(item)] + if issubclass(backend, OpenIdAuth): + result['openid'].append(item) + elif issubclass(backend, BaseOAuth2): + result['oauth2'].append(item) + elif issubclass(backend, BaseOAuth): + result['oauth'].append(item) + return dict(result) + + if __name__ == '__main__': import doctest doctest.testmod() -- 2.39.5