Skip to content

Commit 70acfcb

Browse files
committed
merged into master
2 parents d15d8ee + a83f19a commit 70acfcb

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

Diff for: oauth2_provider/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def __str__(self):
109109
class Application(AbstractApplication):
110110
pass
111111

112+
# Add swappable like this to not break django 1.4 compatibility
113+
Application._meta.swappable = 'OAUTH2_PROVIDER_APPLICATION_MODEL'
114+
112115

113116
@python_2_unicode_compatible
114117
class Grant(models.Model):

Diff for: oauth2_provider/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
'WRITE_SCOPE': 'write',
3535
'AUTHORIZATION_CODE_EXPIRE_SECONDS': 60,
3636
'ACCESS_TOKEN_EXPIRE_SECONDS': 36000,
37-
'APPLICATION_MODEL': 'oauth2_provider.Application',
37+
'APPLICATION_MODEL': getattr(settings, 'OAUTH2_PROVIDER_APPLICATION_MODEL', 'oauth2_provider.Application'),
3838

3939
# Special settings that will be evaluated at runtime
4040
'_SCOPES': [],

Diff for: oauth2_provider/tests/models.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
from oauth2_provider.models import AbstractApplication
3+
4+
5+
class TestApplication(AbstractApplication):
6+
custom_field = models.CharField(max_length=255)

Diff for: oauth2_provider/tests/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
'django.contrib.admin',
6969

7070
'oauth2_provider',
71+
'oauth2_provider.tests',
7172
)
7273

7374
LOGGING = {

Diff for: oauth2_provider/tests/test_models.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
from __future__ import unicode_literals
22

3+
try:
4+
from unittest import skipIf
5+
except ImportError:
6+
from django.utils.unittest.case import skipIf
7+
8+
import django
39
from django.test import TestCase
10+
from django.test.utils import override_settings
411
from django.core.exceptions import ValidationError
512

613
from ..models import AccessToken, get_application_model
@@ -72,3 +79,22 @@ def test_str(self):
7279

7380
app.name = "test_app"
7481
self.assertEqual("%s" % app, "test_app")
82+
83+
@skipIf(django.VERSION < (1, 5), "Behavior is broken on 1.4 and there is no solution")
84+
@override_settings(OAUTH2_PROVIDER_APPLICATION_MODEL='tests.TestApplication')
85+
class TestCustomApplicationModel(TestCase):
86+
def setUp(self):
87+
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
88+
89+
def test_related_objects(self):
90+
"""
91+
If a custom application model is installed, it should be present in
92+
the related objects and not the swapped out one.
93+
94+
See issue #90 (https://github.com/evonove/django-oauth-toolkit/issues/90)
95+
"""
96+
# Django internals caches the related objects.
97+
del UserModel._meta._related_objects_cache
98+
related_object_names = [ro.name for ro in UserModel._meta.get_all_related_objects()]
99+
self.assertNotIn('oauth2_provider:application', related_object_names)
100+
self.assertIn('tests:testapplication', related_object_names)

0 commit comments

Comments
 (0)