From 5a46e0a9b683af363bf0d77f56dba456d6770277 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Mon, 29 Aug 2011 15:58:43 -0300 Subject: [PATCH] Use messages framework if available. Refs gh-136 --- example/app/views.py | 6 +++--- example/templates/error.html | 7 +++++++ social_auth/views.py | 20 +++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/example/app/views.py b/example/app/views.py index c104662..002e86c 100644 --- a/example/app/views.py +++ b/example/app/views.py @@ -1,9 +1,9 @@ -from django.conf import settings from django.http import HttpResponseRedirect from django.contrib.auth import logout as auth_logout from django.contrib.auth.decorators import login_required from django.template import RequestContext from django.shortcuts import render_to_response +from django.contrib.messages.api import get_messages from social_auth import __version__ as version @@ -25,9 +25,9 @@ def done(request): def error(request): """Error view""" - error_msg = request.session.pop(settings.SOCIAL_AUTH_ERROR_KEY, None) + messages = get_messages(request) return render_to_response('error.html', {'version': version, - 'error_msg': error_msg}, + 'messages': messages}, RequestContext(request)) def logout(request): diff --git a/example/templates/error.html b/example/templates/error.html index e90ef46..ac4181d 100644 --- a/example/templates/error.html +++ b/example/templates/error.html @@ -5,10 +5,17 @@ {% block content %}

Sorry but some error made you impossible to login.

+ + {% if messages %} + {% for msg in messages %} +

{{ msg.message }} ({{ msg.extra_tags }})

+ {% endfor %} + {% endif %} {% if error_msg %}

Details:

{{ error_msg }}

{% endif %} +

Please try again Home

{% endblock %} diff --git a/social_auth/views.py b/social_auth/views.py index 6a7243b..cb32e68 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -36,7 +36,8 @@ ASSOCIATE_URL_NAME = _setting('SOCIAL_AUTH_ASSOCIATE_URL_NAME', SOCIAL_AUTH_LAST_LOGIN = _setting('SOCIAL_AUTH_LAST_LOGIN', 'social_auth_last_login_backend') SESSION_EXPIRATION = _setting('SOCIAL_AUTH_SESSION_EXPIRATION', True) -BACKEND_ERROR_REDIRECT = _setting('SOCIAL_AUTH_BACKEND_ERROR_URL') +BACKEND_ERROR_REDIRECT = _setting('SOCIAL_AUTH_BACKEND_ERROR_URL', + LOGIN_ERROR_URL) ERROR_KEY = _setting('SOCIAL_AUTH_BACKEND_ERROR', 'socialauth_backend_error') NAME_KEY = _setting('SOCIAL_AUTH_BACKEND_KEY', 'socialauth_backend_name') @@ -63,12 +64,17 @@ def dsa_view(redirect_name=None): try: return func(request, backend, *args, **kwargs) except Exception, e: # some error ocurred - if ERROR_KEY: - # store error in session - request.session[ERROR_KEY] = str(e) - if NAME_KEY: - # store the backend name in the session for convenience - request.session[NAME_KEY] = backend.AUTH_BACKEND.name + backend_name = backend.AUTH_BACKEND.name + msg = str(e) + + if 'django.contrib.messages' in settings.INSTALLED_APPS: + from django.contrib.messages.api import error + error(request, msg, extra_tags=backend_name) + else: + if ERROR_KEY: # store error in session + request.session[ERROR_KEY] = msg + if NAME_KEY: # store the backend name for convenience + request.session[NAME_KEY] = backend_name return HttpResponseRedirect(BACKEND_ERROR_REDIRECT) return wrapper return dec -- 2.39.5