Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Web - Open
drf-sqla
Commits
15d53271
Commit
15d53271
authored
Sep 16, 2014
by
Ashish
Browse files
model cache
parent
3216e67d
Changes
4
Hide whitespace changes
Inline
Side-by-side
djangorest_alchemy/apibuilder.py
View file @
15d53271
...
...
@@ -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
djangorest_alchemy/model_cache.py
0 → 100644
View file @
15d53271
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
()
]))
djangorest_alchemy/tests/test_apibuilder.py
View file @
15d53271
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
)
examples/urls.py
View file @
15d53271
...
...
@@ -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
)),
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment