mirror of
https://github.com/ansible/awx.git
synced 2026-05-21 07:47:44 -02:30
Updates to work with REST framework 2.3.x, update browseable API style to mimic UI.
This commit is contained in:
14
ansibleworks/main/renderers.py
Normal file
14
ansibleworks/main/renderers.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import rest_framework.renderers
|
||||
|
||||
class BrowsableAPIRenderer(rest_framework.renderers.BrowsableAPIRenderer):
|
||||
'''
|
||||
Customizations to the default browsable API renderer.
|
||||
'''
|
||||
|
||||
def get_form(self, view, method, request):
|
||||
'''Never show auto-generated form (only raw form).'''
|
||||
obj = getattr(view, 'object', None)
|
||||
if not self.show_form_for_method(view, method, request, obj):
|
||||
return
|
||||
if method in ('DELETE', 'OPTIONS'):
|
||||
return True # Don't actually need to return a form
|
||||
@@ -1,8 +1,7 @@
|
||||
# Copyright (c) 2013 AnsibleWorks, Inc.
|
||||
# All Rights Reserved.
|
||||
|
||||
from django.conf.urls import include, patterns, url as original_url
|
||||
import ansibleworks.main.views as views
|
||||
from django.conf.urls import include, patterns, url as original_url
|
||||
|
||||
def url(regex, view, kwargs=None, name=None, prefix=''):
|
||||
# Set default name from view name (if a string).
|
||||
@@ -135,3 +134,47 @@ urlpatterns = patterns('ansibleworks.main.views',
|
||||
url(r'^$', 'api_root_view'),
|
||||
url(r'^v1/', include(v1_urls)),
|
||||
)
|
||||
|
||||
# Monkeypatch get_view_name and get_view_description in Django REST Framework
|
||||
# 2.3.x to allow a custom view name or description to be defined on the view
|
||||
# class, instead of always using __name__ and __doc__. Used to be possible in
|
||||
# 2.2.x by defining get_name() and get_description() methods on a view.
|
||||
|
||||
try:
|
||||
import rest_framework.utils.formatting
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
original_get_view_name = rest_framework.utils.formatting.get_view_name
|
||||
def get_view_name(cls, suffix=None):
|
||||
name = ''
|
||||
# Support for get_name method on views compatible with 2.2.x.
|
||||
if hasattr(cls, 'get_name') and callable(cls.get_name):
|
||||
name = cls().get_name()
|
||||
elif hasattr(cls, 'view_name'):
|
||||
if callable(cls.view_name):
|
||||
name = cls.view_name()
|
||||
else:
|
||||
name = cls.view_name
|
||||
if name:
|
||||
return ('%s %s' % (name, suffix)) if suffix else name
|
||||
return original_get_view_name(cls, suffix=None)
|
||||
rest_framework.utils.formatting.get_view_name = get_view_name
|
||||
|
||||
original_get_view_description = rest_framework.utils.formatting.get_view_description
|
||||
def get_view_description(cls, html=False):
|
||||
# Support for get_description method on views compatible with 2.2.x.
|
||||
if hasattr(cls, 'get_description') and callable(cls.get_description):
|
||||
desc = cls().get_description(html=html)
|
||||
elif hasattr(cls, 'view_description'):
|
||||
if callable(cls.view_description):
|
||||
view_desc = cls.view_description()
|
||||
else:
|
||||
view_desc = cls.view_description
|
||||
cls = type(cls.__name__, (object,), {'__doc__': view_desc})
|
||||
desc = original_get_view_description(cls, html=html)
|
||||
if html:
|
||||
desc = '<div class="description">%s</div>' % desc
|
||||
return mark_safe(desc)
|
||||
rest_framework.utils.formatting.get_view_description = get_view_description
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@@ -49,18 +49,18 @@ def handle_500(request):
|
||||
|
||||
class ApiRootView(APIView):
|
||||
'''
|
||||
Ansible Commander REST API
|
||||
This resource is the root of the AnsibleWorks REST API and provides
|
||||
information about the available API versions.
|
||||
'''
|
||||
|
||||
def get_name(self):
|
||||
return 'REST API'
|
||||
view_name = 'REST API'
|
||||
|
||||
def get(self, request, format=None):
|
||||
''' list supported API versions '''
|
||||
|
||||
current = reverse('main:api_v1_root_view', args=[])
|
||||
data = dict(
|
||||
description = 'Ansible Commander REST API',
|
||||
description = 'AnsibleWorks REST API',
|
||||
current_version = current,
|
||||
available_versions = dict(
|
||||
v1 = current
|
||||
@@ -71,10 +71,11 @@ class ApiRootView(APIView):
|
||||
class ApiV1RootView(APIView):
|
||||
'''
|
||||
Version 1 of the REST API.
|
||||
|
||||
Subject to change until the final 1.2 release.
|
||||
'''
|
||||
|
||||
def get_name(self):
|
||||
return 'Version 1'
|
||||
view_name = 'Version 1'
|
||||
|
||||
def get(self, request, format=None):
|
||||
''' list top level resources '''
|
||||
@@ -431,8 +432,7 @@ class UsersMeList(BaseList):
|
||||
permission_classes = (CustomRbac,)
|
||||
filter_fields = ('username',)
|
||||
|
||||
def get_name(self):
|
||||
return 'Me!'
|
||||
view_name = 'Me!'
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
raise PermissionDenied()
|
||||
@@ -973,8 +973,7 @@ class BaseJobHostSummaryList(generics.ListAPIView):
|
||||
parent_model = None # Subclasses must define this attribute.
|
||||
relationship = 'job_host_summaries'
|
||||
|
||||
def get_name(self):
|
||||
return 'Job Host Summary List'
|
||||
view_name = 'Job Host Summary List'
|
||||
|
||||
def get_queryset(self):
|
||||
# FIXME: Verify read permission on the parent object and job.
|
||||
|
||||
Reference in New Issue
Block a user