Skip to content

Commit 00e4818

Browse files
committed
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
1 parent 3cb39d4 commit 00e4818

4 files changed

Lines changed: 16 additions & 20 deletions

File tree

ietf/api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.conf import settings
66
from django.http import HttpResponse
7+
from django.core.urlresolvers import reverse
78

89
from tastypie.api import Api
910
from tastypie.serializers import Serializer
@@ -30,11 +31,11 @@
3031
def top_level(request):
3132
available_resources = {}
3233

33-
apitop = settings.RESTAPI_V1_URL_TOP
34+
apitop = reverse('ietf.api.top_level')
3435

3536
for name in sorted([ name for name, api in _api_list if len(api._registry) > 0 ]):
3637
available_resources[name] = {
37-
'list_endpoint': '/%s/%s/' % (apitop, name),
38+
'list_endpoint': '%s/%s/' % (apitop, name),
3839
}
3940

4041
serializer = Serializer()

ietf/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ def skip_unreadable_post(record):
459459

460460
POSTCONFIRM_PATH = "/a/postconfirm/test-wrapper"
461461

462-
RESTAPI_V1_URL_TOP = "api/v1" # no leading or trailing slash
463-
464462
# Put the production SECRET_KEY in settings_local.py, and also any other
465463
# sensitive or site-specific changes. DO NOT commit settings_local.py to svn.
466464
from settings_local import * # pyflakes:ignore

ietf/urls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@
6565
)
6666

6767
# Endpoints for Tastypie's REST API
68-
apitop = settings.RESTAPI_V1_URL_TOP
6968
urlpatterns += patterns('',
70-
(r'^%s/?$'%apitop, api.top_level),
69+
(r'^api/v1/?$', api.top_level),
7170
)
7271
for n,a in api._api_list:
7372
urlpatterns += patterns('',
74-
(r'^%s/'%apitop, include(a.urls)),
73+
(r'^api/v1/', include(a.urls)),
7574
)
7675

7776
if settings.SERVER_MODE in ('development', 'test'):

ietf/utils/tests_restapi.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import debug
44
debug.debug = True
55

6-
from django.conf import settings
6+
from django.core.urlresolvers import reverse
77

88
from tastypie.test import ResourceTestCase
9-
from ietf.utils.test_data import make_test_data
10-
119

12-
10+
from ietf.utils.test_data import make_test_data
1311

1412
class RestApi(ResourceTestCase):
1513
def list_recursively(self, resource, format):
@@ -34,12 +32,12 @@ def list_recursively(self, resource, format):
3432

3533
def test_json_api_explore(self):
3634
make_test_data()
37-
apitop = settings.RESTAPI_V1_URL_TOP
38-
self.list_recursively('/%s/'%apitop, format='json')
35+
apitop = reverse('ietf.api.top_level')
36+
self.list_recursively('%s/'%apitop, format='json')
3937

4038
def test_xml_api_explore(self):
41-
apitop = settings.RESTAPI_V1_URL_TOP
42-
self.assertValidXMLResponse(self.api_client.get('/%s/doc/'%apitop, format='xml'))
39+
apitop = reverse('ietf.api.top_level')
40+
self.assertValidXMLResponse(self.api_client.get('%s/doc/'%apitop, format='xml'))
4341

4442
def test_json_doc_document(self):
4543
"""
@@ -48,8 +46,8 @@ def test_json_doc_document(self):
4846
than 100 documents in the test-data (the current count is 10)
4947
"""
5048
make_test_data()
51-
apitop = settings.RESTAPI_V1_URL_TOP
52-
r = self.api_client.get('/%s/doc/document/'%apitop, format='json', limit=100)
49+
apitop = reverse('ietf.api.top_level')
50+
r = self.api_client.get('%s/doc/document/'%apitop, format='json', limit=100)
5351
doclist = self.deserialize(r)["objects"]
5452
docs = dict( (doc["name"], doc) for doc in doclist )
5553
for name in (
@@ -68,12 +66,12 @@ def test_json_doc_relationships(self):
6866
of relationships give URLs which are handled without raising exceptions.
6967
"""
7068
make_test_data()
71-
apitop = settings.RESTAPI_V1_URL_TOP
72-
r = self.api_client.get('/%s/doc/document/'%apitop, format='json')
69+
apitop = reverse('ietf.api.top_level')
70+
r = self.api_client.get('%s/doc/document/'%apitop, format='json')
7371
doclist = self.deserialize(r)["objects"]
7472
for doc in doclist:
7573
for key in doc:
7674
value = doc[key]
77-
if isinstance(value, basestring) and value.startswith('/%s/'%apitop):
75+
if isinstance(value, basestring) and value.startswith('%s/'%apitop):
7876
self.api_client.get(value, format='json')
7977

0 commit comments

Comments
 (0)