Skip to content

Commit c5a5f03

Browse files
committed
compat module simplified
1 parent 7a34535 commit c5a5f03

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

oauth2_provider/compat.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
The `compat` module provides support for backwards compatibility with older
3+
versions of django and python..
4+
"""
5+
6+
from __future__ import unicode_literals
7+
8+
import django
9+
from django.conf import settings
10+
111
# urlparse in python3 has been renamed to urllib.parse
212
try:
313
from urlparse import urlparse, parse_qs
@@ -10,22 +20,7 @@
1020
from urllib.parse import urlencode
1121

1222
# Django 1.5 add support for custom auth user model
13-
import django
14-
if django.VERSION >= (1, 5):
15-
from django.conf import settings
16-
if hasattr(settings, 'AUTH_USER_MODEL'):
17-
User = settings.AUTH_USER_MODEL
18-
else:
19-
from django.contrib.auth.models import User
20-
else:
21-
try:
22-
from django.contrib.auth.models import User
23-
except ImportError:
24-
raise ImportError("User model is not to be found.")
25-
2623
if django.VERSION >= (1, 5):
27-
from django.contrib.auth import get_user_model
24+
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
2825
else:
29-
def get_user_model():
30-
from django.contrib.auth.models import User
31-
return User
26+
AUTH_USER_MODEL = 'auth.User'

oauth2_provider/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.core.exceptions import ImproperlyConfigured
1010

1111
from .settings import oauth2_settings
12-
from .compat import User
12+
from .compat import AUTH_USER_MODEL
1313
from .generators import generate_client_secret, generate_client_id
1414
from .validators import validate_uris
1515

@@ -57,7 +57,7 @@ class AbstractApplication(models.Model):
5757

5858
client_id = models.CharField(max_length=100, unique=True,
5959
default=generate_client_id)
60-
user = models.ForeignKey(User)
60+
user = models.ForeignKey(AUTH_USER_MODEL)
6161
help_text = _("Allowed URIs list, space separated")
6262
redirect_uris = models.TextField(help_text=help_text,
6363
validators=[validate_uris], blank=True)
@@ -129,7 +129,7 @@ class Grant(models.Model):
129129
* :attr:`redirect_uri` Self explained
130130
* :attr:`scope` Required scopes, optional
131131
"""
132-
user = models.ForeignKey(User)
132+
user = models.ForeignKey(AUTH_USER_MODEL)
133133
code = models.CharField(max_length=255) # code comes from oauthlib
134134
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
135135
expires = models.DateTimeField()
@@ -164,7 +164,7 @@ class AccessToken(models.Model):
164164
:data:`settings.ACCESS_TOKEN_EXPIRE_SECONDS`
165165
* :attr:`scope` Allowed scopes
166166
"""
167-
user = models.ForeignKey(User)
167+
user = models.ForeignKey(AUTH_USER_MODEL)
168168
token = models.CharField(max_length=255)
169169
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
170170
expires = models.DateTimeField()
@@ -216,7 +216,7 @@ class RefreshToken(models.Model):
216216
* :attr:`access_token` AccessToken instance this refresh token is
217217
bounded to
218218
"""
219-
user = models.ForeignKey(User)
219+
user = models.ForeignKey(AUTH_USER_MODEL)
220220
token = models.CharField(max_length=255)
221221
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
222222
access_token = models.OneToOneField(AccessToken,

0 commit comments

Comments
 (0)