blob: 6cb25a0998c567d53d8b1c9071234885c3cd49b6 [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
17 tail_template = r"{% endblock %}"
18
19 def get(self, request, name="root", *args, **kwargs):
20 context = self.get_context_data(**kwargs)
21 context = getDashboardContext(request.user, context)
22
23 if name=="root":
24 return self.multiDashboardView(request, context)
25 else:
26 return self.singleDashboardView(request, name, context)
27
Scott Bakerbb53ee82014-06-18 18:26:45 -070028 def readTemplate(self, fn):
Scott Bakerc7325a42014-05-30 16:06:46 -070029 try:
30 template= open("/opt/planetstack/templates/admin/dashboard/%s.html" % fn, "r").read()
31 if (fn=="tenant"):
32 # fix for tenant view - it writes html to a div called tabs-5
33 template = '<div id="tabs-5"></div>' + template
34 return template
35 except:
36 return "failed to open %s" % fn
37
Scott Bakerbb53ee82014-06-18 18:26:45 -070038 def embedDashboard(self, url):
39 if url.startswith("template:"):
40 fn = url[9:]
41 return self.readTemplate(fn)
42 elif url.startswith("http"):
43 return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url
44
Scott Bakerc7325a42014-05-30 16:06:46 -070045 def multiDashboardView(self, request, context):
46 head_template = self.head_template
47 tail_template = self.tail_template
48
49 body = """
50 <div id="hometabs" >
51 <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
52 """
53
54 dashboards = request.user.get_dashboards()
55
56 # customize is a special dashboard they always get
57 customize = DashboardView.objects.filter(name="Customize")
58 if customize:
59 dashboards.append(customize[0])
60
61 for i,view in enumerate(dashboards):
62 body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
63
64 body = body + "</ul>\n"
65
66 for i,view in enumerate(dashboards):
67 url = view.url
68 body = body + '<div id="dashtab-%d">\n' % i
Scott Bakerbb53ee82014-06-18 18:26:45 -070069 body = body + self.embedDashboard(url)
Scott Bakerc7325a42014-05-30 16:06:46 -070070 body = body + '</div>\n'
71
72 body=body+"</div>\n"
73
74 t = template.Template(head_template + body + self.tail_template)
75
76 response_kwargs = {}
77 response_kwargs.setdefault('content_type', self.content_type)
78 return self.response_class(
79 request = request,
80 template = t,
81 context = context,
82 **response_kwargs)
83
84 def singleDashboardView(self, request, name, context):
85 head_template = self.head_template
86 tail_template = self.tail_template
87
Scott Bakerbb53ee82014-06-18 18:26:45 -070088 t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
Scott Bakerc7325a42014-05-30 16:06:46 -070089
90 response_kwargs = {}
91 response_kwargs.setdefault('content_type', self.content_type)
92 return self.response_class(
93 request = request,
94 template = t,
95 context = context,
96 **response_kwargs)
97