Automagically determine base_name in router class

This commit is contained in:
Tom Christie 2013-05-02 12:07:37 +01:00
parent e4067bfb75
commit 387250bee4

View File

@ -42,10 +42,22 @@ class BaseRouter(object):
def __init__(self): def __init__(self):
self.registry = [] self.registry = []
def register(self, prefix, viewset, name): def register(self, prefix, viewset, base_name=None):
self.registry.append((prefix, viewset, name)) if base_name is None:
base_name = self.get_default_base_name(viewset)
self.registry.append((prefix, viewset, base_name))
def get_default_base_name(self, viewset):
"""
If `base_name` is not specified, attempt to automatically determine
it from the viewset.
"""
raise NotImplemented('get_default_base_name must be overridden')
def get_urls(self): def get_urls(self):
"""
Return a list of URL patterns, given the registered viewsets.
"""
raise NotImplemented('get_urls must be overridden') raise NotImplemented('get_urls must be overridden')
@property @property
@ -91,6 +103,22 @@ class SimpleRouter(BaseRouter):
), ),
] ]
def get_default_base_name(self, viewset):
"""
If `base_name` is not specified, attempt to automatically determine
it from the viewset.
"""
model_cls = getattr(viewset, 'model', None)
queryset = getattr(viewset, 'queryset', None)
if model_cls is None and queryset is not None:
model_cls = queryset.model
assert model_cls, '`name` not argument not specified, and could ' \
'not automatically determine the name from the viewset, as ' \
'it does not have a `.model` or `.queryset` attribute.'
return model_cls._meta.object_name.lower()
def get_routes(self, viewset): def get_routes(self, viewset):
""" """
Augment `self.routes` with any dynamically generated routes. Augment `self.routes` with any dynamically generated routes.