Skip to content

Commit e52333b

Browse files
committed
Allows PKs with dashes and alphanumeric digits
This way they can be hex numbers, UUIDs, slugs and so on
1 parent a4994eb commit e52333b

File tree

7 files changed

+55
-50
lines changed

7 files changed

+55
-50
lines changed

Diff for: restless/dj.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ def urls(cls, name_prefix=None):
8686
"""
8787
return patterns('',
8888
url(r'^$', cls.as_list(), name=cls.build_url_name('list', name_prefix)),
89-
url(r'^(?P<pk>\d+)/$', cls.as_detail(), name=cls.build_url_name('detail', name_prefix)),
89+
url(r'^(?P<pk>[\w-]+)/$', cls.as_detail(), name=cls.build_url_name('detail', name_prefix)),
9090
)

Diff for: restless/it.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def setup_urls(cls, rule_prefix):
3030
:returns: ``None``
3131
"""
3232
list_url = "%s" % itty.add_slash(rule_prefix)
33-
detail_url = "%s" % itty.add_slash(rule_prefix + "/(?P<pk>\d+)")
33+
detail_url = "%s" % itty.add_slash(rule_prefix + "/(?P<pk>[\w-]+)")
3434

3535
list_re = re.compile("^%s$" % list_url)
3636
detail_re = re.compile("^%s$" % detail_url)

Diff for: tests/test_dj.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ def fake_init(self):
3838
# Just for testing.
3939
self.__class__.fake_db = [
4040
FakeModel(
41-
id=2,
41+
id='dead-beef',
4242
title='First post',
4343
username='daniel',
4444
content='Hello world!'),
4545
FakeModel(
46-
id=4,
46+
id='de-faced',
4747
title='Another',
4848
username='daniel',
4949
content='Stuff here.'),
50-
FakeModel(id=5, title='Last', username='daniel', content="G'bye!"),
50+
FakeModel(
51+
id='bad-f00d',
52+
title='Last',
53+
username='daniel',
54+
content="G'bye!"),
5155
]
5256

5357
def is_authenticated(self):
@@ -144,19 +148,19 @@ def test_as_list(self):
144148
{
145149
'author': 'daniel',
146150
'body': 'Hello world!',
147-
'id': 2,
151+
'id': 'dead-beef',
148152
'title': 'First post'
149153
},
150154
{
151155
'author': 'daniel',
152156
'body': 'Stuff here.',
153-
'id': 4,
157+
'id': 'de-faced',
154158
'title': 'Another'
155159
},
156160
{
157161
'author': 'daniel',
158162
'body': "G'bye!",
159-
'id': 5,
163+
'id': 'bad-f00d',
160164
'title': 'Last'
161165
}
162166
]
@@ -166,13 +170,13 @@ def test_as_detail(self):
166170
detail_endpoint = DjTestResource.as_detail()
167171
req = FakeHttpRequest('GET')
168172

169-
resp = detail_endpoint(req, 4)
173+
resp = detail_endpoint(req, 'de-faced')
170174
self.assertEqual(resp['Content-Type'], 'application/json')
171175
self.assertEqual(resp.status_code, 200)
172176
self.assertEqual(json.loads(resp.content.decode('utf-8')), {
173177
'author': 'daniel',
174178
'body': 'Stuff here.',
175-
'id': 4,
179+
'id': 'de-faced',
176180
'title': 'Another'
177181
})
178182

Diff for: tests/test_fl.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class FlTestResource(FlaskResource):
1515
def fake_init(self):
1616
# Just for testing.
1717
self.__class__.fake_db = [
18-
{"id": 2, "title": 'First post'},
19-
{"id": 4, "title": 'Another'},
20-
{"id": 5, "title": 'Last'},
18+
{"id": 'dead-beef', "title": 'First post'},
19+
{"id": 'de-faced', "title": 'Another'},
20+
{"id": 'bad-f00d', "title": 'Last'},
2121
]
2222

