automatically determine urls for xoslib methods
authorScott Baker <smbaker@gmail.com>
Sun, 13 Jul 2014 23:21:38 +0000 (16:21 -0700)
committerScott Baker <smbaker@gmail.com>
Sun, 13 Jul 2014 23:21:38 +0000 (16:21 -0700)
planetstack/core/xoslib/methods/__init__.py
planetstack/planetstack/urls.py

index 8b13789..6c75063 100644 (file)
@@ -1 +1,45 @@
+from django.views.generic import View
+from django.conf.urls import patterns, url
+import os, sys
+import inspect
+import importlib
 
+# XXX based on core/dashboard/views/__init__.py
+
+# Find all modules in the current directory that have descendents of the View
+# object, and add them as globals to this module. Also, build up a list of urls
+# based on the "url" field of the view classes.
+
+urlpatterns=[]
+
+sys_path_save = sys.path
+try:
+    # __import__() and importlib.import_module() both import modules from
+    # sys.path. So we make sure that the path where we can find the views is
+    # the first thing in sys.path.
+    view_dir = os.path.dirname(os.path.abspath(__file__))
+    sys.path = [view_dir] + sys.path
+    view_urls = []
+    for fn in os.listdir(view_dir):
+        pathname = os.path.join(view_dir,fn)
+        if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
+            module = __import__(fn[:-3])
+            for classname in dir(module):
+                c = getattr(module, classname, None)
+
+                if inspect.isclass(c) and issubclass(c, View) and (classname not in globals()):
+                    globals()[classname] = c
+
+                    method_kind = getattr(c, "method_kind", None)
+                    method_name = getattr(c, "method_name", None)
+                    if method_kind and method_name:
+                        view_urls.append( (method_kind, method_name, classname, c) )
+
+    for view_url in view_urls:
+        if view_url[0] == "list":
+           urlpatterns.append(url(r'^xoslib/' + view_url[1] + '/$',  view_url[3].as_view(), name=view_url[1]+'list'))
+        elif view_url[0] == "detail":
+           urlpatterns.append(url(r'^xoslib/' + view_url[1] + '/(?P<pk>[a-zA-Z0-9\-]+)/$',  view_url[3].as_view(), name=view_url[1]+'list'))
+
+finally:
+    sys.path = sys_path_save
index eb7f40c..3513a38 100644 (file)
@@ -12,6 +12,7 @@ from core.models import *
 from rest_framework import generics
 from core.dashboard.sites import SitePlus
 from django.http import HttpResponseRedirect
+#from core.xoslib import XOSLibDataView
 
 admin.site = SitePlus()
 admin.autodiscover()
@@ -34,6 +35,8 @@ urlpatterns = patterns('',
     url(r'^', include(admin.site.urls)),
     #url(r'^profile/home', 'core.views.home'),
 
+#    url(r'^admin/xoslib/(?P<name>\w+)/$', XOSLibDataView.as_view(), name="xoslib"),
+
     url(r'^plstackapi/$', api_root),
 
     url(r'^plstackapi/dashboardviews/$', DashboardViewList.as_view(), name='dashboardview-list'),
@@ -119,5 +122,7 @@ urlpatterns = patterns('',
 
     #Adding in rest_framework urls
     url(r'^plstackapi/', include('rest_framework.urls', namespace='rest_framework')),
-    
+
+    # XOSLib rest methods
+    url(r'^xoslib/', include('core.xoslib.methods', namespace='xoslib')),    
 )