aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/routers.py
diff options
context:
space:
mode:
authorTom Christie2013-04-04 20:35:40 +0100
committerTom Christie2013-04-04 20:35:40 +0100
commitfb41d2ac8f495ae0728e3f38c6a21306f0507316 (patch)
tree8dc1c7acb4778b900eaef144bd2e1857216d72a1 /rest_framework/routers.py
parentc785628300d2b7cce63862a18915c537f8a3ab24 (diff)
downloaddjango-rest-framework-fb41d2ac8f495ae0728e3f38c6a21306f0507316.tar.bz2
Add support for action and link routing
Diffstat (limited to 'rest_framework/routers.py')
-rw-r--r--rest_framework/routers.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/rest_framework/routers.py b/rest_framework/routers.py
index 63eae5d7..d1e96156 100644
--- a/rest_framework/routers.py
+++ b/rest_framework/routers.py
@@ -24,10 +24,12 @@ class DefaultRouter(BaseRouter):
(r'$', {'get': 'list', 'post': 'create'}, '%s-list'),
(r'(?P<pk>[^/]+)/$', {'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}, '%s-detail'),
]
+ extra_routes = (r'(?P<pk>[^/]+)/%s/$', '%s-%s')
def get_urlpatterns(self):
ret = []
for prefix, viewset, base_name in self.registry:
+ # Bind standard routes
for suffix, action_mapping, name_format in self.route_list:
# Only actions which actually exist on the viewset will be bound
@@ -36,8 +38,26 @@ class DefaultRouter(BaseRouter):
if hasattr(viewset, action):
bound_actions[method] = action
+ # Build the url pattern
regex = prefix + suffix
view = viewset.as_view(bound_actions)
name = name_format % base_name
ret.append(url(regex, view, name=name))
+
+ # Bind any extra @action or @link routes
+ for attr in dir(viewset):
+ func = getattr(viewset, attr)
+ http_method = getattr(func, 'bind_to_method', None)
+ if not http_method:
+ continue
+
+ regex_format, name_format = self.extra_routes
+
+ # Build the url pattern
+ regex = regex_format % attr
+ view = viewset.as_view({http_method: attr}, **func.kwargs)
+ name = name_format % (base_name, attr)
+ ret.append(url(regex, view, name=name))
+
+ # Return a list of url patterns
return ret