Skip to content

Commit 43c1b5b

Browse files
committed
application registration view uses custom application model in form class
1 parent 95442d2 commit 43c1b5b

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

oauth2_provider/forms.py

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

3-
from .models import Application
4-
53

64
class AllowForm(forms.Form):
75
allow = forms.BooleanField(required=False)
@@ -17,12 +15,3 @@ def __init__(self, *args, **kwargs):
1715
if data and 'scopes' in data:
1816
data['scope'] = data['scopes']
1917
return super(AllowForm, self).__init__(*args, **kwargs)
20-
21-
22-
class RegistrationForm(forms.ModelForm):
23-
"""
24-
TODO: add docstring
25-
"""
26-
class Meta:
27-
model = Application
28-
fields = ('name', 'client_id', 'client_secret', 'client_type', 'authorization_grant_type', 'redirect_uris')

oauth2_provider/tests/test_application_views.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import unicode_literals
22

3+
import mock
34
from django.core.urlresolvers import reverse
45
from django.test import TestCase
6+
from django.test.utils import override_settings
57

68
from ..models import get_application_model
79
from ..compat import get_user_model
@@ -21,6 +23,24 @@ def tearDown(self):
2123

2224

2325
class TestApplicationRegistrationView(BaseTest):
26+
def test_get_form_class(self):
27+
"""
28+
Tests that the form class returned by the 'get_form_class' method is
29+
bound to custom application model defined in the
30+
'OAUTH2_PROVIDER_APPLICATION_MODEL' setting.
31+
"""
32+
from ..views.application import ApplicationRegistration
33+
from .models import TestApplication
34+
from ..settings import oauth2_settings
35+
# Patch oauth2 settings to use a custom Application model
36+
oauth2_settings.APPLICATION_MODEL = 'tests.TestApplication'
37+
# Create a registration view and tests that the model form is bound
38+
# to the custom Application model
39+
application_form_class = ApplicationRegistration().get_form_class()
40+
self.assertEqual(TestApplication, application_form_class._meta.model)
41+
# Revert oauth2 settings
42+
oauth2_settings.APPLICATION_MODEL = 'oauth2_provider.Application'
43+
2444
def test_application_registration_user(self):
2545
self.client.login(username="foo_user", password="123456")
2646

oauth2_provider/views/application.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from django.core.urlresolvers import reverse_lazy
2+
from django.forms.models import modelform_factory
23
from django.views.generic import CreateView, DetailView, DeleteView, ListView, UpdateView
34

45
from braces.views import LoginRequiredMixin
56

6-
from ..forms import RegistrationForm
77
from ..models import get_application_model
88

99

@@ -21,9 +21,18 @@ class ApplicationRegistration(LoginRequiredMixin, CreateView):
2121
"""
2222
View used to register a new Application for the request.user
2323
"""
24-
form_class = RegistrationForm
2524
template_name = "oauth2_provider/application_registration_form.html"
2625

26+
def get_form_class(self):
27+
"""
28+
Returns the form class for the application model
29+
"""
30+
return modelform_factory(
31+
get_application_model(),
32+
fields=('name', 'client_id', 'client_secret', 'client_type',
33+
'authorization_grant_type', 'redirect_uris')
34+
)
35+
2736
def form_valid(self, form):
2837
form.instance.user = self.request.user
2938
return super(ApplicationRegistration, self).form_valid(form)

0 commit comments

Comments
 (0)