Alternative DRY definition of the REST api top URL; this time in ietf/urls.py. This in itself is nicer, but I'm not so sure about the use of reverse() everywhere else, instead of referring to settings.

- Legacy-Id: 8752
This commit is contained in:
Henrik Levkowetz 2014-12-17 21:26:32 +00:00
parent 3cb39d462e
commit 00e4818ceb
4 changed files with 16 additions and 20 deletions

View file

@ -4,6 +4,7 @@ import datetime
from django.conf import settings from django.conf import settings
from django.http import HttpResponse from django.http import HttpResponse
from django.core.urlresolvers import reverse
from tastypie.api import Api from tastypie.api import Api
from tastypie.serializers import Serializer from tastypie.serializers import Serializer
@ -30,11 +31,11 @@ for _app in settings.INSTALLED_APPS:
def top_level(request): def top_level(request):
available_resources = {} available_resources = {}
apitop = settings.RESTAPI_V1_URL_TOP apitop = reverse('ietf.api.top_level')
for name in sorted([ name for name, api in _api_list if len(api._registry) > 0 ]): for name in sorted([ name for name, api in _api_list if len(api._registry) > 0 ]):
available_resources[name] = { available_resources[name] = {
'list_endpoint': '/%s/%s/' % (apitop, name), 'list_endpoint': '%s/%s/' % (apitop, name),
} }
serializer = Serializer() serializer = Serializer()

View file

@ -459,8 +459,6 @@ GROUP_VIRTUAL_DOMAIN = "virtual.ietf.org"
POSTCONFIRM_PATH = "/a/postconfirm/test-wrapper" POSTCONFIRM_PATH = "/a/postconfirm/test-wrapper"
RESTAPI_V1_URL_TOP = "api/v1" # no leading or trailing slash
# Put the production SECRET_KEY in settings_local.py, and also any other # Put the production SECRET_KEY in settings_local.py, and also any other
# sensitive or site-specific changes. DO NOT commit settings_local.py to svn. # sensitive or site-specific changes. DO NOT commit settings_local.py to svn.
from settings_local import * # pyflakes:ignore from settings_local import * # pyflakes:ignore

View file

@ -65,13 +65,12 @@ urlpatterns = patterns('',
) )
# Endpoints for Tastypie's REST API # Endpoints for Tastypie's REST API
apitop = settings.RESTAPI_V1_URL_TOP
urlpatterns += patterns('', urlpatterns += patterns('',
(r'^%s/?$'%apitop, api.top_level), (r'^api/v1/?$', api.top_level),
) )
for n,a in api._api_list: for n,a in api._api_list:
urlpatterns += patterns('', urlpatterns += patterns('',
(r'^%s/'%apitop, include(a.urls)), (r'^api/v1/', include(a.urls)),
) )
if settings.SERVER_MODE in ('development', 'test'): if settings.SERVER_MODE in ('development', 'test'):

View file

@ -3,14 +3,12 @@ from __future__ import print_function
import debug import debug
debug.debug = True debug.debug = True
from django.conf import settings from django.core.urlresolvers import reverse
from tastypie.test import ResourceTestCase from tastypie.test import ResourceTestCase
from ietf.utils.test_data import make_test_data from ietf.utils.test_data import make_test_data
class RestApi(ResourceTestCase): class RestApi(ResourceTestCase):
def list_recursively(self, resource, format): def list_recursively(self, resource, format):
""" """
@ -34,12 +32,12 @@ class RestApi(ResourceTestCase):
def test_json_api_explore(self): def test_json_api_explore(self):
make_test_data() make_test_data()
apitop = settings.RESTAPI_V1_URL_TOP apitop = reverse('ietf.api.top_level')
self.list_recursively('/%s/'%apitop, format='json') self.list_recursively('%s/'%apitop, format='json')
def test_xml_api_explore(self): def test_xml_api_explore(self):
apitop = settings.RESTAPI_V1_URL_TOP apitop = reverse('ietf.api.top_level')
self.assertValidXMLResponse(self.api_client.get('/%s/doc/'%apitop, format='xml')) self.assertValidXMLResponse(self.api_client.get('%s/doc/'%apitop, format='xml'))
def test_json_doc_document(self): def test_json_doc_document(self):
""" """
@ -48,8 +46,8 @@ class RestApi(ResourceTestCase):
than 100 documents in the test-data (the current count is 10) than 100 documents in the test-data (the current count is 10)
""" """
make_test_data() make_test_data()
apitop = settings.RESTAPI_V1_URL_TOP apitop = reverse('ietf.api.top_level')
r = self.api_client.get('/%s/doc/document/'%apitop, format='json', limit=100) r = self.api_client.get('%s/doc/document/'%apitop, format='json', limit=100)
doclist = self.deserialize(r)["objects"] doclist = self.deserialize(r)["objects"]
docs = dict( (doc["name"], doc) for doc in doclist ) docs = dict( (doc["name"], doc) for doc in doclist )
for name in ( for name in (
@ -68,12 +66,12 @@ class RestApi(ResourceTestCase):
of relationships give URLs which are handled without raising exceptions. of relationships give URLs which are handled without raising exceptions.
""" """
make_test_data() make_test_data()
apitop = settings.RESTAPI_V1_URL_TOP apitop = reverse('ietf.api.top_level')
r = self.api_client.get('/%s/doc/document/'%apitop, format='json') r = self.api_client.get('%s/doc/document/'%apitop, format='json')
doclist = self.deserialize(r)["objects"] doclist = self.deserialize(r)["objects"]
for doc in doclist: for doc in doclist:
for key in doc: for key in doc:
value = doc[key] value = doc[key]
if isinstance(value, basestring) and value.startswith('/%s/'%apitop): if isinstance(value, basestring) and value.startswith('%s/'%apitop):
self.api_client.get(value, format='json') self.api_client.get(value, format='json')