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

Commit 77da4d6

Browse files
committed
Use ugettext function to translate message strings
1 parent a6524cc commit 77da4d6

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
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

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)

0 commit comments

Comments
 (0)