blob: c58c51f36f104e2e3d3dd6e5ef4969bbf0cde543 [file] [log] [blame]
Scott Bakerc7325a42014-05-30 16:06:46 -07001from view_common import *
2
Scott Bakerc7325a42014-05-30 16:06:46 -07003class DashboardDynamicView(TemplateView):
4 head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %}
5 {% load admin_static %}
6 {% block content %}
7 """
8
Scott Baker7e7cade2014-11-03 14:35:58 -08009 head_wholePage_template = r"""{% extends "admin/wholePage.html" %}
10 {% load admin_static %}
11 {% block content %}
12 """
13
Scott Bakerc7325a42014-05-30 16:06:46 -070014 tail_template = r"{% endblock %}"
15
16 def get(self, request, name="root", *args, **kwargs):
17 context = self.get_context_data(**kwargs)
18 context = getDashboardContext(request.user, context)
19
20 if name=="root":
21 return self.multiDashboardView(request, context)
Scott Bakerb9da9212014-11-03 15:59:52 -080022 elif kwargs.get("wholePage",None):
Scott Baker7e7cade2014-11-03 14:35:58 -080023 return self.singleFullView(request, name, context)
Scott Bakerc7325a42014-05-30 16:06:46 -070024 else:
25 return self.singleDashboardView(request, name, context)
26
Scott Bakerbb53ee82014-06-18 18:26:45 -070027 def readTemplate(self, fn):
Scott Bakerbbe7c822014-07-13 16:48:46 -070028 TEMPLATE_DIRS = ["/opt/planetstack/templates/admin/dashboard/",
29 "/opt/planetstack/core/xoslib/dashboards/"]
30
31 for template_dir in TEMPLATE_DIRS:
32 pathname = os.path.join(template_dir, fn) + ".html"
33 if os.path.exists(pathname):
34 break
35 else:
36 return "failed to find %s in %s" % (fn, TEMPLATE_DIRS)
37
38 template= open(pathname, "r").read()
39 if (fn=="tenant"):
40 # fix for tenant view - it writes html to a div called tabs-5
41 template = '<div id="tabs-5"></div>' + template
42 return template
Scott Bakerc7325a42014-05-30 16:06:46 -070043
Scott Bakerbb53ee82014-06-18 18:26:45 -070044 def embedDashboard(self, url):
45 if url.startswith("template:"):
46 fn = url[9:]
47 return self.readTemplate(fn)
48 elif url.startswith("http"):
49 return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url
Scott Baker55766452014-07-15 10:58:22 -070050 else:
51 return "don't know how to load dashboard %s" % url
Scott Bakerbb53ee82014-06-18 18:26:45 -070052
Scott Bakerc7325a42014-05-30 16:06:46 -070053 def multiDashboardView(self, request, context):
54 head_template = self.head_template
55 tail_template = self.tail_template
56
57 body = """
58 <div id="hometabs" >
59 <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
60 """
61
62 dashboards = request.user.get_dashboards()
63
64 # customize is a special dashboard they always get
65 customize = DashboardView.objects.filter(name="Customize")
66 if customize:
67 dashboards.append(customize[0])
68
69 for i,view in enumerate(dashboards):
Scott Bakerc15ad1b2015-01-18 16:33:30 -080070 # don't display disabled dashboards
71 if (not view.enabled):
72 continue
Scott Bakerc7325a42014-05-30 16:06:46 -070073 body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
74
75 body = body + "</ul>\n"
76
77 for i,view in enumerate(dashboards):
Scott Bakerc15ad1b2015-01-18 16:33:30 -080078 # don't display disabled dashboards
79 if (not view.enabled):
80 continue
81
Scott Bakerc7325a42014-05-30 16:06:46 -070082 url = view.url
83 body = body + '<div id="dashtab-%d">\n' % i
Scott Baker9de6c442014-12-19 17:15:59 -080084 if (view.controllers.all().count()>0):
Scott Baker25e3db42015-01-26 22:12:40 -080085 body = body + 'Controller: <select id="dashselect-%d">' % i;
86 body = body + '<option value="None">(select a controller)</option>';
Scott Bakerc04e1422014-12-23 09:39:06 -080087 for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()):
Scott Baker9de6c442014-12-19 17:15:59 -080088 body = body + '<option value="%d">%s</option>' % (j, controllerdashboard.controller.name)
Scott Baker25e3db42015-01-26 22:12:40 -080089 body = body + '</select><hr>'
Scott Baker9de6c442014-12-19 17:15:59 -080090
Scott Bakerc04e1422014-12-23 09:39:06 -080091 for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()):
Scott Bakerc7a73142015-01-06 23:00:30 -080092 body = body + '<script type="text/template" id="dashtemplate-%d-%d">\n%s\n</script>\n' % (i,j, self.embedDashboard(controllerdashboard.url));
93
94 body = body + '<div id="dashcontent-%d" class="dashcontent"></div>\n' % i
Scott Baker9de6c442014-12-19 17:15:59 -080095
96 body = body + """<script>
Scott Bakerc7a73142015-01-06 23:00:30 -080097 $("#dashselect-%d").change(function() {
Scott Baker9de6c442014-12-19 17:15:59 -080098 v=$("#dashselect-%d").val();
Scott Baker25e3db42015-01-26 22:12:40 -080099 if (v=="None") {
100 $("#dashcontent-%d").html("");
101 return;
102 }
Scott Bakerc7a73142015-01-06 23:00:30 -0800103 $("#dashcontent-%d").html( $("#dashtemplate-%d-" + v).html() );
Scott Baker9de6c442014-12-19 17:15:59 -0800104 });
Scott Baker25e3db42015-01-26 22:12:40 -0800105 //$("#dashcontent-%d").html( $("#dashtemplate-%d-0").html() );
Scott Baker9de6c442014-12-19 17:15:59 -0800106 </script>
Scott Baker25e3db42015-01-26 22:12:40 -0800107 """ % (i,i,i,i,i,i,i);
Scott Baker9de6c442014-12-19 17:15:59 -0800108 else:
109 body = body + self.embedDashboard(url)
Scott Bakerc7325a42014-05-30 16:06:46 -0700110 body = body + '</div>\n'
111
112 body=body+"</div>\n"
113
114 t = template.Template(head_template + body + self.tail_template)
115
116 response_kwargs = {}
117 response_kwargs.setdefault('content_type', self.content_type)
118 return self.response_class(
119 request = request,
120 template = t,
121 context = context,
122 **response_kwargs)
123
124 def singleDashboardView(self, request, name, context):
125 head_template = self.head_template
126 tail_template = self.tail_template
127
Scott Bakerbb53ee82014-06-18 18:26:45 -0700128 t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
Scott Bakerc7325a42014-05-30 16:06:46 -0700129
130 response_kwargs = {}
131 response_kwargs.setdefault('content_type', self.content_type)
132 return self.response_class(
133 request = request,
134 template = t,
135 context = context,
136 **response_kwargs)
137
Scott Baker7e7cade2014-11-03 14:35:58 -0800138 def singleFullView(self, request, name, context):
139 head_template = self.head_wholePage_template
140 tail_template = self.tail_template
141
142 t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
143
144 response_kwargs = {}
145 response_kwargs.setdefault('content_type', self.content_type)
146 return self.response_class(
147 request = request,
148 template = t,
149 context = context,
150 **response_kwargs)
151