Repository: jazzband/django-oauth-toolkit

Issue:
Lazy reference issues when swapping the application model on Django 1.10
Hi,

I'd like to thank you for this app and your support. Much appreciated. 

I'm running into some lazy reference issues when I try to swap the application model on django 1.10. I believe I've followed the steps outlined in https://django-oauth-toolkit.readthedocs.io/en/latest/advanced_topics.html. I'm running into some migration issues when I try to create a migration to add the new model. Here's the output:

`(env-brownpapersession)bastb@bastb-vps:/var/www/brownpapersession/dev/brownpapersession$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 167, in handle
    migration_name=self.migration_name,
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 47, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 132, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 180, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 249, in __init__
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'app.brownpaperapplication', but app 'app' doesn't provide model 'brownpaperapplication'.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'app.brownpaperapplication', but app 'app' doesn't provide model 'brownpaperapplication'.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'app.brownpaperapplication', but app 'app' doesn't provide model 'brownpaperapplication'.`

I've changed the setting and added the model

`OAUTH2_PROVIDER_APPLICATION_MODEL = 'app.BrownPaperApplication'
`

Can you help me?

OAuth2TokenMiddleware does not work with Django 1.10
Adding the OAuth2TokenMiddleware to the MIDDLEWARE array will break the application:
`
Traceback (most recent call last):
...
  File "/var/www/brownpapersession/dev/env-brownpapersession/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
TypeError: object.__new__() takes no parameters
`

Mitigation described here: https://docs.djangoproject.com/en/1.10/topics/http/middleware/



Discussion:
I found the solution while writing this PR but decided to go ahead filing the PR anyway.

The solution was actually quite simple. First add the swapping model and create and run the migration. Then change the setting to swap the model. 



Resolving patch:
diff --git a/oauth2_provider/middleware.py b/oauth2_provider/middleware.py
--- a/oauth2_provider/middleware.py
+++ b/oauth2_provider/middleware.py
@@ -1,8 +1,18 @@
 from django.contrib.auth import authenticate
 from django.utils.cache import patch_vary_headers
 
+# bastb Django 1.10 has updated Middleware. This code imports the Mixin required to get old-style
+# middleware working again
+# More? 
+#  https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
+try:
+    from django.utils.deprecation import MiddlewareMixin
+    middleware_parent_class = MiddlewareMixin
+except ImportError:
+    middleware_parent_class = object
 
-class OAuth2TokenMiddleware(object):
+
+class OAuth2TokenMiddleware(middleware_parent_class):
     """
     Middleware for OAuth2 user authentication
 


Test patch: