blob: 6f57666c161ade5ad264e64fc1d9d5d21b6f7574 [file] [log] [blame]
Scott Bakerc7325a42014-05-30 16:06:46 -07001from view_common import *
2
3class DashboardWelcomeView(TemplateView):
4 template_name = 'admin/dashboard/welcome.html'
5
6 def get(self, request, *args, **kwargs):
7 context = self.get_context_data(**kwargs)
8 context = getDashboardContext(request.user, context)
9 return self.render_to_response(context=context)
10
11class DashboardDynamicView(TemplateView):
12 head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %}
13 {% load admin_static %}
14 {% block content %}
15 """
16
Scott Baker7e7cade2014-11-03 14:35:58 -080017 head_wholePage_template = r"""{% extends "admin/wholePage.html" %}
18 {% load admin_static %}
19 {% block content %}
20 """
21
Scott Bakerc7325a42014-05-30 16:06:46 -070022 tail_template = r"{% endblock %}"
23
24 def get(self, request, name="root", *args, **kwargs):
25 context = self.get_context_data(**kwargs)
26 context = getDashboardContext(request.user, context)
27
28 if name=="root":
29 return self.multiDashboardView(request, context)
Scott Bakerb9da9212014-11-03 15:59:52 -080030 elif kwargs.get("wholePage",None):
Scott Baker7e7cade2014-11-03 14:35:58 -080031 return self.singleFullView(request, name, context)
Scott Bakerc7325a42014-05-30 16:06:46 -070032 else:
33 return self.singleDashboardView(request, name, context)
34
Scott Bakerbb53ee82014-06-18 18:26:45 -070035 def readTemplate(self, fn):
Scott Bakerbbe7c822014-07-13 16:48:46 -070036 TEMPLATE_DIRS = ["/opt/planetstack/templates/admin/dashboard/",
37 "/opt/planetstack/core/xoslib/dashboards/"]
38
39 for template_dir in TEMPLATE_DIRS:
40 pathname = os.path.join(template_dir, fn) + ".html"
41 if os.path.exists(pathname):
42 break
43 else:
44 return "failed to find %s in %s" % (fn, TEMPLATE_DIRS)
45
46 template= open(pathname, "r").read()
47 if (fn=="tenant"):
48 # fix for tenant view - it writes html to a div called tabs-5
49 template = '<div id="tabs-5"></div>' + template
50 return template
Scott Bakerc7325a42014-05-30 16:06:46 -070051
Scott Bakerbb53ee82014-06-18 18:26:45 -070052 def embedDashboard(self, url):
53 if url.startswith("template:"):
54 fn = url[9:]
55 return self.readTemplate(fn)
56 elif url.startswith("http"):
57 return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url
Scott Baker55766452014-07-15 10:58:22 -070058 else:
59 return "don't know how to load dashboard %s" % url
Scott Bakerbb53ee82014-06-18 18:26:45 -070060
Scott Bakerc7325a42014-05-30 16:06:46 -070061 def multiDashboardView(self, request, context):
62 head_template = self.head_template
63 tail_template = self.tail_template
64
65 body = """
66 <div id="hometabs" >
67 <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
68 """
69
70 dashboards = request.user.get_dashboards()
71
72 # customize is a special dashboard they always get
73 customize = DashboardView.objects.filter(name="Customize")
74 if customize:
75 dashboards.append(customize[0])
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
Scott Bakerc7325a42014-05-30 16:06:46 -070081 body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
82
83 body = body + "</ul>\n"
84
85 for i,view in enumerate(dashboards):
Scott Bakerc15ad1b2015-01-18 16:33:30 -080086 # don't display disabled dashboards
87 if (not view.enabled):
88 continue
89
Scott Bakerc7325a42014-05-30 16:06:46 -070090 url = view.url
91 body = body + '<div id="dashtab-%d">\n' % i
Scott Baker9de6c442014-12-19 17:15:59 -080092 if (view.controllers.all().count()>0):
93 body = body + '<select id="dashselect-%d">' % i;
Scott Bakerc04e1422014-12-23 09:39:06 -080094 for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()):
Scott Baker9de6c442014-12-19 17:15:59 -080095 body = body + '<option value="%d">%s</option>' % (j, controllerdashboard.controller.name)
96 body = body + '</select>'
97
Scott Bakerc04e1422014-12-23 09:39:06 -080098 for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()):
Scott Bakerc7a73142015-01-06 23:00:30 -080099 body = body + '<script type="text/template" id="dashtemplate-%d-%d">\n%s\n</script>\n' % (i,j, self.embedDashboard(controllerdashboard.url));
100
101 body = body + '<div id="dashcontent-%d" class="dashcontent"></div>\n' % i
Scott Baker9de6c442014-12-19 17:15:59 -0800102
103 body = body + """<script>
Scott Bakerc7a73142015-01-06 23:00:30 -0800104 $("#dashselect-%d").change(function() {
Scott Baker9de6c442014-12-19 17:15:59 -0800105 v=$("#dashselect-%d").val();
Scott Bakerc7a73142015-01-06 23:00:30 -0800106 $("#dashcontent-%d").html( $("#dashtemplate-%d-" + v).html() );
Scott Baker9de6c442014-12-19 17:15:59 -0800107 });
Scott Bakerc7a73142015-01-06 23:00:30 -0800108 $("#dashcontent-%d").html( $("#dashtemplate-%d-0").html() );
Scott Baker9de6c442014-12-19 17:15:59 -0800109 </script>
110 """ % (i,i,i,i,i,i);
111 else:
112 body = body + self.embedDashboard(url)
Scott Bakerc7325a42014-05-30 16:06:46 -0700113 body = body + '</div>\n'
114
115 body=body+"</div>\n"
116
117 t = template.Template(head_template + body + self.tail_template)
118
119 response_kwargs = {}
120 response_kwargs.setdefault('content_type', self.content_type)
121 return self.response_class(
122 request = request,
123 template = t,
124 context = context,
125 **response_kwargs)
126
127 def singleDashboardView(self, request, name, context):
128 head_template = self.head_template
129 tail_template = self.tail_template
130
Scott Bakerbb53ee82014-06-18 18:26:45 -0700131 t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
Scott Bakerc7325a42014-05-30 16:06:46 -0700132
133 response_kwargs = {}
134 response_kwargs.setdefault('content_type', self.content_type)
135 return self.response_class(
136 request = request,
137 template = t,
138 context = context,
139 **response_kwargs)
140
Scott Baker7e7cade2014-11-03 14:35:58 -0800141 def singleFullView(self, request, name, context):
142 head_template = self.head_wholePage_template
143 tail_template = self.tail_template
144
145 t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
146
147 response_kwargs = {}
148 response_kwargs.setdefault('content_type', self.content_type)
149 return self.response_class(
150 request = request,
151 template = t,
152 context = context,
153 **response_kwargs)
154