Skip to content

Commit

Permalink
application registration view uses custom application model in form c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
synasius committed Nov 20, 2015
1 parent 95442d2 commit 43c1b5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
11 changes: 0 additions & 11 deletions oauth2_provider/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django import forms

from .models import Application


class AllowForm(forms.Form):
allow = forms.BooleanField(required=False)
Expand All @@ -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')
20 changes: 20 additions & 0 deletions oauth2_provider/tests/test_application_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")

Expand Down
13 changes: 11 additions & 2 deletions oauth2_provider/views/application.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Expand Down

0 comments on commit 43c1b5b

Please sign in to comment.