2323
def list(self):
@@ -54,15 +54,15 @@ def test_as_list(self):
5454
self.assertEqual(json.loads(resp.data.decode('utf-8')), {
5555
'objects': [
5656
{
57-
'id': 2,
57+
'id': 'dead-beef',
5858
'title': 'First post'
5959
},
6060
{
61-
'id': 4,
61+
'id': 'de-faced',
6262
'title': 'Another'
6363
},
6464
{
65-
'id': 5,
65+
'id': 'bad-f00d',
6666
'title': 'Last'
6767
}
6868
]
@@ -73,11 +73,11 @@ def test_as_detail(self):
7373
flask.request = FakeHttpRequest('GET')
7474

7575
with self.app.test_request_context('/whatever/', method='GET'):
76-
resp = detail_endpoint(4)
76+
resp = detail_endpoint('de-faced')
7777
self.assertEqual(resp.headers['Content-Type'], 'application/json')
7878
self.assertEqual(resp.status_code, 200)
7979
self.assertEqual(json.loads(resp.data.decode('utf-8')), {
80-
'id': 4,
80+
'id': 'de-faced',
8181
'title': 'Another'
8282
})
8383

Diff for: tests/test_it.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class ItTestResource(IttyResource):
1818
def fake_init(self):
1919
# Just for testing.
2020
self.__class__.fake_db = [
21-
{"id": 2, "title": 'First post'},
22-
{"id": 4, "title": 'Another'},
23-
{"id": 5, "title": 'Last'},
21+
{"id": 'dead-beef', "title": 'First post'},
22+
{"id": 'de-faced', "title": 'Another'},
23+
{"id": 'bad-f00d', "title": 'Last'},
2424
]
2525

2626
def list(self):
@@ -54,15 +54,15 @@ def test_as_list(self):
5454
self.assertEqual(json.loads(resp.output), {
5555
'objects': [
5656
{
57-
'id': 2,
57+
'id': 'dead-beef',
5858
'title': 'First post'
5959
},
6060
{
61-
'id': 4,
61+
'id': 'de-faced',
6262
'title': 'Another'
6363
},
6464
{
65-
'id': 5,
65+
'id': 'bad-f00d',
6666
'title': 'Last'
6767
}
6868
]
@@ -72,11 +72,11 @@ def test_as_detail(self):
7272
detail_endpoint = ItTestResource.as_detail()
7373
request = FakeHttpRequest('GET')
7474

75-
resp = detail_endpoint(request, 4)
75+
resp = detail_endpoint(request, 'de-faced')
7676
self.assertEqual(resp.content_type, 'application/json')
7777
self.assertEqual(resp.status, 200)
7878
self.assertEqual(json.loads(resp.output), {
79-
'id': 4,
79+
'id': 'de-faced',
8080
'title': 'Another'
8181
})
8282

@@ -105,4 +105,5 @@ def test_setup_urls(self):
105105
self.assertEqual(len(itty.REQUEST_MAPPINGS['PUT']), 2)
106106
self.assertEqual(len(itty.REQUEST_MAPPINGS['DELETE']), 2)
107107
self.assertEqual(itty.REQUEST_MAPPINGS['GET'][0][1], '/test/')
108-
self.assertEqual(itty.REQUEST_MAPPINGS['GET'][1][1], '/test/(?P<pk>\\d+)/')
108+
self.assertEqual(itty.REQUEST_MAPPINGS['GET'][1][1],
109+
'/test/(?P<pk>[\\w-]+)/')

Diff for: tests/test_pyr.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class PyrTestResource(PyramidResource):
1414
def fake_init(self):
1515
# Just for testing.
1616
self.__class__.fake_db = [
17-
{"id": 2, "title": 'First post'},
18-
{"id": 4, "title": 'Another'},
19-
{"id": 5, "title": 'Last'},
17+
{"id": "dead-beef", "title": 'First post'},
18+
{"id": "de-faced", "title": 'Another'},
19+
{"id": "bad-f00d", "title": 'Last'},
2020
]
2121

2222
def list(self):
@@ -51,15 +51,15 @@ def test_as_list(self):
5151
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
5252
'objects': [
5353
{
54-
'id': 2,
54+
'id': 'dead-beef',
5555
'title': 'First post'
5656
},
5757
{
58-
'id': 4,
58+
'id': 'de-faced',
5959
'title': 'Another'
6060
},
6161
{
62-
'id': 5,
62+
'id': 'bad-f00d',
6363
'title': 'Last'
6464
}
6565
]
@@ -70,13 +70,13 @@ def test_as_detail(self):
7070
req = testing.DummyRequest()
7171

7272
req = FakeHttpRequest('GET')
73-
req.matchdict = {'name': 4}
73+
req.matchdict = {'name': 'de-faced'}
7474

7575
resp = detail_endpoint(req)
7676
self.assertEqual(resp.content_type, 'application/json')
7777
self.assertEqual(resp.status_code, 200)
7878
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
79-
'id': 4,
79+
'id': 'de-faced',
8080
'title': 'Another'
8181
})
8282

Diff for: tests/test_tnd.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class TndBaseTestResource(TornadoResource):
1515
def __init__(self):
1616
# Just for testing.
1717
self.__class__.fake_db = [
18-
{"id": 2, "title": 'First post'},
19-
{"id": 4, "title": 'Another'},
20-
{"id": 5, "title": 'Last'},
18+
{"id": "dead-beef", "title": 'First post'},
19+
{"id": "de-faced", "title": 'Another'},
20+
{"id": "bad-f00d", "title": 'Last'},
2121
]
2222

2323

@@ -30,7 +30,7 @@ def list(self):
3030

3131
def detail(self, pk):
3232
for item in self.fake_db:
33-
if item['id'] == int(pk):
33+
if item['id'] == pk:
3434
return item
3535
return None
3636

@@ -49,7 +49,7 @@ def list(self):
4949
@gen.coroutine
5050
def detail(self, pk):
5151
for item in self.fake_db:
52-
if item['id'] == int(pk):
52+
if item['id'] == pk:
5353
raise gen.Return(item)
5454
raise gen.Return(None)
5555

@@ -88,30 +88,30 @@ def test_as_list(self):
8888
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
8989
'objects': [
9090
{
91-
'id': 2,
91+
'id': 'dead-beef',
9292
'title': 'First post'
9393
},
9494
{
95-
'id': 4,
95+
'id': 'de-faced',
9696
'title': 'Another'
9797
},
9898
{
99-
'id': 5,
99+
'id': 'bad-f00d',
100100
'title': 'Last'
101101
}
102102
]
103103
})
104104

