Commit 15d53271 authored by Ashish's avatar Ashish

model cache

parent 3216e67d
......@@ -34,7 +34,7 @@ class APIModelBuilder(object):
}
)
router.register(model.__name__.lower() + 's', viewset,
router.register('data-api/' + model.__name__.lower() + 's', viewset,
base_name=model.__name__)
return router.urls
import importlib
import inspect
import itertools
import os
class ModelCache(object):
"""Store for all the models in the project
Based on Django's AppCache, this collects all the
models in the application for introspection.
Future:
There will be a settings.py file which will allow
customization
"""
__shared_state = dict(
modules={},
models=[]
)
def __init__(self):
self.__dict__ = self.__shared_state
def get_module_names_and_path(self, directory_path="."):
all_paths = []
for root, dirs, files in os.walk(directory_path):
all_paths.extend(
[(file[:-3], os.path.join(root, file))
for file in files if
file.endswith(".py") and file.startswith("models")]
)
return all_paths
def get_models_from_modules(self, module_name):
module = importlib.import_module(module_name)
models = [
obj for name, obj in vars(module).items()
if (inspect.isclass(obj)
and hasattr(obj, '__tablename__'))
]
return models
model_cache = ModelCache()
model_cache.modules = {
module[0]: module[1]
for module in model_cache.get_module_names_and_path()
}
model_cache.models = list(itertools.chain(*[
model_cache.get_models_from_modules(module)
for module in model_cache.modules.keys()
]))
import unittest
from djangorest_alchemy.apibuilder import APIModelBuilder
import mock
class TestAPIBuilder(unittest.TestCase):
......@@ -16,8 +17,8 @@ class TestAPIBuilder(unittest.TestCase):
pass
class SessionMixin(object):
pass
def __init__(self):
self.session = mock.Mock()
builder = APIModelBuilder([Model, Model2], SessionMixin)
self.assertIsNotNone(builder.urls)
......@@ -8,22 +8,25 @@ from djangorest_alchemy.apibuilder import APIModelBuilder
from viewsets import CarViewSet
from viewsets import PartViewSet
from models import Car, Part, SessionMixin
from models import SessionMixin
#viewset_router = routers.SimpleRouter()
#viewset_router.register(r'api/cars_foo', CarViewSet,
# base_name='car')
viewset_router = routers.SimpleRouter()
viewset_router.register(r'api/cars', CarViewSet,
base_name='car')
# Register the child model
#child_router = routers.NestedSimpleRouter(viewset_router, r'api/cars_foo',
# lookup='cars')
#child_router.register("parts", PartViewSet,
# base_name='part')
child_router = routers.NestedSimpleRouter(viewset_router, r'api/cars',
lookup='cars')
child_router.register("parts", PartViewSet,
base_name='part')
builder = APIModelBuilder([Car, Part], SessionMixin)
# Demonstrate dynamic API builder featire
from djangorest_alchemy.model_cache import model_cache
builder = APIModelBuilder(model_cache.models, SessionMixin)
urlpatterns = patterns('',
#url(r'^', include(viewset_router.urls)),
#url(r'^', include(child_router.urls)),
url(r'^', include(viewset_router.urls)),
url(r'^', include(child_router.urls)),
url(r'^', include(builder.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