Updates to work with REST framework 2.3.x, update browseable API style to mimic UI.

This commit is contained in:
Chris Church
2013-06-14 04:13:34 -04:00
parent e5737cae46
commit c526f58098
7 changed files with 152 additions and 42 deletions

View File

@@ -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