better flexibility with base manager classes

parent d33180d7
......@@ -4,14 +4,25 @@ Build dynamic API based on the provided SQLAlchemy model
"""
from viewsets import AlchemyModelViewSet
from rest_framework_nested import routers
from managers import AlchemyModelManager
class APIModelBuilder(object):
def __init__(self, models, SessionMixin, *args, **kwargs):
def __init__(self,
models,
base_managers,
base_viewsets=None,
*args, **kwargs):
self.models = models
self.SessionMixin = SessionMixin
if not isinstance(base_managers, (tuple, list)):
base_managers = [base_managers]
if not isinstance(base_managers, tuple):
base_managers = tuple(base_managers)
if base_viewsets is None:
base_viewsets = (AlchemyModelViewSet,)
self.base_managers = base_managers
self.base_viewsets = base_viewsets
@property
def urls(self):
......@@ -21,14 +32,14 @@ class APIModelBuilder(object):
manager = type(
str('{}Manager'.format(model.__name__)),
(self.SessionMixin, AlchemyModelManager,),
self.base_managers,
{
'model_class': model,
}
)
viewset = type(
str('{}ModelViewSet'.format(model.__name__)),
(AlchemyModelViewSet,),
self.base_viewsets,
{
'manager_class': manager,
}
......
......@@ -11,11 +11,16 @@ def module_walk(root_module, include_self=True):
if isinstance(root_module, six.string_types):
root_module = importlib.import_module(root_module)
root_path = os.path.abspath(os.path.dirname(root_module.__file__))
if include_self:
yield root_module
root_path = os.path.abspath(root_module.__file__)
if '__init__' not in os.path.basename(root_path):
return
root_path = os.path.dirname(root_path)
for path, dirs, files in os.walk(root_path):
path = os.path.abspath(path)
......
......@@ -123,6 +123,8 @@ INSTALLED_APPS = (
# 'django.contrib.admindocs',
)
SA_MODEL_LOADER = ('models',)
# DJANGO REST FRAMEWORK SETTINGS
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
......
......@@ -4,6 +4,8 @@ Url registrations
from django.conf.urls import patterns, include, url
from rest_framework_nested import routers
from djangorest_alchemy.apibuilder import APIModelBuilder
from djangorest_alchemy.managers import AlchemyModelManager
from viewsets import CarViewSet
from viewsets import PartViewSet
......@@ -24,7 +26,8 @@ child_router.register("parts", PartViewSet,
# Demonstrate dynamic API builder featire
from djangorest_alchemy.model_cache import model_cache
builder = APIModelBuilder(model_cache.models, SessionMixin)
builder = APIModelBuilder(model_cache.models,
(SessionMixin, AlchemyModelManager))
urlpatterns = patterns('',
url(r'^', include(viewset_router.urls)),
url(r'^', include(child_router.urls)),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment