From 595e76bee2916ce8efcf58b84d53a5aae9a95021 Mon Sep 17 00:00:00 2001 From: Andrii Kostenko Date: Thu, 16 Feb 2012 22:21:07 +0400 Subject: [PATCH] User-friendly errors, russian translation --- setup.py | 1 + social_auth/backends/__init__.py | 13 ++++--- social_auth/locale/ru/LC_MESSAGES/django.mo | Bin 0 -> 877 bytes social_auth/locale/ru/LC_MESSAGES/django.po | 37 ++++++++++++++++++++ social_auth/utils.py | 8 +++++ social_auth/views.py | 15 ++++++-- 6 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 social_auth/locale/ru/LC_MESSAGES/django.mo create mode 100644 social_auth/locale/ru/LC_MESSAGES/django.po diff --git a/setup.py b/setup.py index 171c7ae..83a0de3 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ setup(name='django-social-auth', 'social_auth.backends', 'social_auth.backends.contrib', 'social_auth.backends.pipeline'], + package_data={'social_auth':['locale/*/LC_MESSAGES/*']}, long_description=long_description(), install_requires=['oauth2>=1.5.167', 'python_openid>=2.2'], diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 30e84ef..0b06d37 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -12,6 +12,7 @@ enabled. from urllib2 import Request, urlopen from urllib import urlencode from urlparse import urlsplit +from django.utils.translation import ugettext from openid.consumer.consumer import Consumer, SUCCESS, CANCEL, FAILURE from openid.consumer.discover import DiscoveryFailure @@ -26,7 +27,7 @@ from django.contrib.auth.backends import ModelBackend from django.utils import simplejson from django.utils.importlib import import_module -from social_auth.utils import setting, log, model_to_ctype, ctype_to_model +from social_auth.utils import setting, log, model_to_ctype, ctype_to_model, DSAException from social_auth.store import DjangoOpenIDStore from social_auth.backends.exceptions import StopPipeline @@ -629,8 +630,10 @@ class BaseOAuth2(BaseOAuth): def auth_complete(self, *args, **kwargs): """Completes loging process, must return user instance""" if self.data.get('error'): - error = self.data.get('error_description') or self.data['error'] - raise ValueError('OAuth2 authentication failed: %s' % error) + if self.data['error'] == 'access_denied': + raise DSAException(ugettext(u'Authentication process was cancelled')) + error = self.data.get('error_description') or self.data.get('error') + raise DSAException(ugettext(u'Authentication failed: %s') % error) client_id, client_secret = self.get_key_and_secret() params = {'grant_type': 'authorization_code', # request auth code @@ -648,8 +651,10 @@ class BaseOAuth2(BaseOAuth): raise ValueError('Unknown OAuth2 response type') if response.get('error'): + if response.get('error') == 'access_denied': + raise DSAException(ugettext(u'Authentication process was cancelled')) error = response.get('error_description') or response.get('error') - raise ValueError('OAuth2 authentication failed: %s' % error) + raise DSAException(ugettext(u'Authentication failed: %s') % error) else: response.update(self.user_data(response['access_token']) or {}) kwargs.update({ diff --git a/social_auth/locale/ru/LC_MESSAGES/django.mo b/social_auth/locale/ru/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..9d0cc9cd89afc38d2c0b0f16294668bd6b62097f GIT binary patch literal 877 zcma)4!EVz)5M7{La^!}@VUTD=Rjsi@g(^2LHH}*$>?BH^N+2$avvm#bI;0n4I9 zz$%Mk-Z5c3{WsH8OT(x`Z}q~I`iTjpX2Yiok{2>* z72BCm){QdN%hb35Wy72{ru1oDMzSJH`uDI@e9BGLR zPdF&mt9j1J{~4NIT1Z5Ow@m%bF6z$s)9hL|#(SBCV2#O?XI9%^q%$YHk*rlNsUw1< z6N%7-nFmo;YQ{auV!D{f_0$B>E!@<)J`2Z3>#7(xbjvbeVgg1-{jy~ka1(SuI{I@~ z`A@21O^=R6gddLzrX~B z^OtCc#j{a+`7?EdfkD2VZ=cEswV&^*ub{qR$pG;|_0=I!@34IlQU@3s;*R6+>*ucl fTmPRJ4b+}GD*7DZoE`OfBpYX8@RRze1{(PVvmr)d literal 0 HcmV?d00001 diff --git a/social_auth/locale/ru/LC_MESSAGES/django.po b/social_auth/locale/ru/LC_MESSAGES/django.po new file mode 100644 index 0000000..1a62569 --- /dev/null +++ b/social_auth/locale/ru/LC_MESSAGES/django.po @@ -0,0 +1,37 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2012-02-16 21:41+0400\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%" +"10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n" + +#: build/lib/social_auth/views.py:70 social_auth/views.py:70 +msgid "Unknown authentication error. Try again later." +msgstr "Непредвиденная ошибка аутентификации. Попробуйте позже." + +#: build/lib/social_auth/backends/__init__.py:634 +#: build/lib/social_auth/backends/__init__.py:655 +#: social_auth/backends/__init__.py:634 social_auth/backends/__init__.py:655 +msgid "Authentication process was cancelled" +msgstr "Процесс аутентификации был прерван" + +#: build/lib/social_auth/backends/__init__.py:636 +#: build/lib/social_auth/backends/__init__.py:657 +#: social_auth/backends/__init__.py:636 social_auth/backends/__init__.py:657 +#, python-format +msgid "Authentication failed: %s" +msgstr "Ошибка аутентификации: %s" diff --git a/social_auth/utils.py b/social_auth/utils.py index 9f288ed..4a0601a 100644 --- a/social_auth/utils.py +++ b/social_auth/utils.py @@ -1,3 +1,4 @@ +# coding=utf-8 import urlparse import logging from collections import defaultdict @@ -6,6 +7,13 @@ from django.conf import settings from django.db.models import Model from django.contrib.contenttypes.models import ContentType +class DSAException(ValueError): + """ + django-social-auth exception. This exception can be showed to user. It is thrown in normal situations – user declined + access, access token expired, etc. + """ + pass + def sanitize_log_data(secret, data=None, leave_characters=4): """ diff --git a/social_auth/views.py b/social_auth/views.py index 45140ee..6caeaeb 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -13,10 +13,11 @@ from django.core.urlresolvers import reverse from django.contrib.auth import login, REDIRECT_FIELD_NAME from django.contrib.auth.decorators import login_required from django.contrib import messages +from django.utils.translation import ugettext from django.views.decorators.csrf import csrf_exempt from social_auth.backends import get_backend -from social_auth.utils import sanitize_redirect, setting, log +from social_auth.utils import sanitize_redirect, setting, log, DSAException DEFAULT_REDIRECT = setting('SOCIAL_AUTH_LOGIN_REDIRECT_URL') or \ @@ -46,6 +47,16 @@ def dsa_view(redirect_name=None): try: return func(request, backend, *args, **kwargs) + except DSAException, e: + backend_name = backend.AUTH_BACKEND.name + if 'django.contrib.messages' in setting('INSTALLED_APPS'): + from django.contrib.messages.api import error + error(request, unicode(e), extra_tags=backend_name) + else: + log('warn', 'Messages framework not in place, some '+ + 'errors have not been shown to the user.') + url = setting('SOCIAL_AUTH_BACKEND_ERROR_URL', LOGIN_ERROR_URL) + return HttpResponseRedirect(url) except Exception, e: # some error ocurred if RAISE_EXCEPTIONS: raise @@ -56,7 +67,7 @@ def dsa_view(redirect_name=None): if 'django.contrib.messages' in setting('INSTALLED_APPS'): from django.contrib.messages.api import error - error(request, unicode(e), extra_tags=backend_name) + error(request, ugettext(u'Unknown authentication error. Try again later.'), extra_tags=backend_name) else: log('warn', 'Messages framework not in place, some '+ 'errors have not been shown to the user.') -- 2.39.5