From bb7e84f2d495da8dc5c4786a6f66ce95e5d413d2 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 16 Jun 2014 23:08:43 -0700 Subject: [PATCH] figure out dashboard view classes and urls automatically --- planetstack/core/dashboard/sites.py | 15 ++++-- planetstack/core/dashboard/views/__init__.py | 51 +++++++++++++++++--- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/planetstack/core/dashboard/sites.py b/planetstack/core/dashboard/sites.py index 5d670d0..7200035 100644 --- a/planetstack/core/dashboard/sites.py +++ b/planetstack/core/dashboard/sites.py @@ -17,9 +17,14 @@ class AdminMixin(object): TenantViewData,TenantCreateSlice, TenantAddOrRemoveSliverView, TenantPickSitesView, TenantDeleteSliceView, \ TenantUpdateSlice, DashboardSliceInteractions + from views import view_urls + urls = super(AdminMixin, self).get_urls() del urls[0] - custom_url = patterns('', + + # these ones are for the views that were written before we implemented + # the ability to get the url from the View class. + dashboard_urls = [ url(r'^$', self.admin_view(DashboardDynamicView.as_view()), name="index"), url(r'^test/', self.admin_view(DashboardUserSiteView.as_view()), @@ -54,9 +59,13 @@ class AdminMixin(object): name="picksites"), url(r'^tenantdeleteslice/$', self.admin_view(TenantDeleteSliceView.as_view()), name="tenantdeleteslice") - ) + ] + + # these ones are for the views that have a "url" member in the class + for (view_url, view_classname, view_class) in view_urls: + dashboard_urls.append( url(view_url, self.admin_view(view_class.as_view()), name=view_classname.lower())) - return custom_url + urls + return dashboard_urls + urls class SitePlus(AdminMixin, AdminSite): diff --git a/planetstack/core/dashboard/views/__init__.py b/planetstack/core/dashboard/views/__init__.py index a152700..bbe403b 100644 --- a/planetstack/core/dashboard/views/__init__.py +++ b/planetstack/core/dashboard/views/__init__.py @@ -1,9 +1,44 @@ -from home import DashboardWelcomeView, DashboardDynamicView -from tenant import TenantCreateSlice, TenantUpdateSlice, TenantDeleteSliceView, TenantAddOrRemoveSliverView, TenantPickSitesView, TenantViewData -from simulator import SimulatorView -from cdn import DashboardSummaryAjaxView, DashboardAddOrRemoveSliverView, DashboardAjaxView -from analytics import DashboardAnalyticsAjaxView -from customize import DashboardCustomize -from interactions import DashboardSliceInteractions -from test import DashboardUserSiteView +#from home import DashboardWelcomeView, DashboardDynamicView +#from tenant import TenantCreateSlice, TenantUpdateSlice, TenantDeleteSliceView, TenantAddOrRemoveSliverView, TenantPickSitesView, TenantViewData +#from simulator import SimulatorView +#from cdn import DashboardSummaryAjaxView, DashboardAddOrRemoveSliverView, DashboardAjaxView +#from analytics import DashboardAnalyticsAjaxView +#from customize import DashboardCustomize +#from interactions import DashboardSliceInteractions +#from test import DashboardUserSiteView +from django.views.generic import View +from django.conf.urls import patterns, url +import os, sys +import inspect +import importlib + +# 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. + +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 = imp.load_source(fn[:-3],pathname) + #module = importlib.import_module(fn[:-3]) + 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 + + view_url = getattr(c, "url", None) + if view_url: + view_urls.append( (view_url, classname, c) ) +finally: + sys.path = sys_path_save -- 2.43.0