Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit a61568c

Browse files
committed
Merge pull request #68 from migonzalvar/add_translation
Add translation utils
2 parents a6524cc + ea6bbb1 commit a61568c

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

rest_framework_jwt/authentication.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jwt
22

33
from django.utils.encoding import smart_text
4+
from django.utils.translation import ugettext as _
45
from rest_framework import exceptions
56
from rest_framework_jwt.settings import api_settings
67
from rest_framework.authentication import (BaseAuthentication,
@@ -42,20 +43,20 @@ def authenticate(self, request):
4243
return None
4344

4445
if len(auth) == 1:
45-
msg = 'Invalid Authorization header. No credentials provided.'
46+
msg = _('Invalid Authorization header. No credentials provided.')
4647
raise exceptions.AuthenticationFailed(msg)
4748
elif len(auth) > 2:
48-
msg = ('Invalid Authorization header. Credentials string '
49-
'should not contain spaces.')
49+
msg = _('Invalid Authorization header. Credentials string '
50+
'should not contain spaces.')
5051
raise exceptions.AuthenticationFailed(msg)
5152

5253
try:
5354
payload = jwt_decode_handler(auth[1])
5455
except jwt.ExpiredSignature:
55-
msg = 'Signature has expired.'
56+
msg = _('Signature has expired.')
5657
raise exceptions.AuthenticationFailed(msg)
5758
except jwt.DecodeError:
58-
msg = 'Error decoding signature.'
59+
msg = _('Error decoding signature.')
5960
raise exceptions.AuthenticationFailed(msg)
6061

6162
user = self.authenticate_credentials(payload)
@@ -72,10 +73,10 @@ def authenticate_credentials(self, payload):
7273
if user_id is not None:
7374
user = User.objects.get(pk=user_id, is_active=True)
7475
else:
75-
msg = 'Invalid payload'
76+
msg = _('Invalid payload.')
7677
raise exceptions.AuthenticationFailed(msg)
7778
except User.DoesNotExist:
78-
msg = 'Invalid signature'
79+
msg = _('Invalid signature.')
7980
raise exceptions.AuthenticationFailed(msg)
8081

8182
return user
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the PACKAGE package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: PACKAGE VERSION\n"
10+
"Report-Msgid-Bugs-To: \n"
11+
"POT-Creation-Date: 2015-01-16 18:06+0100\n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <[email protected]>\n"
15+
"Language: \n"
16+
"MIME-Version: 1.0\n"
17+
"Content-Type: text/plain; charset=UTF-8\n"
18+
"Content-Transfer-Encoding: 8bit\n"
19+
20+
#: authentication.py:46
21+
msgid "Invalid Authorization header. No credentials provided."
22+
msgstr ""
23+
24+
#: authentication.py:49
25+
msgid ""
26+
"Invalid Authorization header. Credentials string should not contain spaces."
27+
msgstr ""
28+
29+
#: authentication.py:56 serializers.py:92
30+
msgid "Signature has expired."
31+
msgstr ""
32+
33+
#: authentication.py:59 serializers.py:95
34+
msgid "Error decoding signature."
35+
msgstr ""
36+
37+
#: authentication.py:76 serializers.py:105
38+
msgid "Invalid payload."
39+
msgstr ""
40+
41+
#: authentication.py:79
42+
msgid "Invalid signature."
43+
msgstr ""
44+
45+
#: serializers.py:53
46+
msgid "User account is disabled."
47+
msgstr ""
48+
49+
#: serializers.py:69
50+
msgid "Unable to login with provided credentials."
51+
msgstr ""
52+
53+
#: serializers.py:72
54+
#, python-brace-format
55+
msgid "Must include \"{username_field}\" and \"password\"."
56+
msgstr ""
57+
58+
#: serializers.py:108
59+
msgid "User doesn't exist."
60+
msgstr ""
61+
62+
#: serializers.py:126
63+
msgid "Refresh has expired."
64+
msgstr ""
65+
66+
#: serializers.py:129
67+
msgid "orig_iat field is required."
68+
msgstr ""

rest_framework_jwt/serializers.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import jwt
44

55
from django.contrib.auth import authenticate
6+
from django.utils.translation import ugettext as _
67
from rest_framework import serializers
78
from .compat import Serializer
89

@@ -49,7 +50,7 @@ def validate(self, attrs):
4950

5051
if user:
5152
if not user.is_active:
52-
msg = 'User account is disabled.'
53+
msg = _('User account is disabled.')
5354
raise serializers.ValidationError(msg)
5455

5556
payload = jwt_payload_handler(user)
@@ -65,11 +66,11 @@ def validate(self, attrs):
6566
'token': jwt_encode_handler(payload)
6667
}
6768
else:
68-
msg = 'Unable to login with provided credentials.'
69+
msg = _('Unable to login with provided credentials.')
6970
raise serializers.ValidationError(msg)
7071
else:
71-
msg = 'Must include "{0}" and "password"'.format(
72-
self.username_field)
72+
msg = _('Must include "{username_field}" and "password".')
73+
msg = msg.format(username_field=self.username_field)
7374
raise serializers.ValidationError(msg)
7475

7576

@@ -88,10 +89,10 @@ def validate(self, attrs):
8889
try:
8990
payload = jwt_decode_handler(token)
9091
except jwt.ExpiredSignature:
91-
msg = 'Signature has expired.'
92+
msg = _('Signature has expired.')
9293
raise serializers.ValidationError(msg)
9394
except jwt.DecodeError:
94-
msg = 'Error decoding signature.'
95+
msg = _('Error decoding signature.')
9596
raise serializers.ValidationError(msg)
9697

9798
# Make sure user exists (may want to refactor this)
@@ -101,10 +102,10 @@ def validate(self, attrs):
101102
if user_id is not None:
102103
user = User.objects.get(pk=user_id, is_active=True)
103104
else:
104-
msg = 'Invalid payload'
105+
msg = _('Invalid payload.')
105106
raise serializers.ValidationError(msg)
106107
except User.DoesNotExist:
107-
msg = "User doesn't exist"
108+
msg = _("User doesn't exist.")
108109
raise serializers.ValidationError(msg)
109110

110111
# Get and check 'orig_iat'
@@ -122,10 +123,10 @@ def validate(self, attrs):
122123
now_timestamp = timegm(datetime.utcnow().utctimetuple())
123124

124125
if now_timestamp > expiration_timestamp:
125-
msg = 'Refresh has expired'
126+
msg = _('Refresh has expired.')
126127
raise serializers.ValidationError(msg)
127128
else:
128-
msg = 'orig_iat field is required'
129+
msg = _('orig_iat field is required.')
129130
raise serializers.ValidationError(msg)
130131

131132
new_payload = jwt_payload_handler(user)

tests/test_authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def test_post_form_passing_jwt_invalid_payload(self):
220220
response = self.csrf_client.post(
221221
'/jwt/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
222222

223-
msg = 'Invalid payload'
223+
msg = 'Invalid payload.'
224224

225225
self.assertEqual(response.data['detail'], msg)
226226
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

tests/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_refresh_jwt_after_refresh_expiration(self):
265265
format='json')
266266
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
267267
self.assertEqual(response.data['non_field_errors'][0],
268-
'Refresh has expired')
268+
'Refresh has expired.')
269269

270270
def tearDown(self):
271271
# Restore original settings

0 commit comments

Comments
 (0)