blob: 1b5e3ca1c7909ca270e4dff952f0512d1f65a752 [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
Scott Baker2fa68782016-04-05 14:13:09 -07004from xosapi_helpers import XOSIndexViewSet
Scott Bakerfb9544a2016-03-25 10:55:03 -07005import os, sys
6import inspect
7import importlib
8
Scott Baker29087132016-03-29 11:14:38 -07009try:
10 from rest_framework.serializers import DictField
11except:
12 raise Exception("Failed check for django-rest-framework >= 3.3.3")
13
Scott Bakerd9d55f22016-03-25 13:33:11 -070014urlpatterns = []
15
16def import_module_from_filename(dirname, fn):
Scott Baker795de952016-03-28 13:20:15 -070017 print "importing", dirname, fn
Scott Bakerfb9544a2016-03-25 10:55:03 -070018 sys_path_save = sys.path
19 try:
20 # __import__() and importlib.import_module() both import modules from
21 # sys.path. So we make sure that the path where we can find the views is
22 # the first thing in sys.path.
Scott Bakerd9d55f22016-03-25 13:33:11 -070023 sys.path = [dirname] + sys.path
Scott Bakerfb9544a2016-03-25 10:55:03 -070024
25 module = __import__(fn[:-3])
26 finally:
27 sys.path = sys_path_save
28
29 return module
30
Scott Bakera2c76272016-03-30 10:58:18 -070031def import_module_by_dotted_name(name):
32 print "import", name
33 module = __import__(name)
34 for part in name.split(".")[1:]:
35 module = getattr(module, part)
36 return module
37
38def import_api_methods(dirname=None, api_path="api", api_module="api"):
Scott Baker2fa68782016-04-05 14:13:09 -070039 has_index_view = False
Scott Bakerfb9544a2016-03-25 10:55:03 -070040 subdirs=[]
Scott Bakerd9d55f22016-03-25 13:33:11 -070041 urlpatterns=[]
Scott Bakerfb9544a2016-03-25 10:55:03 -070042
Scott Bakerd9d55f22016-03-25 13:33:11 -070043 if not dirname:
44 dirname = os.path.dirname(os.path.abspath(__file__))
Scott Bakerfb9544a2016-03-25 10:55:03 -070045
Scott Bakerd9d55f22016-03-25 13:33:11 -070046 view_urls = []
47 for fn in os.listdir(dirname):
48 pathname = os.path.join(dirname,fn)
Scott Bakerfd38f132016-03-28 13:51:52 -070049 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py") and (fn!="import_methods.py"):
Scott Bakera2c76272016-03-30 10:58:18 -070050 #module = import_module_from_filename(dirname, fn)
51 module = import_module_by_dotted_name(api_module + "." + fn[:-3])
Scott Bakerfb9544a2016-03-25 10:55:03 -070052 for classname in dir(module):
Scott Baker750eadd2016-03-31 12:00:35 -070053# print " ",classname
Scott Bakerfb9544a2016-03-25 10:55:03 -070054 c = getattr(module, classname, None)
55
56 if inspect.isclass(c) and issubclass(c, View) and (classname not in globals()):
57 globals()[classname] = c
58
59 method_kind = getattr(c, "method_kind", None)
Scott Bakerd9d55f22016-03-25 13:33:11 -070060 method_name = getattr(c, "method_name", None)
Scott Baker13a287e2016-04-01 16:27:53 -070061 if method_kind:
62 if method_name:
63 method_name = os.path.join(api_path, method_name)
64 else:
65 method_name = api_path
Scott Baker2fa68782016-04-05 14:13:09 -070066 has_index_view = True
Scott Bakerfb9544a2016-03-25 10:55:03 -070067 view_urls.append( (method_kind, method_name, classname, c) )
68
69 elif os.path.isdir(pathname):
Scott Bakera2c76272016-03-30 10:58:18 -070070 urlpatterns.extend(import_api_methods(pathname, os.path.join(api_path, fn), api_module+"." + fn))
Scott Baker2fa68782016-04-05 14:13:09 -070071 subdirs.append(fn)
Scott Bakerfb9544a2016-03-25 10:55:03 -070072
73 for view_url in view_urls:
74 if view_url[0] == "list":
75 urlpatterns.append(url(r'^' + view_url[1] + '/$', view_url[3].as_view(), name=view_url[1]+'list'))
76 elif view_url[0] == "detail":
77 urlpatterns.append(url(r'^' + view_url[1] + '/(?P<pk>[a-zA-Z0-9\-]+)/$', view_url[3].as_view(), name=view_url[1]+'detail'))
78 elif view_url[0] == "viewset":
79 viewset = view_url[3]
Scott Baker795de952016-03-28 13:20:15 -070080 urlpatterns.extend(viewset.get_urlpatterns(api_path="^"+api_path+"/"))
Scott Bakerfb9544a2016-03-25 10:55:03 -070081
Scott Baker2fa68782016-04-05 14:13:09 -070082 if not has_index_view:
83 urlpatterns.append(url('^' + api_path + '/$', XOSIndexViewSet.as_view({'get': 'list'}, view_urls=view_urls, subdirs=subdirs), name="api_path"+"_index"))
84
Scott Bakerfb9544a2016-03-25 10:55:03 -070085 return urlpatterns
Scott Bakerd9d55f22016-03-25 13:33:11 -070086
87urlpatterns = import_api_methods()
88