Skip to content

Commit 69e5e3c

Browse files
Use timezone aware datetimes with oauth2 provider, when supported. Closes encode#947.
1 parent 715bd47 commit 69e5e3c

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

rest_framework/authentication.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"""
44
from __future__ import unicode_literals
55
import base64
6-
from datetime import datetime
76

87
from django.contrib.auth import authenticate
98
from django.core.exceptions import ImproperlyConfigured
109
from rest_framework import exceptions, HTTP_HEADER_ENCODING
1110
from rest_framework.compat import CsrfViewMiddleware
1211
from rest_framework.compat import oauth, oauth_provider, oauth_provider_store
13-
from rest_framework.compat import oauth2_provider
12+
from rest_framework.compat import oauth2_provider, provider_now
1413
from rest_framework.authtoken.models import Token
1514

1615

@@ -320,9 +319,9 @@ def authenticate_credentials(self, request, access_token):
320319

321320
try:
322321
token = oauth2_provider.models.AccessToken.objects.select_related('user')
323-
# TODO: Change to timezone aware datetime when oauth2_provider add
324-
# support to it.
325-
token = token.get(token=access_token, expires__gt=datetime.now())
322+
# provider_now switches to timezone aware datetime when
323+
# the oauth2_provider version supports to it.
324+
token = token.get(token=access_token, expires__gt=provider_now())
326325
except oauth2_provider.models.AccessToken.DoesNotExist:
327326
raise exceptions.AuthenticationFailed('Invalid token')
328327

rest_framework/compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
The `compat` module provides support for backwards compatibility with older
33
versions of django/python, and compatibility wrappers around optional packages.
44
"""
5+
56
# flake8: noqa
67
from __future__ import unicode_literals
78

@@ -489,12 +490,21 @@ def apply_markdown(text):
489490
from provider.oauth2 import forms as oauth2_provider_forms
490491
from provider import scope as oauth2_provider_scope
491492
from provider import constants as oauth2_constants
493+
from provider import __version__ as provider_version
494+
if provider_version in ('0.2.3', '0.2.4'):
495+
# 0.2.3 and 0.2.4 are supported version that do not support
496+
# timezone aware datetimes
497+
from datetime.datetime import now as provider_now
498+
else:
499+
# Any other supported version does use timezone aware datetimes
500+
from django.utils.timezone import now as provider_now
492501
except ImportError:
493502
oauth2_provider = None
494503
oauth2_provider_models = None
495504
oauth2_provider_forms = None
496505
oauth2_provider_scope = None
497506
oauth2_constants = None
507+
provider_now = None
498508

499509
# Handle lazy strings
500510
from django.utils.functional import Promise

rest_framework/serializers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,10 @@ def get_default_fields(self):
915915
view_name=self.opts.view_name,
916916
lookup_field=self.opts.lookup_field
917917
)
918-
fields.insert(0, 'url', url_field)
918+
ret = self._dict_class()
919+
ret['url'] = url_field
920+
ret.update(fields)
921+
fields = ret
919922

920923
return fields
921924

0 commit comments

Comments
 (0)