blob: a60b62d616b68a79cc47b0f92588d0b5cf60223f [file] [log] [blame]
Scott Bakerfb9544a2016-03-25 10:55:03 -07001from django.views.generic import View
Scott Bakerd9d55f22016-03-25 13:33:11 -07002from django.conf.urls import patterns, url, include
Scott Bakerfb9544a2016-03-25 10:55:03 -07003from rest_framework.routers import DefaultRouter
4import os, sys
5import inspect
6import importlib
7
Scott Bakerd9d55f22016-03-25 13:33:11 -07008urlpatterns = []
9
10def import_module_from_filename(dirname, fn):
Scott Bakerfb9544a2016-03-25 10:55:03 -070011 sys_path_save = sys.path
12 try:
13 # __import__() and importlib.import_module() both import modules from
14 # sys.path. So we make sure that the path where we can find the views is
15 # the first thing in sys.path.
Scott Bakerd9d55f22016-03-25 13:33:11 -070016 sys.path = [dirname] + sys.path
Scott Bakerfb9544a2016-03-25 10:55:03 -070017
18 module = __import__(fn[:-3])
19 finally:
20 sys.path = sys_path_save
21
22 return module
23
Scott Bakerd9d55f22016-03-25 13:33:11 -070024def import_api_methods(dirname=None, api_path="api"):
Scott Bakerfb9544a2016-03-25 10:55:03 -070025 subdirs=[]
Scott Bakerd9d55f22016-03-25 13:33:11 -070026 urlpatterns=[]
Scott Bakerfb9544a2016-03-25 10:55:03 -070027
Scott Bakerd9d55f22016-03-25 13:33:11 -070028 if not dirname:
29 dirname = os.path.dirname(os.path.abspath(__file__))
Scott Bakerfb9544a2016-03-25 10:55:03 -070030
Scott Bakerd9d55f22016-03-25 13:33:11 -070031 view_urls = []
32 for fn in os.listdir(dirname):
33 pathname = os.path.join(dirname,fn)
Scott Bakerfb9544a2016-03-25 10:55:03 -070034 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
Scott Bakerd9d55f22016-03-25 13:33:11 -070035 module = import_module_from_filename(dirname, fn)
Scott Bakerfb9544a2016-03-25 10:55:03 -070036 for classname in dir(module):
37 c = getattr(module, classname, None)
38
39 if inspect.isclass(c) and issubclass(c, View) and (classname not in globals()):
40 globals()[classname] = c
41
42 method_kind = getattr(c, "method_kind", None)
Scott Bakerd9d55f22016-03-25 13:33:11 -070043 method_name = getattr(c, "method_name", None)
Scott Bakerfb9544a2016-03-25 10:55:03 -070044 if method_kind and method_name:
Scott Bakerd9d55f22016-03-25 13:33:11 -070045 method_name = os.path.join(api_path, method_name)
Scott Bakerfb9544a2016-03-25 10:55:03 -070046 view_urls.append( (method_kind, method_name, classname, c) )
47
48 elif os.path.isdir(pathname):
Scott Bakerd9d55f22016-03-25 13:33:11 -070049 urlpatterns.extend(import_api_methods(pathname, os.path.join(api_path, fn)))
Scott Bakerfb9544a2016-03-25 10:55:03 -070050
51 for view_url in view_urls:
52 if view_url[0] == "list":
53 urlpatterns.append(url(r'^' + view_url[1] + '/$', view_url[3].as_view(), name=view_url[1]+'list'))
54 elif view_url[0] == "detail":
55 urlpatterns.append(url(r'^' + view_url[1] + '/(?P<pk>[a-zA-Z0-9\-]+)/$', view_url[3].as_view(), name=view_url[1]+'detail'))
56 elif view_url[0] == "viewset":
57 viewset = view_url[3]
Scott Bakerd9d55f22016-03-25 13:33:11 -070058 urlpatterns.extend(viewset.get_urlpatterns(api_path=api_path+"/"))
Scott Bakerfb9544a2016-03-25 10:55:03 -070059
60 return urlpatterns
Scott Bakerd9d55f22016-03-25 13:33:11 -070061
62urlpatterns = import_api_methods()
63