105105
def test_as_detail(self):
106106
resp = self.fetch(
107-
'/fake/4',
107+
'/fake/de-faced',
108108
method='GET',
109109
follow_redirects=False
110110
)
111111
self.assertEqual(resp.headers['Content-Type'], 'application/json; charset=UTF-8')
112112
self.assertEqual(resp.code, 200)
113113
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
114-
'id': 4,
114+
'id': 'de-faced',
115115
'title': 'Another'
116116
})
117117

@@ -237,30 +237,30 @@ def test_as_list(self):
237237
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
238238
'objects': [
239239
{
240-
'id': 2,
240+
'id': 'dead-beef',
241241
'title': 'First post'
242242
},
243243
{
244-
'id': 4,
244+
'id': 'de-faced',
245245
'title': 'Another'
246246
},
247247
{
248-
'id': 5,
248+
'id': 'bad-f00d',
249249
'title': 'Last'
250250
}
251251
]
252252
})
253253

254254
def test_as_detail(self):
255255
resp = self.fetch(
256-
'/fake_async/4',
256+
'/fake_async/de-faced',
257257
method='GET',
258258
follow_redirects=False
259259
)
260260
self.assertEqual(resp.headers['Content-Type'], 'application/json; charset=UTF-8')
261261
self.assertEqual(resp.code, 200)
262262
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
263-
'id': 4,
263+
'id': 'de-faced',
264264
'title': 'Another'
265265
})
266266

0 commit comments

Comments
 (0)