add in admin page for creating oauth keys (bug 600422)

This commit is contained in:
Andy McKay 2011-07-06 09:20:31 -07:00
Родитель feb4352793
Коммит 1649873da6
7 изменённых файлов: 64 добавлений и 1 удалений

9
apps/zadmin/admin.py Normal file
Просмотреть файл

@ -0,0 +1,9 @@
from django.contrib import admin
from piston.models import Consumer
class ConsumerAdmin(admin.ModelAdmin):
list_display = ('name', 'key', 'secret', 'status', 'user')
raw_id_fields = ('user',)
admin.site.register(Consumer, ConsumerAdmin)

Просмотреть файл

@ -7,6 +7,7 @@ from django.forms.models import modelformset_factory
from django.template import Context, Template, TemplateSyntaxError
import happyforms
from piston.models import Consumer
from tower import ugettext_lazy as _lazy
from quieter_formset.formset import BaseModelFormSet
@ -158,3 +159,9 @@ class BaseFeaturedCollectionFormSet(BaseModelFormSet):
FeaturedCollectionFormSet = modelformset_factory(FeaturedCollection,
form=FeaturedCollectionForm, formset=BaseFeaturedCollectionFormSet,
can_delete=True, extra=0)
class OAuthConsumerForm(happyforms.ModelForm):
class Meta:
model = Consumer
fields = ['name', 'description', 'status']

Просмотреть файл

@ -17,6 +17,7 @@
('Jetpack', url('zadmin.jetpack'), 'Upgrade jetpack add-ons'),
('Mail', url('zadmin.mail'), 'View fake mail'),
('Models', url('zadmin.home'), 'Django Admin Pages'),
('OAuth Consumer', url('zadmin.oauth-consumer-create'), 'Create a new OAuth Consumer'),
('Settings', url('zadmin.settings'), 'See the site settings'),
('Validation', url('zadmin.start_validation'), 'Bulk add-on validation'),
) %}

Просмотреть файл

@ -0,0 +1,14 @@
{% extends "admin/base.html" %}
{% block title %}{{ page_title('Create Consumer for OAuth') }}{% endblock %}
{% block content %}
<h2>Create Consumer for OAuth</h2>
<p>Creates a consumer application for OAuth, automatically generating the consumer key and secret.</p>
<form method="post" action="">
{{ csrf() }}
{{ form.as_p() }}
<input type="submit" value="Create" />
</form>
{% endblock %}

Просмотреть файл

@ -11,6 +11,7 @@ from django.core import mail
import mock
from nose.plugins.attrib import attr
from nose.tools import eq_
from piston.models import Consumer
from pyquery import PyQuery as pq
import test_utils
@ -1123,3 +1124,18 @@ class TestFeatures(test_utils.TestCase):
d.update(DELETE=True)
r = self.client.post(self.url, formset(d, initial_count=1))
eq_(FeaturedCollection.objects.count(), 0)
class TestOAuth(test_utils.TestCase):
fixtures = ['base/users',]
def setUp(self):
assert self.client.login(username='admin@mozilla.com',
password='password')
def test_create_consumer(self):
self.client.post(reverse('zadmin.oauth-consumer-create'),
data={'name': 'Test',
'description': 'Test description',
'status': 'accepted'})
eq_(Consumer.objects.count(), 1)

Просмотреть файл

@ -47,6 +47,8 @@ urlpatterns = patterns('',
url('^addon-name-blocklist$', views.addon_name_blocklist,
name='zadmin.addon-name-blocklist'),
url('^addon-search$', views.addon_search, name='zadmin.addon-search'),
url('^oauth-consumer-create$', views.oauth_consumer_create,
name='zadmin.oauth-consumer-create'),
# The Django admin.
url('^models/', include(admin.site.urls)),

Просмотреть файл

@ -40,7 +40,8 @@ from files.models import Approval, File
from versions.models import Version
from . import tasks
from .forms import BulkValidationForm, NotifyForm, FeaturedCollectionFormSet
from .forms import (BulkValidationForm, FeaturedCollectionFormSet, NotifyForm,
OAuthConsumerForm)
from .models import ValidationJob, EmailPreviewTopic, ValidationJobTally
log = commonware.log.getLogger('z.zadmin')
@ -479,3 +480,16 @@ def addon_search(request):
return redirect('/admin/addons?q=[%s]' % qs[0].id)
ctx['addons'] = qs
return jingo.render(request, 'zadmin/addon-search.html', ctx)
@admin.site.admin_view
def oauth_consumer_create(request):
form = OAuthConsumerForm(request.POST or None)
if form.is_valid():
# Generate random codes and save.
form.instance.user = request.user
form.instance.generate_random_codes()
return redirect('admin:piston_consumer_changelist')
return jingo.render(request, 'zadmin/oauth-consumer-create.html',
{'form':form})