blob: d53556c772bd2ea83b4fa905f029261dfd34f528 [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 Baker29087132016-03-29 11:14:38 -07008try:
9 from rest_framework.serializers import DictField
10except:
11 raise Exception("Failed check for django-rest-framework >= 3.3.3")
12
Scott Bakerd9d55f22016-03-25 13:33:11 -070013urlpatterns = []
14
15def import_module_from_filename(dirname, fn):
Scott Baker795de952016-03-28 13:20:15 -070016 print "importing", dirname, fn
Scott Bakerfb9544a2016-03-25 10:55:03 -070017 sys_path_save = sys.path
18 try:
19 # __import__() and importlib.import_module() both import modules from
20 # sys.path. So we make sure that the path where we can find the views is
21 # the first thing in sys.path.
Scott Bakerd9d55f22016-03-25 13:33:11 -070022 sys.path = [dirname] + sys.path
Scott Bakerfb9544a2016-03-25 10:55:03 -070023
24 module = __import__(fn[:-3])
25 finally:
26 sys.path = sys_path_save
27
28 return module
29
Scott Bakera2c76272016-03-30 10:58:18 -070030def import_module_by_dotted_name(name):
31 print "import", name
32 module = __import__(name)
33 for part in name.split(".")[1:]:
34 module = getattr(module, part)
35 return module
36
37def import_api_methods(dirname=None, api_path="api", api_module="api"):
Scott Bakerfb9544a2016-03-25 10:55:03 -070038 subdirs=[]
Scott Bakerd9d55f22016-03-25 13:33:11 -070039 urlpatterns=[]
Scott Bakerfb9544a2016-03-25 10:55:03 -070040
Scott Bakerd9d55f22016-03-25 13:33:11 -070041 if not dirname:
42 dirname = os.path.dirname(os.path.abspath(__file__))
Scott Bakerfb9544a2016-03-25 10:55:03 -070043
Scott Bakerd9d55f22016-03-25 13:33:11 -070044 view_urls = []
45 for fn in os.listdir(dirname):
46 pathname = os.path.join(dirname,fn)
Scott Bakerfd38f132016-03-28 13:51:52 -070047 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 -070048 #module = import_module_from_filename(dirname, fn)
49 module = import_module_by_dotted_name(api_module + "." + fn[:-3])
Scott Bakerfb9544a2016-03-25 10:55:03 -070050 for classname in dir(module):
Scott Baker750eadd2016-03-31 12:00:35 -070051# print " ",classname
Scott Bakerfb9544a2016-03-25 10:55:03 -070052 c = getattr(module, classname, None)
53
54 if inspect.isclass(c) and issubclass(c, View) and (classname not in globals()):
55 globals()[classname] = c
56
57 method_kind = getattr(c, "method_kind", None)
Scott Bakerd9d55f22016-03-25 13:33:11 -070058 method_name = getattr(c, "method_name", None)
Scott Baker13a287e2016-04-01 16:27:53 -070059 if method_kind:
60 if method_name:
61 method_name = os.path.join(api_path, method_name)
62 else:
63 method_name = api_path
Scott Bakerfb9544a2016-03-25 10:55:03 -070064 view_urls.append( (method_kind, method_name, classname, c) )
65
66 elif os.path.isdir(pathname):
Scott Bakera2c76272016-03-30 10:58:18 -070067 urlpatterns.extend(import_api_methods(pathname, os.path.join(api_path, fn), api_module+"." + fn))
Scott Bakerfb9544a2016-03-25 10:55:03 -070068
69 for view_url in view_urls:
70 if view_url[0] == "list":
71 urlpatterns.append(url(r'^' + view_url[1] + '/$', view_url[3].as_view(), name=view_url[1]+'list'))
72 elif view_url[0] == "detail":
73 urlpatterns.append(url(r'^' + view_url[1] + '/(?P<pk>[a-zA-Z0-9\-]+)/$', view_url[3].as_view(), name=view_url[1]+'detail'))
74 elif view_url[0] == "viewset":
75 viewset = view_url[3]
Scott Baker795de952016-03-28 13:20:15 -070076 urlpatterns.extend(viewset.get_urlpatterns(api_path="^"+api_path+"/"))
Scott Bakerfb9544a2016-03-25 10:55:03 -070077
78 return urlpatterns
Scott Bakerd9d55f22016-03-25 13:33:11 -070079
80urlpatterns = import_api_methods()
81