Skip to content

Commit f5a1a49

Browse files
committed
fixed #151: get_application_model incompatible with Django 1.7
1 parent fed892b commit f5a1a49

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

oauth2_provider/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django import forms
22

3-
from .models import get_application_model
3+
from .models import Application
44

55

66
class AllowForm(forms.Form):
@@ -24,5 +24,5 @@ class RegistrationForm(forms.ModelForm):
2424
TODO: add docstring
2525
"""
2626
class Meta:
27-
model = get_application_model()
27+
model = Application
2828
fields = ('name', 'client_id', 'client_secret', 'client_type', 'authorization_grant_type', 'redirect_uris')

oauth2_provider/oauth2_validators.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@
1111
from oauthlib.oauth2 import RequestValidator
1212

1313
from .compat import unquote_plus
14-
from .models import Grant, AccessToken, RefreshToken, get_application_model
14+
from .models import Grant, AccessToken, RefreshToken, get_application_model, AbstractApplication
1515
from .settings import oauth2_settings
1616

17-
Application = get_application_model()
18-
1917
log = logging.getLogger('oauth2_provider')
2018

2119
GRANT_TYPE_MAPPING = {
22-
'authorization_code': (Application.GRANT_AUTHORIZATION_CODE,),
23-
'password': (Application.GRANT_PASSWORD,),
24-
'client_credentials': (Application.GRANT_CLIENT_CREDENTIALS,),
25-
'refresh_token': (Application.GRANT_AUTHORIZATION_CODE, Application.GRANT_PASSWORD,
26-
Application.GRANT_CLIENT_CREDENTIALS)
20+
'authorization_code': (AbstractApplication.GRANT_AUTHORIZATION_CODE,),
21+
'password': (AbstractApplication.GRANT_PASSWORD,),
22+
'client_credentials': (AbstractApplication.GRANT_CLIENT_CREDENTIALS,),
23+
'refresh_token': (AbstractApplication.GRANT_AUTHORIZATION_CODE, AbstractApplication.GRANT_PASSWORD,
24+
AbstractApplication.GRANT_CLIENT_CREDENTIALS)
2725
}
2826

2927

@@ -117,6 +115,7 @@ def _load_application(self, client_id, request):
117115
# we want to be sure that request has the client attribute!
118116
assert hasattr(request, "client"), "'request' instance has no 'client' attribute"
119117

118+
Application = get_application_model()
120119
try:
121120
request.client = request.client or Application.objects.get(client_id=client_id)
122121
return request.client
@@ -149,7 +148,7 @@ def client_authentication_required(self, request, *args, **kwargs):
149148

150149
self._load_application(request.client_id, request)
151150
if request.client:
152-
return request.client.client_type == Application.CLIENT_CONFIDENTIAL
151+
return request.client.client_type == AbstractApplication.CLIENT_CONFIDENTIAL
153152

154153
return super(OAuth2Validator, self).client_authentication_required(request,
155154
*args, **kwargs)
@@ -179,7 +178,7 @@ def authenticate_client_id(self, client_id, request, *args, **kwargs):
179178
"""
180179
if self._load_application(client_id, request) is not None:
181180
log.debug("Application %s has type %s" % (client_id, request.client.client_type))
182-
return request.client.client_type != Application.CLIENT_CONFIDENTIAL
181+
return request.client.client_type != AbstractApplication.CLIENT_CONFIDENTIAL
183182
return False
184183

185184
def confirm_redirect_uri(self, client_id, code, redirect_uri, client, *args, **kwargs):
@@ -253,9 +252,9 @@ def validate_response_type(self, client_id, response_type, client, request, *arg
253252
rfc:`8.4`, so validate the response_type only if it matches 'code' or 'token'
254253
"""
255254
if response_type == 'code':
256-
return client.authorization_grant_type == Application.GRANT_AUTHORIZATION_CODE
255+
return client.authorization_grant_type == AbstractApplication.GRANT_AUTHORIZATION_CODE
257256
elif response_type == 'token':
258-
return client.authorization_grant_type == Application.GRANT_IMPLICIT
257+
return client.authorization_grant_type == AbstractApplication.GRANT_IMPLICIT
259258
else:
260259
return False
261260

oauth2_provider/views/application.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ class ApplicationOwnerIsUserMixin(LoginRequiredMixin):
1111
"""
1212
This mixin is used to provide an Application queryset filtered by the current request.user.
1313
"""
14-
model = get_application_model()
1514
fields = '__all__'
1615

1716
def get_queryset(self):
18-
queryset = super(ApplicationOwnerIsUserMixin, self).get_queryset()
19-
return queryset.filter(user=self.request.user)
17+
return get_application_model().objects.filter(user=self.request.user)
2018

2119

2220
class ApplicationRegistration(LoginRequiredMixin, CreateView):

oauth2_provider/views/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from ..models import get_application_model
1818
from .mixins import OAuthLibMixin
1919

20-
Application = get_application_model()
21-
2220
log = logging.getLogger('oauth2_provider')
2321

2422

@@ -116,7 +114,7 @@ def get(self, request, *args, **kwargs):
116114
kwargs['scopes_descriptions'] = [oauth2_settings.SCOPES[scope] for scope in scopes]
117115
kwargs['scopes'] = scopes
118116
# at this point we know an Application instance with such client_id exists in the database
119-
application = Application.objects.get(client_id=credentials['client_id']) # TODO: cache it!
117+
application = get_application_model().objects.get(client_id=credentials['client_id']) # TODO: cache it!
120118
kwargs['application'] = application
121119
kwargs.update(credentials)
122120
self.oauth2_data = kwargs
@@ -147,7 +145,7 @@ def get(self, request, *args, **kwargs):
147145
uri, headers, body, status = self.create_authorization_response(
148146
request=self.request, scopes=" ".join(scopes),
149147
credentials=credentials, allow=True)
150-
return HttpResponseUriRedirect(uri)
148+
return HttpResponseUriRedirect(uri)
151149

152150
return self.render_to_response(self.get_context_data(**kwargs))
153151

0 commit comments

Comments
 (0)