Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | from view_common import * |
| 2 | |
| 3 | class 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 | |
| 11 | class 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 Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 28 | def readTemplate(self, fn): |
Scott Baker | bbe7c82 | 2014-07-13 16:48:46 -0700 | [diff] [blame] | 29 | TEMPLATE_DIRS = ["/opt/planetstack/templates/admin/dashboard/", |
| 30 | "/opt/planetstack/core/xoslib/dashboards/"] |
| 31 | |
| 32 | for template_dir in TEMPLATE_DIRS: |
| 33 | pathname = os.path.join(template_dir, fn) + ".html" |
| 34 | if os.path.exists(pathname): |
| 35 | break |
| 36 | else: |
| 37 | return "failed to find %s in %s" % (fn, TEMPLATE_DIRS) |
| 38 | |
| 39 | template= open(pathname, "r").read() |
| 40 | if (fn=="tenant"): |
| 41 | # fix for tenant view - it writes html to a div called tabs-5 |
| 42 | template = '<div id="tabs-5"></div>' + template |
| 43 | return template |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 44 | |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 45 | def embedDashboard(self, url): |
| 46 | if url.startswith("template:"): |
| 47 | fn = url[9:] |
| 48 | return self.readTemplate(fn) |
| 49 | elif url.startswith("http"): |
| 50 | return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url |
| 51 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 52 | def multiDashboardView(self, request, context): |
| 53 | head_template = self.head_template |
| 54 | tail_template = self.tail_template |
| 55 | |
| 56 | body = """ |
| 57 | <div id="hometabs" > |
| 58 | <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab"> |
| 59 | """ |
| 60 | |
| 61 | dashboards = request.user.get_dashboards() |
| 62 | |
| 63 | # customize is a special dashboard they always get |
| 64 | customize = DashboardView.objects.filter(name="Customize") |
| 65 | if customize: |
| 66 | dashboards.append(customize[0]) |
| 67 | |
| 68 | for i,view in enumerate(dashboards): |
| 69 | body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name) |
| 70 | |
| 71 | body = body + "</ul>\n" |
| 72 | |
| 73 | for i,view in enumerate(dashboards): |
| 74 | url = view.url |
| 75 | body = body + '<div id="dashtab-%d">\n' % i |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 76 | body = body + self.embedDashboard(url) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 77 | body = body + '</div>\n' |
| 78 | |
| 79 | body=body+"</div>\n" |
| 80 | |
| 81 | t = template.Template(head_template + body + self.tail_template) |
| 82 | |
| 83 | response_kwargs = {} |
| 84 | response_kwargs.setdefault('content_type', self.content_type) |
| 85 | return self.response_class(
|
| 86 | request = request,
|
| 87 | template = t,
|
| 88 | context = context,
|
| 89 | **response_kwargs) |
| 90 | |
| 91 | def singleDashboardView(self, request, name, context): |
| 92 | head_template = self.head_template |
| 93 | tail_template = self.tail_template |
| 94 | |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 95 | t = template.Template(head_template + self.readTemplate(name) + self.tail_template) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 96 | |
| 97 | response_kwargs = {} |
| 98 | response_kwargs.setdefault('content_type', self.content_type) |
| 99 | return self.response_class(
|
| 100 | request = request,
|
| 101 | template = t,
|
| 102 | context = context,
|
| 103 | **response_kwargs) |
| 104 | |