Repository: jazzband/django-oauth-toolkit

Issue:
BUG: Application registration form not working for extended application models
Submitting the registration form redirects to a 404 page when using a custom application model.

Upon initial inspection of the code, the problem seems to have been introduced in commit f5a1a493b939447076cb7af7553522b398b1d1eb while fixing #151. It looks like the form is using the default application model.



Discussion:
I just ran into this, too after upgrading to 0.9.0.  I'm not sure what issue really caused #151, but I'm running Django 1.7.10 and when I change the `RegistrationForm` code to 

``` python
...
from .models import get_application_model

Application = get_application_model()

...

class RegistrationForm(forms.ModelForm):
    class Meta:
        model = Application
        fields = ('name', 'client_id', 'client_secret', 'client_type', 'authorization_grant_type', 'redirect_uris')
```

everything works just fine.  So +1 to changing `RegistrationForm` back to properly using custom application models

Any status on this I have just ran into the same situation tonight. Will the pull request that @jannon made make it into master soonish or do I have to hack my local version? On a deadline need to use a custom model. 

Using Django 1.8.4


Resolving patch:
diff --git a/oauth2_provider/forms.py b/oauth2_provider/forms.py
--- a/oauth2_provider/forms.py
+++ b/oauth2_provider/forms.py
@@ -1,7 +1,5 @@
 from django import forms
 
-from .models import Application
-
 
 class AllowForm(forms.Form):
     allow = forms.BooleanField(required=False)
@@ -17,12 +15,3 @@ def __init__(self, *args, **kwargs):
         if data and 'scopes' in data:
             data['scope'] = data['scopes']
         return super(AllowForm, self).__init__(*args, **kwargs)
-
-
-class RegistrationForm(forms.ModelForm):
-    """
-    TODO: add docstring
-    """
-    class Meta:
-        model = Application
-        fields = ('name', 'client_id', 'client_secret', 'client_type', 'authorization_grant_type', 'redirect_uris')
diff --git a/oauth2_provider/views/application.py b/oauth2_provider/views/application.py
--- a/oauth2_provider/views/application.py
+++ b/oauth2_provider/views/application.py
@@ -1,9 +1,9 @@
 from django.core.urlresolvers import reverse_lazy
+from django.forms.models import modelform_factory
 from django.views.generic import CreateView, DetailView, DeleteView, ListView, UpdateView
 
 from braces.views import LoginRequiredMixin
 
-from ..forms import RegistrationForm
 from ..models import get_application_model
 
 
@@ -21,9 +21,18 @@ class ApplicationRegistration(LoginRequiredMixin, CreateView):
     """
     View used to register a new Application for the request.user
     """
-    form_class = RegistrationForm
     template_name = "oauth2_provider/application_registration_form.html"
 
+    def get_form_class(self):
+        """
+        Returns the form class for the application model
+        """
+        return modelform_factory(
+            get_application_model(),
+            fields=('name', 'client_id', 'client_secret', 'client_type',
+                    'authorization_grant_type', 'redirect_uris')
+        )
+
     def form_valid(self, form):
         form.instance.user = self.request.user
         return super(ApplicationRegistration, self).form_valid(form)


Test patch:
diff --git a/oauth2_provider/tests/test_application_views.py b/oauth2_provider/tests/test_application_views.py
--- a/oauth2_provider/tests/test_application_views.py
+++ b/oauth2_provider/tests/test_application_views.py
@@ -1,7 +1,9 @@
 from __future__ import unicode_literals
 
+import mock
 from django.core.urlresolvers import reverse
 from django.test import TestCase
+from django.test.utils import override_settings
 
 from ..models import get_application_model
 from ..compat import get_user_model
@@ -21,6 +23,24 @@ def tearDown(self):
 
 
 class TestApplicationRegistrationView(BaseTest):
+    def test_get_form_class(self):
+        """
+        Tests that the form class returned by the 'get_form_class' method is
+        bound to custom application model defined in the
+        'OAUTH2_PROVIDER_APPLICATION_MODEL' setting.
+        """
+        from ..views.application import ApplicationRegistration
+        from .models import TestApplication
+        from ..settings import oauth2_settings
+        # Patch oauth2 settings to use a custom Application model
+        oauth2_settings.APPLICATION_MODEL = 'tests.TestApplication'
+        # Create a registration view and tests that the model form is bound
+        # to the custom Application model
+        application_form_class = ApplicationRegistration().get_form_class()
+        self.assertEqual(TestApplication, application_form_class._meta.model)
+        # Revert oauth2 settings
+        oauth2_settings.APPLICATION_MODEL = 'oauth2_provider.Application'
+
     def test_application_registration_user(self):
         self.client.login(username="foo_user", password="123456")