From 86a487fe2137826a19a32b883bb89ffcb9d3b2d6 Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Wed, 2 Mar 2016 14:33:27 -0500 Subject: [PATCH 1/2] Import allauth.socialaccount only when declared in INSTALLED_APPS Don't silently ignore ImportError --- rest_auth/registration/serializers.py | 6 +----- rest_auth/social_serializers.py | 8 +++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 2390b05..33d6698 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -15,12 +15,8 @@ from rest_framework import serializers from requests.exceptions import HTTPError # Import is needed only if we are using social login, in which # case the allauth.socialaccount will be declared - if 'allauth.socialaccount' in settings.INSTALLED_APPS: - try: - from allauth.socialaccount.helpers import complete_social_login - except ImportError: - pass + from allauth.socialaccount.helpers import complete_social_login class SocialLoginSerializer(serializers.Serializer): diff --git a/rest_auth/social_serializers.py b/rest_auth/social_serializers.py index 087e6e1..5bd2f0f 100644 --- a/rest_auth/social_serializers.py +++ b/rest_auth/social_serializers.py @@ -1,14 +1,12 @@ +from django.conf import settings from django.http import HttpRequest from rest_framework import serializers from requests.exceptions import HTTPError # Import is needed only if we are using social login, in which # case the allauth.socialaccount will be declared -try: +if 'allauth.socialaccount' in settings.INSTALLED_APPS: from allauth.socialaccount.helpers import complete_social_login -except ImportError: - pass - -from allauth.socialaccount.models import SocialToken + from allauth.socialaccount.models import SocialToken class TwitterLoginSerializer(serializers.Serializer): From 0737da0077ad063eb56a9b832a32ece2af7ea32b Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Wed, 2 Mar 2016 14:59:01 -0500 Subject: [PATCH 2/2] Capture OAuthError in TwitterLoginSerializer --- rest_auth/social_serializers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_auth/social_serializers.py b/rest_auth/social_serializers.py index 5bd2f0f..de6223f 100644 --- a/rest_auth/social_serializers.py +++ b/rest_auth/social_serializers.py @@ -1,12 +1,12 @@ from django.conf import settings from django.http import HttpRequest from rest_framework import serializers -from requests.exceptions import HTTPError # Import is needed only if we are using social login, in which # case the allauth.socialaccount will be declared if 'allauth.socialaccount' in settings.INSTALLED_APPS: from allauth.socialaccount.helpers import complete_social_login from allauth.socialaccount.models import SocialToken + from allauth.socialaccount.providers.oauth.client import OAuthError class TwitterLoginSerializer(serializers.Serializer): @@ -65,8 +65,8 @@ class TwitterLoginSerializer(serializers.Serializer): try: login = self.get_social_login(adapter, app, token, access_token) complete_social_login(request, login) - except HTTPError: - raise serializers.ValidationError('Incorrect value') + except OAuthError as e: + raise serializers.ValidationError(str(e)) if not login.is_existing: login.lookup()