@@ -165,7 +165,7 @@ def __str__(self):
165165
166166
167167@python_2_unicode_compatible
168- class AccessToken (models .Model ):
168+ class AbstractAccessToken (models .Model ):
169169 """
170170 An AccessToken instance represents the actual access token to
171171 access user's resources, as in :rfc:`5`.
@@ -217,8 +217,15 @@ def __str__(self):
217217 return self .token
218218
219219
220+ class AccessToken (AbstractAccessToken ):
221+ pass
222+
223+ # Add swappable like this to not break django 1.4 compatibility
224+ AccessToken ._meta .swappable = 'OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL'
225+
226+
220227@python_2_unicode_compatible
221- class RefreshToken (models .Model ):
228+ class AbstractRefreshToken (models .Model ):
222229 """
223230 A RefreshToken instance represents a token that can be swapped for a new
224231 access token when it expires.
@@ -234,13 +241,20 @@ class RefreshToken(models.Model):
234241 user = models .ForeignKey (AUTH_USER_MODEL )
235242 token = models .CharField (max_length = 255 , db_index = True )
236243 application = models .ForeignKey (oauth2_settings .APPLICATION_MODEL )
237- access_token = models .OneToOneField (AccessToken ,
244+ access_token = models .OneToOneField (oauth2_settings . ACCESS_TOKEN_MODEL ,
238245 related_name = 'refresh_token' )
239246
240247 def __str__ (self ):
241248 return self .token
242249
243250
251+ class RefreshToken (AbstractRefreshToken ):
252+ pass
253+
254+ # Add swappable like this to not break django 1.4 compatibility
255+ RefreshToken ._meta .swappable = 'OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL'
256+
257+
244258def get_application_model ():
245259 """ Return the Application model that is active in this project. """
246260 try :
@@ -253,3 +267,31 @@ def get_application_model():
253267 e = "APPLICATION_MODEL refers to model {0} that has not been installed"
254268 raise ImproperlyConfigured (e .format (oauth2_settings .APPLICATION_MODEL ))
255269 return app_model
270+
271+
272+ def get_access_token_model ():
273+ """ Return the AccessToken model that is active in this project. """
274+ try :
275+ app_label , model_name = oauth2_settings .ACCESS_TOKEN_MODEL .split ('.' )
276+ except ValueError :
277+ e = "ACCESS_TOKEN_MODEL must be of the form 'app_label.model_name'"
278+ raise ImproperlyConfigured (e )
279+ access_token_model = get_model (app_label , model_name )
280+ if access_token_model is None :
281+ e = "ACCESS_TOKEN_MODEL refers to model {0} that has not been installed"
282+ raise ImproperlyConfigured (e .format (oauth2_settings .ACCESS_TOKEN_MODEL ))
283+ return access_token_model
284+
285+
286+ def get_refresh_token_model ():
287+ """ Return the RefreshToken model that is active in this project. """
288+ try :
289+ app_label , model_name = oauth2_settings .REFRESH_TOKEN_MODEL .split ('.' )
290+ except ValueError :
291+ e = "REFRESH_TOKEN_MODEL must be of the form 'app_label.model_name'"
292+ raise ImproperlyConfigured (e )
293+ refresh_token_model = get_model (app_label , model_name )
294+ if refresh_token_model is None :
295+ e = "REFRESH_TOKEN_MODEL refers to model {0} that has not been installed"
296+ raise ImproperlyConfigured (e .format (oauth2_settings .REFRESH_TOKEN_MODEL ))
297+ return refresh_token_model
0 commit comments